if循环:两种选择
例1:
1 a=42 2 if a<=10: 3 print(‘the number less than 10‘) 4 else: 5 print(‘thank you!‘)
例2:
while循环:重复某个功能多次
例1
1 greetings=1 2 while greetings<=3: #这里记住了是<=而不是<-,不要和R混淆了 3 print(‘hello!‘*greetings) 4 greetings=greetings+1
[[email protected] ~]# python 2.py hello! hello!hello! hello!hello!hello!
例2
for循环:遍历列表中的每一个元素
例1
1 names=[‘a‘,‘b‘,‘c‘] 2 for name in names: 3 print(‘hello!‘+name)
[[email protected] ~]# python 1.py hello!a hello!b hello!c
例2
1 n=10 2 for i in range(n): 3 print(i)
题1:a=100,b=200,求a,b之间所有奇数的和
时间: 2024-10-08 19:35:01