一.if,else
if语句用于根据条件执行代码块
else语句可与if语句结合,当if判断为假时执行else语句
x=10
if x>5:
print("x大于5")
y=3
if y>5:
print("y大于5")
else:
print("y小于等于5")
结果:
二.while循环
while循环会在条件为真时反复执行,直到判断条件为假结束
count=0
while count<5:
print(count)
count+=1
num=0
while num<10:
num+=1
if num==3:
continue
if num==7:
break
print(num)
结果:
三.for循环
for循环用于遍历可迭代的对象中的元素
常见的有:列表,数组,字符串
fruits=["apple","banana","cheerry"]
for fruit in fruits:
print(fruit)
s="hello"
for c in s:
print(c)
for i in range(5):
print(i)
for j in range(2,7):
print(j)
for k in range(1,10,2):
print(k)