python入门 第5章 条件判断和循环

1、Python之if语句:
Python代码的缩进规则。具有相同缩进的代码被视为代码块,4个空格,不要使用Tab

age = 20
if age >= 18:
    print 'your age is', age
    print 'adult'
print 'END'

2、Python之if-else:

score = 55
if score>=60:
    print 'passed'
else:
    print 'failed'

3、Python之if-elif-else:

score = 85

if score>=90:
    print 'excellent'
elif score>=80:
    print 'good'
elif score>=60:
    print 'passed'
else:
    print 'failed'

4、Python之 for循环:

L = [75, 92, 59, 68]
sum = 0.0
for var in L:
    sum=sum+var
print sum / 4

5、Python之 while循环:

sum = 0
x = 1
while x<100:
    sum=sum+x
    x=x+2
print sum

6、Python之 break退出循环:

sum = 0
x = 1
n = 1
while True:
    sum=sum+x
    x=x*2
    n=n+1
    if n>20:
        break
print sum

7、Python之 continue继续循环:

sum = 0
x = 0
while True:
    x = x + 1
    if x > 100:
        break
    if x%2==0:
        continue
    sum=sum+x
print sum

8、Python之 多重循环:

for x in [ 1,2,3,4,5,6,7,8,9 ]:
    for y in [  0, 1,2,3,4,5,6,7,8,9 ]:
       if x<y:
           num=10*x+y;
           print num

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值