a = 111
if a < 10:
if a == 1:
print(1)
elif a == 2:
print(2)
else:
print(3456789)
elif a == 10:
print(10)
else:
print(11)
2.循环
while循环
# while循环
a = 1
while a < 10:
print(a)
a += 2
# 输出13579
# 无限循环
while True:
inp = input()
print("输入的是", inp)
# while 后面的条件语句为 false 时,则执行 else 的语句块
a = 0
while a < 10:
print(a)
a += 3
else:
print("end")
for循环
# for 循环可以遍历任何可迭代对象
list1 = [1, 2, 3, 4, 5, 6]
for i in list1:
print(i)
else:
print("end")
range函数
# range()函数遍历数字
for i in range(5):
print(i, end=' ') # 0 1 2 3 4
# 指定区间
for i in range(3, 9):
print(i, end=' ') # 3 4 5 6 7 8
# 指定增量
for i in range(3, 9, 2):
print(i, end=' ') # 3 5 7
# 负数
for i in range(-1, -10, -2):
print(i, end=' ') # -1 -3 -5 -7 -9
# 创建一个列表
list1 = list(range(5))
print(list1) # [0, 1, 2, 3, 4]
break与continue语句
# break语句跳出当前循环体
n = 1
while n < 5:
n += 1
if n == 3:
break
print(n, end=' ') # 2
# continue语句跳过当前循环块中的剩余语句,然后继续进行下一轮循环
m = 1
while m < 5:
m += 1
if m == 3:
continue
print(m, end=' ') # 2 4 5
pass语句
# pass是空语句,是为了保持程序结构的完整性
for i in range(5):
if i == 2:
pass
print(i, end=' ') # 0 1 2 3 4