第六章第3讲:条件与循环(2)
1.断言
num1 = 20 assert num1 < 10
结果:
assert num1 < 10
AssertionError
2.while:默认条件为真
# while 默认条件为真 name = "" while not name: name = input("Please enter your name:") print("hello %s" % name) 结果: Please enter your name:Ann hello Ann
# while 默认条件为真 # while 判断条件 # 语句 # demo:计算1到100的和 n =100 sum = 0 counter = 1 while counter <=n: sum = sum + counter counter +=1 print("1到 %d 之和为:%d" % (n,sum)) 结果: 1到 100 之和为:5050
注意:使用while的时候,小心陷入无限循环
# while无限循环 var = 1 while var ==1: num = int(input("Please enter a number:")) print("The num is :") print("Goodbye!") 或 while True: num = int(input("Please enter a number:")) print("The num is :") print("Goodbye!")
while与else
count = 0 while count < 5: print(count,"小于5") count +=1 else: print(count,"大于5") 结果: 0 小于5 1 小于5 2 小于5 3 小于5 4 小于5 5 大于5
3.for语句
for语句的格式:
for <variable> in <sequences>:
<statements>
else:
<statements>
language = ["vb","c","c++","java","python"] for x in language: print(x) 结果: vb c c++ java python
4.range函数
range(上限,下限,步长):下限不包含
# range函数 for number in range(1,5): print(number) print("===================") # range函数的简写 for number in range(5): print(number) print("===================") # range函数的应用 for number in range(1,10,2): print(number) 结果: 1 2 3 4 =================== 0 1 2 3 4 =================== 1 3 5 7 9
5.并行迭代
# 并行迭代 names = ["Alice","Bela","Bob","Dan"] ages = [11,12,13,14] for i in range(len(names)): print(names[i],"is",ages[i],"years old.") 结果: Alice is 11 years old. Bela is 12 years old. Bob is 13 years old. Dan is 14 years old.
借助zip函数快速处理并行迭代
# zip函数处理并行迭代 names = ["Alice","Bela","Bob","Dan","Ann"] ages = [11,12,13,14] for name,ages in zip(names,ages): print(name,"is",ages,"years old.") 结果: Alice is 11 years old. Bela is 12 years old. Bob is 13 years old. Dan is 14 years old.
6.按索引迭代
# 按索引迭代 names = ["Alice","Bela","Bob","Dan","Ann"] index = 0 for name in names: if "ob" in name: names[index] = "Tom" index +=1 print(names) 结果: ['Alice', 'Bela', 'Tom', 'Dan', 'Ann']
7.continue:结束本次循环,进入下一个迭代循环
break:结束循环(终止循环)
# continue for i in range(5): print("========") print("i=",i) if i == 3: continue print("hello everyone!") 结果: ======== i= 0 hello everyone! ======== i= 1 hello everyone! ======== i= 2 hello everyone! ======== i= 3 ======== i= 4 hello everyone!
# break for i in range(5): print("========") print("i=", i) if i == 3: break print("hello everyone!") 结果: ======== i= 0 hello everyone! ======== i= 1 hello everyone! ======== i= 2 hello everyone! ======== i= 3
8.break与continue案例
案例1:
# break names = ["Alice","Bob","Bela","Dan","Ann"] for name in names: if name == "Bela": print("Hello Bela") break print("The student name is ",name) else: print("no student name is Bela") print("Done") 结果: The student name is Alice The student name is Bob Hello Bela Done
案例2:
# break
for letter in "python": if letter == "n": break print("The letter is ",letter) 结果: The letter is p The letter is y The letter is t The letter is h The letter is o
案例3:
# break var =10 while var >0: print("The value is ",var) var -=1 if var ==5: break print("Byebye!") 结果: The value is 10 The value is 9 The value is 8 The value is 7 The value is 6 Byebye!
continue案例
# continue for letter in "python": if letter == "o": continue print("The letter is ",letter) 结果: The letter is p The letter is y The letter is t The letter is h The letter is n