Python3学习之路 day1


一、六大数据类型

不可变数据: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

(三)List 列表

(四)Tuple 元组

(五)Set 集合

(六)Dictionary 字典

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值