目录
1、条件语句(if、elif、else)
成绩等级判断:
grade = int(input("请输入你的成绩:"))
if grade >= 90:
print("成绩优秀!")
elif grade >= 80:
print("成绩良好!")
elif grade >= 60:
print("成绩及格!")
else:
print("成绩不及格!")
if循环嵌套:
num = input("请输入一个数字:")
if int(num) >= 18:
if num >= 200:
print("数据不太真实")
else:
print("数据真实")
else:
print("数据不对")
2、循环语句(while、while...else...)
1-100奇数和
i = 0
sum = 0
while i <= 100:
if i%2 != 0:
sum += i
i +=1
print(sum)
死循环
while True:
num = int(input("请输入一个数字:"))
print("你输入的数字是:", num)
while...else
age = 18
while age < 18:
print("你还没有成年,不能上网吧!")
else:
print("你已经成年了,可以进网吧")
打印九九乘法表
# 定义起始行
row = 1
# 最大打印9行
while row <=9:
# 定义起始列
col = 1
# 最大打印row列
while col <= row:
print("%d * %d = %d" % (col, row, row * col), end="\t")
col += 1
print()
row += 1
3、for、for...else
for循环可以遍历任何可迭代对象,如列表,字符串
list = ['小明','18', '武汉']
for i in list:
print(i)
str = 'abcdef'
for i in str:
print(i, end=' ')
print()
tuple = (1, 2, 'aa', 'FF')
for i in tuple:
print(i)
else:
print("遍历结束!")
range()函数
for i in range(5):
print(i)
# 1-10步长为3
for i in range(1, 10, 3):
print(i)
4、break、continue
break跳出循环,后面不再执行;continue跳出当前次循环,从下一次循环开始执行
for循环案例
for i in range(1, 10):
if i%2 == 0:
break
print(i)
print('*'*20)
for i in range(1, 10):
if i%2==0:
continue
print(i)
while循环案例
i = 3
while i > 0:
i -= 1
if i == 1:
break
print(i)
i = 3
while i > 0:
i -= 1
if i == 2:
continue
print(i)