偶然的机会坐上了python的贼船,无奈只能一步步踏上王者之巅。。。。。python
参考博客地址:https://mp.weixin.qq.com/s/OZVT3iFrpFReqdYqVhUf6gapp
1.交换赋值ide
##不推荐 temp = a a = b b = a ##推荐 a, b = b, a #先生成一个元组(tuple)对象,而后unpack
2.uppackui
##不推荐 l = ['David', 'Pythonista', '+1-514-555-1234'] first_name = l[0] last_name = l[1] phone_number = l[2] ##推荐 l = ['David', 'Pythonista', '+1-514-555-1234'] first_name, last_name, phone_number = l
3.使用操做符 in 判断spa
#不推荐 if fruit == "apple" or fruit == "orange" or fruit == "berry": # 屡次判断 ##推荐 if fruit in ["apple", "orange", "berry"]: # 使用 in 更加简洁
4.字符串操做code
##不推荐 colors = ['red', 'blue', 'green', 'yellow'] result = '' for s in colors: result += s # 每次赋值都丢弃之前的字符串对象, 生成一个新对象 ##推荐 colors = ['red', 'blue', 'green', 'yellow'] result = ''.join(colors) # 没有额外的内存分配
join用法 :用于将序列中的元素以指定的字符链接生成一个新的字符串。对象
str = "-"; seq = ("a", "b", "c"); # 字符串序列 print str.join( seq ); 结果:a-b-c
5.字典键值列表blog
my_dict = {1:'a',2:'b'} ##不推荐 for key in my_dict.keys(): print(my_dict[key]) ##推荐 for key in my_dict: print(my_dict[key]) # 只有当循环中须要更改key值的状况下,咱们须要使用 my_dict.keys() # 生成静态的键值列表。 结果:a b a b
6.字典键值判断内存
my_dict = {1:'a',2:'b'} key = 1 ##推荐 if key in my_dict: print('True') #另外一种方法,效率不知 if my_dict.get(key): print('True')
7.字符串