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

猜你喜欢

转载自blog.csdn.net/qq_40876291/article/details/82464019