1. 判断
示例:
age = 3
if age >= 18:
print('adult')
elif age >= 6:
print('teenager')
else:
print('kid')
在执行判断后又冒号:,缩进的内容就是判断结果为true时应执行的代码段
elif=else if
if判断条件还可以简写,比如写:
if x:
print('True')
只要x是非零数值、非空字符串、非空list等,就判断为True,否则为False。
2. 循环
- for...in :依次把list或tuple中的每个元素迭代出来
names = ['Michael', 'Bob', 'Tracy']
for name in names:
print(name)
sum = 0
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
sum = sum + x
print(sum)
range(n):生成的序列是从0开始小于n的整数
range(101):0-100的数列
s = 0
for x in list(range(101))
s = s+x
- while
n = 0
while n <= 10:
n = n + 1
if n % 2 != 0:
continue
print(n)
print('END')