国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

詳解Python 循環(huán)嵌套

瀏覽:3日期:2022-07-18 10:01:34

Python 語言允許在一個(gè)循環(huán)體里面嵌入另一個(gè)循環(huán)。

Python for 循環(huán)嵌套語法:

for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s)

Python while 循環(huán)嵌套語法:

while expression: while expression: statement(s) statement(s)

你可以在循環(huán)體內(nèi)嵌入其他的循環(huán)體,如在while循環(huán)中可以嵌入for循環(huán), 反之,你可以在for循環(huán)中嵌入while循環(huán)。

實(shí)例:

以下實(shí)例使用了while循環(huán)嵌套輸出2~100之間的素?cái)?shù):

#!/usr/bin/python# -*- coding: UTF-8 -*- i = 2while(i < 100): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print i, ' 是素?cái)?shù)' i = i + 1 print 'Good bye!'

以上實(shí)例輸出結(jié)果:

2 是素?cái)?shù)3 是素?cái)?shù)5 是素?cái)?shù)7 是素?cái)?shù)11 是素?cái)?shù)13 是素?cái)?shù)17 是素?cái)?shù)19 是素?cái)?shù)23 是素?cái)?shù)29 是素?cái)?shù)31 是素?cái)?shù)37 是素?cái)?shù)41 是素?cái)?shù)43 是素?cái)?shù)47 是素?cái)?shù)53 是素?cái)?shù)59 是素?cái)?shù)61 是素?cái)?shù)67 是素?cái)?shù)71 是素?cái)?shù)73 是素?cái)?shù)79 是素?cái)?shù)83 是素?cái)?shù)89 是素?cái)?shù)97 是素?cái)?shù)Good bye!

使用for循環(huán)嵌套來獲取100以內(nèi)的素?cái)?shù)

#!/usr/bin/python# -*- coding: UTF-8 -*-num=[];i=2for i in range(2,100): j=2 for j in range(2,i): if(i%j==0): break else: num.append(i)print(num)

輸出結(jié)果

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

以上就是詳解Python 循環(huán)嵌套的詳細(xì)內(nèi)容,更多關(guān)于Python 循環(huán)嵌套的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 荃湾区| 塔河县| 江津市| 南昌市| 莱西市| 翁牛特旗| 玉树县| 丹东市| 阿拉善盟| 理塘县| 福泉市| 色达县| 工布江达县| 巩留县| 区。| 昭觉县| 明光市| 陵川县| 镇巴县| 象山县| 类乌齐县| 内乡县| 吴忠市| 车致| 襄垣县| 呼图壁县| 土默特左旗| 阿合奇县| 宝鸡市| 邹城市| 托克逊县| 浦县| 龙州县| 武川县| 山西省| 五常市| 恩平市| SHOW| 弋阳县| 和政县| 许昌县|