if 语句的判断条件可以用>(大于)、=(大于等于)、= 0 and num 10: # 判断值是否在小于0或大于10 print ('hello')else: print ('undefine')# 输出结果: undefine num = 8# 判断值是否在0~5或者10~15之间if (num >= 0 and num = 10 and num (大于)、 0: # 非双数时跳过输出 fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print ('当前水果 : %s' % fruits[index])
print ("Good bye!")
以上实例输出结果:
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye! print (i) # 输出双数2、4、6、8、10 i = 1while 1: # 循环条件为1必定成立 print (i) # 输出1~10 i += 1 if i > 10: # 当i大于10时跳出循环 for iterating_var in sequence:
statements(s)[/code]无限循环
如果条件判断语句永远为 true,循环将会无限的执行下去,如下实例:
while (flag): print ('Given flag is really true!')
print ("Good bye!")
复制代码
3.循环控制语句
3.1 for iterating_var in sequence:
statements(s) 语句
Python for iterating_var in sequence:
statements(s)语句,就像在C语言中,打破了最小封闭for或while循环。
for iterating_var in sequence:
statements(s)语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。
for iterating_var in sequence:
statements(s)语句用在while和for循环中。
如果您使用嵌套循环,for iterating_var in sequence:
statements(s)语句将停止执行最深层的循环,并开始执行下一行代码。
Python语言 for iterating_var in sequence:
statements(s) 语句语法:
for iterating_var in sequence:
statements(s)
复制代码
'''学习中遇到问题没人解答?小编创建了一个Python学习交流群:711312441寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!'''for letter in 'Python': # 第一个实例 if letter == 'h': for iterating_var in sequence:
statements(s) print ('当前字母 :', letter) var = 10 # 第二个实例while var > 0: print ('当前变量值 :', var) var = var -1 if var == 5: # 当变量 var 等于 5 时退出循环 for iterating_var in sequence:
3.3 for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s) 语句
Python for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s) 是空语句,是为了保持程序结构的完整性。
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s) 不做任何事情,一般用做占位语句。
Python 语言 for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s) 语句语法格式如下:
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
复制代码
实例
# 输出 Python 的每个字母for letter in 'Python': if letter == 'h': for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s) print ('这是 for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s) 块') print ('当前字母 :', letter) print ("Good bye!")以上实例执行结果:当前字母 : P当前字母 : y当前字母 : t这是 for iterating_var in sequence: