python判断和循环
python判断
a = 2
b = 1
if a > b:
print(a)
else:
print(b)
多个if语句
a = 2
b = 1
if a > b:
print(a)
elif a == b:
print(b)
elif a < b:
print(a)
三元表达式
print(a if a > b else b) #打印a如果a大于b的话,否则打印b
循环
有2种方式,for x in list,tutle,另一种是while循环
打印1到100的整数和
sum = 0
for i in range(1, 101):
sum += i
print(sum)
sum = 0
i = 0
while i < 101:
sum += i
i += 1
print(sum)
break和continue,break是跳出当前循环,而continue是跳过本次循环,开启下一次循环
sum = 0
i = 0
while i < 101:
if i % 2 == 0: #跳过偶数,求出奇数和
i += 1
continue
else:
sum += i
i += 1
print(sum)
sum = 0
i = 0
while i < 101:
if i % 2 == 0:
i += 1
break
else:
sum += i
i += 1
print("i的值为:", i) #最后i的值是1
print(sum)
当前标题:python判断和循环
转载来于:http://pwwzsj.com/article/jpgoge.html