for x in range(5): print('hello')
numbers1 = [1,2,3,4,5] numbers2 = (6,7,2,5) name = "zhang" for x in numbers1: print(x) for x in numbers2: print(x) for x in name: print(x)
列表、元组、字典、集合、字符串等都可以用 for 来遍历
for x in [3,4,5,8,9]: if (x % 3 == 0): continue print(x)
continue 跳过本轮循环后面的语句,马上进入下一轮循环,仍然在 for 循环里面
numbers = [3,4,5,8,9] for x in numbers: if (x == 5): break print(x) print("程序结束了")
break 直接退出循环,而不是退出程序,仍然可以执行for循环之外的东西
实例1:一个for循环,从1 到100,如果是3的倍数输出’Y‘;如果是5的倍数输出’N‘;如果又是3的倍数又是5的倍数,输出’Hello‘;其他都直接输出数字自己
for x in range(1,101): if (x % 3 == 0): print('Y') elif (x % 5 == 0): print('N') elif (x % 3 == 0 & x % 5 == 0): print('Hello') else: print(x)
实例2:上面实例1 再增加一个条件:如果是7的倍数就什么都不做;如果碰到66就退出。
for x in range(1,101): if (x % 3 == 0 and x % 15 != 0): print('Y') if (x == 66): break elif (x % 5 == 0 and x % 15 != 0): print('N') elif (x % 15 == 0): print('Hello') elif (x % 7 == 0): continue else: print(x)
weeks = ['Monday','Tuesday','Friday'] for x in weeks: for n in x: print(n) print("-------")
weeks = ['Monday','Tuesday','Friday'] is_found = False for x in weeks: if (is_found): break for n in x: if(n == 'a'): is_found = True break print(n) print("-------")
scores = [98,97,95,86,23] for order,s in enumerate(scores): print(order+1,s)
enumerate 这个函数的基本应用就是用来遍历一个集合对象,它在遍历的同时还可以得到当前元素的索引位置。
import random class RandomCount: def __iter__(self): return self def __next__(self): return random.randint(1,10) rc = RandomCount() for s in rc: print(s)
for x in range(5): print('hello')
numbers1 = [1,2,3,4,5] numbers2 = (6,7,2,5) name = "zhang" for x in numbers1: print(x) for x in numbers2: print(x) for x in name: print(x)
列表、元组、字典、集合、字符串等都可以用 for 来遍历
for x in [3,4,5,8,9]: if (x % 3 == 0): continue print(x)
continue 跳过本轮循环后面的语句,马上进入下一轮循环,仍然在 for 循环里面
numbers = [3,4,5,8,9] for x in numbers: if (x == 5): break print(x) print("程序结束了")
break 直接退出循环,而不是退出程序,仍然可以执行for循环之外的东西
实例1:一个for循环,从1 到100,如果是3的倍数输出’Y‘;如果是5的倍数输出’N‘;如果又是3的倍数又是5的倍数,输出’Hello‘;其他都直接输出数字自己
for x in range(1,101): if (x % 3 == 0): print('Y') elif (x % 5 == 0): print('N') elif (x % 3 == 0 & x % 5 == 0): print('Hello') else: print(x)
实例2:上面实例1 再增加一个条件:如果是7的倍数就什么都不做;如果碰到66就退出。
for x in range(1,101): if (x % 3 == 0 and x % 15 != 0): print('Y') if (x == 66): break elif (x % 5 == 0 and x % 15 != 0): print('N') elif (x % 15 == 0): print('Hello') elif (x % 7 == 0): continue else: print(x)
weeks = ['Monday','Tuesday','Friday'] for x in weeks: for n in x: print(n) print("-------")
weeks = ['Monday','Tuesday','Friday'] is_found = False for x in weeks: if (is_found): break for n in x: if(n == 'a'): is_found = True break print(n) print("-------")
scores = [98,97,95,86,23] for order,s in enumerate(scores): print(order+1,s)
enumerate 这个函数的基本应用就是用来遍历一个集合对象,它在遍历的同时还可以得到当前元素的索引位置。
import random class RandomCount: def __iter__(self): return self def __next__(self): return random.randint(1,10) rc = RandomCount() for s in rc: print(s)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理