python之循环

python之if语句

 

score = 75
if score>=60:
    print 'passed'

注意缩进

 

if else

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

if else if

score = 85

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

 

python 中for循环

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

 

 

python中while循环

利用while循环计算100以内奇数的和。

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

while之break退出

利用 while True 无限循环配合 break 语句,计算 1 + 2 + 4 + 8 + 16 + ... 的前20项的和。

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

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

 

python之双重循环

对100以内的两位数,请使用一个两重循环打印出所有十位数数字比个位数数字小的数,例如,23(2 < 3)。

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:
            print x*10+y

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值