温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python 循环:for()、while()

发布时间:2020-08-04 08:37:28 来源:网络 阅读:394 作者:虎皮喵的喵 栏目:编程语言

格式:

    for 变量 in 列表:

    while 表达式:


一、for循环

#!/usr/bin/python

#for [0..5]
sum = 0;  #当我没有初始化sum时,会提示TypeError: unsupported operand type(s) for +:

                #'builtin_function_or_method' and 'int' 

for x in [0, 1, 2, 3, 4, 5]:
    sum = sum + x;
    print x, sum;
print sum;

python 循环:for()、while()


#for [0..5]
sum = 0;
for x in range(6):
    sum += x;
    print x, sum;
print sum;

python 循环:for()、while()

python 循环:for()、while()


#for list/tuple
list = ['a', 'b', 'c', 'you'];
for list in list:
     print list;

python 循环:for()、while()


二、while循环

#!/usr/bin/python

n = 9
while n > 0:
    print n;
    n -= 1;  #python不支持 ++/--操作

python 循环:for()、while()


向AI问一下细节

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

AI