深度学习机器视觉课前基础准备-python数据结构(1)
数字(Number)
-
Python Number 数据类型用于存储数值,包括整型、长整型、浮点型、复数。
-
Python 中数学运算常用的函数基本都在 math 模块。
下列几个python种常见的数字处理函数
返回上入整数
import math ptint(math.ceil(4.1))
输出为:
5
返回下舍整数
import math print(math.floor(4.1))
输出为
4
返回数字绝对值
import math print(math.fabs(-10))
输出为
10.0
返回数字平方根
import math print(math.sqrt(9))
输出为
3.0
返回e的c次幂
import math print(math.exp(1))
输出为
2.718281828459045
-
Python 中随机数
随机生成一个[0,1)范围内的实数import random ran = random.random() print(ran)
输出为
0.12770500612024305
运行多次输出结果不同 (真随机)
随机生成一个[1,20)范围内的整数import random ran = random.randint(1,20) print(ran)
运行多次输出结果不同 (真随机)
当用random.seed()设置好种子后,random()生成的随机数就是同一个import random print("--------设置种子 seed--------") random.seed(10) ran=random.random() print("Random Number with seed 10:",ran)
输出为
--------设置种子 seed-------- Random Number with seed 10: 0.5714025946899135
运行多次输出结果相同 (假随机)
字符串(string)
-
单引号、双引号、三引号
-
Python中的字符串可以使用单引号 、双引号和三引号(三个单引号或三个双引号)括起来,使用反斜杠 \ 转义特殊字符
下例方面理解
print("hello world") print('''hello world''')
输出依次为( 故" "与’’’ ‘’'等效)
hello world hello world
print("The \t is Tab") print("I\'m going to the movies!")
输出依次为(便于理解转义符 \ 的用途)
The is Tab I'm going to the movies!
对于第二个输出“I‘m going to the movies!”而言,转义符存在于不存在的输出结果相同。
-
字符串连接
下例方便理解
使用+运算符str1="hello " str2="world!" print(str1+str2)
输出结果为(两字符串+连接是直接暴力相接,中间不会自动加空格或其他东西)
hello world!
使用join运算符
new_str = '-'.join("hello") print(new_str)
输出结果为
h-e-l-l-o