溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Python for 循環(huán)語(yǔ)句

發(fā)布時(shí)間:2020-10-17 18:45:46 來(lái)源:網(wǎng)絡(luò) 閱讀:230 作者:素顏豬 欄目:編程語(yǔ)言

語(yǔ)法:

for 變量名 in 字符串/列表:

    變量名;

說(shuō)明:

for循環(huán)可以遍歷任何序列的項(xiàng)目,如一個(gè)列表或者一個(gè)字符串

示例1:

#!/usr/bin/python

# -*- coding: UTF-8 -*-


# 遍歷字符串

for ch in 'hello':

print ch;


print 'the end';

代碼截圖1:

Python for 循環(huán)語(yǔ)句

運(yùn)行截圖1:

Python for 循環(huán)語(yǔ)句

示例2:

#!/usr/bin/python

# -*- coding: UTF-8 -*-


# 定義列表

fruits = ['apple','pear','banana'];


# 遍歷列表

for fruit in fruits:

print fruit;


print 'the end';

代碼截圖2:

Python for 循環(huán)語(yǔ)句

運(yùn)行截圖2:

Python for 循環(huán)語(yǔ)句

示例3:

#!/usr/bin/python

# -*- coding: UTF-8 -*-


# 定義列表

fruits = ['apple','pear','banana'];


# 遍歷列表,通過(guò)序列索引迭代

for index in range(len(fruits)):

print index,fruits[index];


print 'the end';

代碼截圖3:

Python for 循環(huán)語(yǔ)句

運(yùn)行截圖3:

Python for 循環(huán)語(yǔ)句

Python for 循環(huán)語(yǔ)句查看更多技術(shù)請(qǐng)移步:https://ui.29mn.com

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI