温馨提示×

Python for 循环语句

发布时间:2020-10-17 18:45:46 来源:网络 阅读:200 作者:素颜猪 栏目:编程语言

语法:

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

    变量名;

说明:

for循环可以遍历任何序列的项目,如一个列表或者一个字符串

示例1:

#!/usr/bin/python

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


# 遍历字符串

for ch in 'hello':

print ch;


print 'the end';

代码截图1:

Python for 循环语句

运行截图1:

Python for 循环语句

示例2:

#!/usr/bin/python

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


# 定义列表

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


# 遍历列表

for fruit in fruits:

print fruit;


print 'the end';

代码截图2:

Python for 循环语句

运行截图2:

Python for 循环语句

示例3:

#!/usr/bin/python

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


# 定义列表

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


# 遍历列表,通过序列索引迭代

for index in range(len(fruits)):

print index,fruits[index];


print 'the end';

代码截图3:

Python for 循环语句

运行截图3:

Python for 循环语句

Python for 循环语句查看更多技术请移步:https://ui.29mn.com

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。