一、数据类型
1)数字 (整形,长整形,浮点型,复数)
2)字符串
3)字节串 :在介绍字符编码时介绍字节bytes类型
4)列表
5)元组
6)字典
7)集合
1、数字
1)整型与浮点型
#整型int
作用:年纪,等级,身份证号,qq号等整型数字相关
定义:
age=10 #本质age=int(10)
#浮点型float
作用:薪资,身高,体重,体质参数等浮点数相关
salary=3000.3 #本质salary=float(3000.3)
#二进制,十进制,八进制,十六进制
2)其他数字类型(了解)
#长整形(了解)
在python2中(python3中没有长整形的概念):
>>> num=2L
>>> type(num)
<type 'long'>
#复数(了解)
>>> x=1-2j
>>> x.real
1.0
>>> x.imag
-2.0
2、字符串
#作用:名字,性别,国籍,地址等描述信息
#定义:在单引号\双引号\三引号内,由一串字符组成
name='egon'
#优先掌握的操作:
#1、按索引取值(正向取+反向取) :只能取
#2、切片(顾头不顾尾,步长)
#3、长度len
#4、成员运算in和not in
#5、移除空白strip
#6、切分split
#7、循环
需要掌握的操作
#1、strip,lstrip,rstrip
#2、lower,upper
#3、startswith,endswith
#4、format的三种玩法
#5、split,rsplit
#6、join
#7、replace
#8、isdigit
代码:
#strip
name='*egon**'
print(name.strip('*'))
print(name.lstrip('*'))
print(name.rstrip('*'))
#lower,upper
name='abcdfg'
print(name.lower())
print(name.upper())
#startswith,endswith
name='rxz_AC'
print(name.endswith('AC'))
print(name.startswith('rxz'))
#format的三种玩法
res='{} {} {}'.format('lida',18,'male')
res='{1} {0} {1}'.format('lida',18,'male')
res='{name} {age} {sex}'.format(sex='male',name='lida',age=18)
#split
name='root:x:0:0::/root:/bin/bash'
print(name.split(':')) #默认分隔符为空格
name='C:/a/b/c/d.txt' #只想拿到顶级目录
print(name.split('/',1))
name='a|b|c'
print(name.rsplit('|',1)) #从右开始切分
#join
tag=' '
print(tag.join(['egon','say','hello','world'])) #可迭代对象必须都是字符串
#replace
name='abc say :i have one tesla,my name is abc'
print(name.replace('abc','ABC',1))
#isdigit:可以判断bytes和unicode类型,是最常用的用于于判断字符是否为"数字"的方法
age=input('>>: ')
print(age.isdigit())