myprofile ={'name':'lalala','age':30}# 如果前面值为假的话则不进行后面判断(不会出现没有'age'报错的情况)if'age'in myprofile and myprofile['age']<=40:print("40岁以下")
组合运算符
x =15if10<= x <=20:print("x在10~20之间")
循环列表
mylist =[3,6,9,12]# 循环1for val in mylist:print(val)# 循环2for index inrange(len(mylist)):print(index, mylist[index])# 循环3for index,val inenumerate(mylist):print(index, val)# 倒序循环1for val inreversed(mylist):print(val)# 倒序循环2for index, val inenumerate(reversed(mylist)):print(index, val)
循环字典
mydict ={'name':'lalala','age':30,'sex':'男'}# 循环1for key in mydict:print(key, mydict[key])# 循环2for key, val in mydict.items():print(key, val)