一、python运算符:
二、数据类型:
1、数字:
int :整型
32位机器上一个整数取值范围为-2**31~2**31-1即-2147483648~2147483647
64位机器上一个整数取值范围为-2**63~2**63-1即-9223372036854775808~~9223372036854775807
Long:长整型
Float:浮点型
2、布尔值
0或者1;真或者假
3、字符串
移除空白(lstrip;rstrip)
a = " adfadf " spcae = a.lstrip() #移除左边空白 print(spcae) a = " adfadf " spcae = a.rstrip() #移除右边空白 print(spcae)
分割(split)
1 a = " adfadfce " 2 spcae = a.split("f",1) 3 print(spcae)
长度(__len__())
索引()
a = " adfadfce " #前后是有空格的
spcae = a[-4]
f
切片
1 a = " adfadfce " 2 spcae = a[1::2] #表示以步长去切片。取出来的值为afdc 3 a = " adfadfce " 4 spcae = a[:7] #表示从头开始到第六个位,不包含7。取出来的值为 adfadf备注最前面有个空格的字符。
4、列表:
创建列表:
1 #name_list = [ 'xxd','yb','wc','hwq', ] 2 name_list = list ([ 'xxd','yb','wc','hwq', ]) 3 print(name_list)
索引
1 #name_list = [ 'xxd','yb','wc','hwq', ] 2 name_list = list ([ 'xxd','yb','wc','hwq', ]) 3 #print(name_list) 4 name_index = name_list[0] #表示第一个元素0表示第一个元素 5 name_index1 = name_list[1] #表示第二个元素 6 name_index2 = name_list[2] #表示第三个元素 7 name_index3 = name_list[1:3] #表示第二个元素到底三个元素,三表示第四个元素但是不含,所以实际上是不包含第四个元素hwq 8 print(name_index) 9 print(name_index1) 10 print(name_index2) 11 print(name_index3) 12 输出的结果是: 13 xxd 14 yb 15 wc 16 ['yb', 'wc']
切片
1 name_list = [ 'xxd','yb','wc','hwq','gk','cp','tc' ] 2 name_spl = name_list[0:1] 3 name_spl2 = name_list[4:] 4 name_spl3 = name_list[3:5] 5 print(name_spl) 6 print(name_spl2) 7 print(name_spl3) 8 结果运行 9 ['xxd'] 10 ['gk', 'cp', 'tc'] 11 ['hwq', 'gk'] 12 其实规则与之前的类似这里不累述:
追加(append)
1 name_list = [ 'xxd','yb','wc','hwq','gk','cp','tc' ] 2 name_list.append('ysl'); 3 print(name_list) 4 5 运行结果: 6 ['xxd', 'yb', 'wc', 'hwq', 'gk', 'cp', 'tc', 'ysl'] 末尾追加了ysl
删除
1 name_list = [ 'xxd','yb','wc','hwq','gk','cp','tc' ] 2 del name_list[3] #删除第四个元素. 3 print(name_list) 4 结果是: 5 ['xxd', 'yb', 'wc', 'gk', 'cp', 'tc'] 6 7 name_list = [ 'xxd','yb','wc','hwq','gk','cp','tc' ] 8 #del name_list[3] 9 name_list.pop() #删除最后一位。 10 print(name_list) 11 结果: 12 ['xxd', 'yb', 'wc', 'hwq', 'gk', 'cp'] 13 14 15 name_list = [ 'xxd','yb','wc','hwq','gk','cp','tc' ] 16 #del name_list[3] 17 #name_list.pop() 18 name_list.remove('xxd') #删除指定的元素xxd 19 print(name_list) 20 输出结果: 21 ['yb', 'wc', 'hwq', 'gk', 'cp', 'tc']
长度
1 name_list = [ 'xxd','yb','wc','hwq','gk','cp','tc' ] 2 LENth = len(name_list) 3 print(LENth) 4 5 结果: 6 7 7
包含
1 name_list = [ 'xxd','yb','wc','hwq','gk','cp','tc' ] 2 user = 'xxd' 3 print (user in name_list) 4 结果: 5 true 因为xxd包含在了列表中 6 7 8 name_list = [ 'xxd','yb','wc','hwq','gk','cp','tc' ] 9 user = 'yyd' 10 print (user in name_list) 11 结果为: 12 false 因为yyd包含在了列表中
5、元组:
创建:
1 ages = (11, 22, 33, 44, 55) 2 或 3 ages = tuple((11, 22, 33, 44, 55))
元组与列表相似,只是没有了修改的功能,可以参考列表操作:索引、切片、长度、包含
6、字典:
创建:
1 place = {"sheng":"sichuang",'shenghui':"chengdu"} 2 或者 3 place = dict{"sheng":"sichuang",'shenghui':"chengdu"}
索引:
1 place = {"sheng":"sichuan",'shenghui':"chengdu"} 2 print(place['shenghui']) 3 place = {"sheng":['sichuan','yunnan','guizhou','shanxi','xizang',],'diqu':['east','south','west','north',]} 4 print(place['diqu']) 5 结果: 6 "chengdu" 7 8 ['east','south','west','north',]
新增
1 place = {"sheng":['sichuan','yunnan','guizhou','shanxi','xizang',],'diqu':['east','sout 2 place['zhou'] = ['Asia','Amaric','Eu'] 3 print(place) 4 结果在输出中含有: 5 'zhou': ['Asia', 'Amaric', 'Eu']
新增还有append参考列表
删除:
del adict[key] 删除键key的项 / del adict 删除整个字典/adict.pop(key) 删除键key的项并返回key对应的 value值
1 place = {'diqu': ['east', 'south', 'west', 'north'], 'zhou': ['Asia', 'Amaric', 'Eu'], 'sheng': ['sichuan', 'yunnan', 'guizhou', 'shanxi', 'xizang']} 2 del place['zhou'] 3 print(place) 4 显示结果已经删除了 'zhou': ['Asia', 'Amaric', 'Eu'],' 5 {'diqu': ['east', 'south', 'west', 'north'], 'sheng': ['sichuan', 'yunnan', 'guizhou', 'shanxi', 'xizang']} 6 place = {'diqu': ['east', 'south', 'west', 'north'], 'zhou': ['Asia', 'Amaric', 'Eu'], 'sheng': ['sichuan', 'yunnan', 'guizhou', 'shanxi', 'xizang']} 7 place.pop('zhou') 8 print(place)
键、值、键值对
循环
长度len()
1 place = {"sheng":['sichuan','yunnan','guizhou','shanxi','xizang',],'diqu':['east','sout']} 2 print len(place)