1.运算符:
数字运算符:+、-、*、/、% 、//
整体注释:ctrl+?, 判断条件:in、 not in
字符串:
布尔值:ture (真) false(假)
逻辑运算:and or not
赋值运算:如count=count+1等于count+=1,其他同理可得。
结果是值的有:算数运算和赋值运算
结果是布尔值的有:比较运算、逻辑运算、成员运算
2.基本数据类型:
2.1数字:a1=123;a2=2341
数字:int,所有的功能都在int里,python3里int类型无范围,python2里有整形和长整形
2.1.1 -int: a=“123”
b=int(a): 将字符串转换为数字。注意:a=‘123a’不能转换
如果想要查看是什么类型,用“type”; print(type(a),a)
num='0011'
v=int(num,base=16):将num以16进制转换
print(v)
2.2.2 -bit_length:
#:当前数字的二进制,至少用n为表示
age=8
r=(age).bit_length()
2.3列表-list
通过list类创建的对象,如:
li=[1,12,90,['李明',"Succ",78],"alex","age"] print(li[3:-1]) #切片,切片的结果也是列表 for item in li: #for循环取值 print(item)
列表可以被修改、删除:
li=[1,12,90,['李明',"Succ",78],"alex","age"] li[1]=120 #修改 print(li) del li[2] #删除 print(li)
列表的in操作:
li=[1,12,90,['李明',"Succ",78],"alex","age"] v=li[3][0][1] print(v)
转换:
(1)字符串转换为列表:
li=list("avaswmdqwklenf") print(li)
(2)列表转换为字符串:
#用for循环一个一个处理:既有数字又有字符串 a=["success",1234] for i in (str(a)): print(i) #直接用join方法:列表中的元素只有字符串 a=["success","1234"] v=" ".join(a) print(v)
列表的追加、删除、复制
li = [1, 12, 90, ['李明', "Succ", 78], "alex", "age", 90, 90] li.append(5) print(li) v=li.clear() print(v) li.copy() print(li)
计算列表中元素出现的次数:
li = [1, 12, 90, ['李明', "Succ", 78], "alex", "age", 90, 90] v=li.count(90) print(v)
li = [1, 12, 90, ['李明', "Succ", 78], "alex", "age", 90, 90] v=li.index(90) #寻找值 print(v) li.extend([9988,"liing",]) #执行了循环 print(li) li.extend("liing",) print(li) li.insert(2,99) #在指定索引位置插入 print(li) v=li.pop(3) #默认情况下,删除某一值,并获取相关值 print(li) print(v) li.remove(12) #删除列表中的指定值,左边优先 print(li)
2.4元祖-tuple
元组:元素不可修改,不能增加或删除,一般写元组在最后加一个‘,’号。
tu = (11, 'alex', (11, 22), [(33, 44)], True, 45, 11,) # 索引取值 v=tu[3] print(v) # 切片 v=tu[0:3] print(v) # for 循环 for item in tu: print(item) # 元组有序 v=tu[3][0][1] print(v) # count:获取指定元素在元组中出现的次数 v=tu.count(11) print(v) # index索引 v=tu.index('alex') print(v)View Code
2.5字典-dict
# 字典:dict # 基本结构 info={ "k1":"v1", # 键值对 "k2":"v2", } # 字典中的value可以是任意值 info = { "k1": 18, "k2": True, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11,22), } ], "k4": (11, 22, 33, 44), } v=info["k3"][5]["kk3"][0] print(v) # 列表、字典不能作为字典的key,布尔值会与(0,1)重复 info = { "k1": 18, 1: 123, True: [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22), } ], "k4": (11, 22, 33, 44), } print(info)View Code
字典中的for循环
# for循环 info = { "k1": 18, 3: 123, True: [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11,22), } ], "k4": (11, 22, 33, 44), } for item in info: #默认关键词 print(item) for item in info.values(): print(item) for k,v in info.items(): print(k,v)View Code
# 判断 dic={ "k1":"v1" } v="k" in dic #判断k是否是关键词 print(v) v="v1" in dic.values() print(v) # fromkeys:根据序列,创建字典,指定统一的值 dic = { "k1": "v1" } v = dic.fromkeys(["k1", 123, "99"]) print(v) v = dic.fromkeys(["k1", 123, "99"], 123) print(v) # get:根据key获取值,key不存在是,可指定默认值 v=dic.get("k1") print(v) v=dic.get("k111",111) print(v) # pop指定某个ki删除 v=dic.pop("k1") print(dic,v) v=dic.pop("k111",90) print(dic,v) # popitem:随机删除 dic = { "k1": "v1", "k2": "v2", "k3": "v3", } v=dic.popitem() print(dic,v) k, v = dic.popitem() print(dic, k, v) # setdefault:设置值,已存在,不设置,获取当前key对应的值;不存在,设置 v=dic.setdefault('k1','123') print(dic,v) v=dic.setdefault('k11','123') print(dic,v) # update dic.update(k1=123,k3=456,k5="adcs") print(dic)View Code