age = 20
if age > 18:
print("我不是小孩子")
elif age < 18:
print("你永远都是小孩子")
else:
print("你永远都是小孩子")
2、while循环语句:语法格式:
age1 = 30
while age1 < 50:
age1 = age1 + 10
print(age1)
# 需求:统计1-10的和
sum = 0
i = 0
while i < 10:
i = i + 1
sum = sum + i
print("1-10的和是{}".format(sum))
3、for循环:语法格式:
a = [1, 2, 3, 4, 5, 6, 7, 8]
# 遍历列表中的数字:
for i in a:
print(i)
# 需求:打印九九乘法口诀表
# range也是含头不含尾部的:
for x in range(1, 10):
for y in range(1, x + 1):
print(f"{y} * {x} = {x * y}", end="\t")
print("")
4、列表推导式:一般是使用在循环中:主要的语法格式:
# 计算需求:
l = [1, 2, 3, 4, 5, 6, 7, 8]
l1 = []
for i in l:
l1.append(i + 1)
print(l)
print(l1)
#使用列表推导式:
l2 = [ i+1 for i in l]
print(l2)
#使用列表推导式将l中的元素变成str类型:
l3 = [str(i) for i in l]
print(l3 ,type(l3[1]))
5、函数:enumreate函数:带着下标进行循环:
words = ["java","hadoop","flink","spark"]
for index,word in enumerate(words):
print(index,word)