一、六大数据类型
不可变数据:number、tuple、string
可变数据:list、set、dictionary
(一)Number 数字
int、float、boolean、complex(复数)
常用数学函数:
- abs(x)
- pow(x,y) //需导入math模块
- sqrt(x) //需导入math模块
随机数函数-------需导入random模块
import random
print('range10',range(10))
print('choice',random.choice(range(10))) #从一个序列中随机选择一个元素
print('random',int(random.random()*10)) #random()随机生成0到1的实数
print('randrange:从指定范围内,按指定基数递增的集合中获取一个随机数,基数默认值为 1',random.randrange(1,10,2))
list=[20, 16, 10, 5]
random.shuffle(list) #shuffle将一段序列重新随机排序,函数本身返回值为none
print('shuffle:打乱序列顺序',list)
print('uniform:随机生成[x,y]范围内的实数',random.uniform(1,10))
输出结果:
range10 range(0, 10)
choice 3
random 1
randrange:从指定范围内,按指定基数递增的集合中获取一个随机数,基数默认值为 1 1
shuffle:打乱序列顺序 [20, 10, 16, 5]
uniform:随机生成[x,y]范围内的实数 7.3342171795521125
(二)String 字符串
字符串不可变,例如:word [0] = 'm’会报错
a="hello"
b="world"
print("a+b=",a+b)
print("a*2=",a*2) #表示输出两次
print("左闭右开 a[:2]=",a[:2])
print("'O' in a? ",'o' in a)
print("'P' in a?",'P' in a)
print("'D' not in b?",'D' not in b)
print("'w' not in b?",'w' not in b)
输出结果
a+b= helloworld
a*2= hellohello
左闭右开 a[:2]= he
'O' in a? True
'P' in a? False
'D' not in b? True
'w' not in b? False
repr和str的使用可美化输出格式
str(): 函数返回一个用户易读的表达形式。
repr(): 产生一个解释器易读的表达形式。
s='str'
print(s)
print(repr(s))
print(str(s))
st=input("请输入:")
print(st)
print(repr(st))
print(str(st))
输出结果
str
'str'
str