您好,登錄后才能下訂單哦!
語(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:
運(yùn)行截圖1:
示例2:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 定義列表
fruits = ['apple','pear','banana'];
# 遍歷列表
for fruit in fruits:
print fruit;
print 'the end';
代碼截圖2:
運(yùn)行截圖2:
示例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:
運(yùn)行截圖3:
查看更多技術(shù)請(qǐng)移步:https://ui.29mn.com
免責(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)容。