for 循环
# 字符串的for 循环 str1 = 'abc' for i in str1: print(i) ''' a b c ''' # 字典的for 循环 dict1 = {'name':'fqs','age':18,'address':'beijing'} for key in dict1: print(key) ''' name age address ''' for value in dict1.values(): print(value) ''' fqs 18 beijing ''' for item in dict1.items(): print(item) ''' ('name', 'fqs') ('age', 18) ('address', 'beijing') '''
# 使用循环 找list最大值 my_list=[1,2,3] temp_max=my_list[0] for index in range(len(my_list)-1): # 范围是下标0,1,最后一个2 不需要比较 if my_list[index]>my_list[index+1]: temp_max=my_list[index] else: temp_max = my_list[index+1] print(temp_max)