公共方法的总结
-
算术运算符
-
加法运算符:用于字符串,元组,列表
-
乘法运算符:用于字符串,元组,列表
-
减法运算符:只用于集合
print('hello'+' '+'word') print([1,2,3]+[4,5,6]) print(('hello','yes')+('ok','hi')) print('hello' * 3) print((1, 2, 3) * 3) print([1, 2, 3] * 3) print({1, 2, 3} - {3}) #result hello word [1, 2, 3, 4, 5, 6] ('hello', 'yes', 'ok', 'hi') hellohellohello (1, 2, 3, 1, 2, 3, 1, 2, 3) [1, 2, 3, 1, 2, 3, 1, 2, 3] {1, 2}
-
-
成员运算符 in:用于可迭代对象,比如字符串,元组,列表,字典,集合
注意:当in用于字典的时候,是判断是否是字典中的key值
person={'name':'zhangsan','age':18,'score':98} print('zhangsan ' in person) print('h' in 'hello') print('c' in ['a','b']) print(1 in (1,2)) #result False True False True
-
遍历
-
for…in 循环遍历:元组,列表,字典,集合
注意对字典的遍历,详细看字典那一节
for i in person: print(i) for i in person.keys(): print(i) for i in person.values(): print(i) for k,v in person.items(): print(k,v) #result name age score name age score zhangsan 18 98 name zhangsan age 18 score 98
-
带下标的遍历
-
使用while循环,然后打印下标
-
使用enumerate获取下标
num=[12,23,45,56,67,78,89] for i,k in enumerate(num): print(i,k) #result 0 12 1 23 2 45 3 56 4 67 5 78 6 89
-
-