x = True if x: print("Executing if") else: print("Executing else") print("Prints regardless of the if-else block") # Out: Executing if # Out: Prints regardless of the if-else block
使用比较运算符==也是可以的:
1 2 3 4 5 6 7 8
name = "阿斌" if name == "阿斌": print("给斌神🧎了") else: print("👋") print("你好呀") # Out: 给斌神🧎了 # Out: 你好呀
在python中,我们使用elif而不是else if:
1 2 3 4 5 6 7 8 9 10 11 12
age = 20
if age < 18: type = "🍚" elif age == 18: type = "🐟" elif age < 28: type = "🐧" else: type = "🐴" print("你是" + type) # 你是🐧
Serendipity = ["🍚", "🐟", "🐧", "🐴", "🆘", "☀️", "🥣"] for bro in Serendipity: print("Bro", bro, "has index", Serendipity.index(bro)) # Out: Bro 🍚 has index 0 # Out: Bro 🐟 has index 1 # Out: Bro 🐧 has index 2 # Out: Bro 🐴 has index 3 # Out: Bro 🆘 has index 4 # Out: Bro ☀️ has index 5 # Out: Bro 🥣 has index 6
如果我们想达到类似java中普通for循环的效果,可以使用for i in range(1, 10)的形式。其中,range中的前者是起始,后者是终止。也可以使用单数字形式,表示从 1 至所填数字(不包含),如:
n = 0 whileTrue: print("Executing while loop") if n == 5: break n = n + 1 print("Finished while loop") # Out: Executing while loop # Out: Executing while loop # Out: Executing while loop # Out: Executing while loop # Out: Executing while loop # Out: Executing while loop # Out: Finished while loop
defdescribe_pet(name, animal = "dog"): # animal的默认参数:dog print("I have a", animal + ".") print("Its name is", name + ".") describe_pet("阿斌", "penguin") describe_pet("🍚") describe_pet(animal = "horse", name = "🐴") # 顺序也可以是无所谓的 # Out: I have a penguin. # Out: Its name is 阿斌. # Out: I have a dog. # Out: Its name is 🍚. # Out: I have a horse. # Out: Its name is 🐴.
并且,我们还可以讲默认参数设置为None。配合if语句,可以实现当未传入该参数时不输出的效果:
1 2 3 4 5 6 7 8 9 10 11 12
defdescribe_pet(name, animal = None): # 默认参数为None if animal: print("I have a", animal + ".") print("Its name is", name + ".") describe_pet("阿斌", "penguin") describe_pet("🍚") describe_pet(animal = "horse", name = "🐴") # 顺序无所谓 # Out: I have a penguin. # Out: Its name is 阿斌. # Out: Its name is 🍚. # Out: I have a horse. # Out: Its name is 🐴.
在定义的函数中使用If-Else关键字:
1 2 3 4 5 6 7 8 9 10 11 12
defroundNum(num): remainder = num % 1 if remainder < 0.5: return num - remainder else: return num + (1 - remainder) print(roundNum(3.4)) print(roundNum(7.7)) print(roundNum(9.2)) # Out: 3.0 # Out: 8.0 # Out: 9.0
deflistFunc(my_list): maximum = max(my_list) minimum = min(my_list) first = my_list[0] last = my_list[-1] return maximum,minimum,first,last
l=[24, 12, 68, 40, 120, 96] params = listFunc(l) print(params) print("Max value is", params[0]) print("Min value is", params[1]) print("First value is", params[2]) print("Last value is", params[3])
# Out: (120, 12, 24, 96) # Out: Max value is 120 # Out: Min value is 12 # Out: First value is 24 # Out: Last value is 96
多种输出方式
python支持很多种将字符串与数字连接的输出方式:
1 2 3 4 5 6 7 8 9 10 11 12 13
pi = 3.14159 d = 12756 c = pi * d
print("Earth's diameter at equator: " + str(d) + " km. Equator's circumference: " + str(c) + " km.") print("Earth's diameter at equator:", d, "km. Equator's circumference:", c, "km.") print("Earth's diameter at equator: {:.1f} km. Equator's circumference: {:.1f} km.".format(d,c)) print(f"Earth's diameter at equator: {d} km. Equator's circumference: {c} km.")
# Out: Earth's diameter at equator: 12756 km. Equator's circumference: 40074.12204 km. # Out: Earth's diameter at equator: 12756 km. Equator's circumference: 40074.12204 km. # Out: Earth's diameter at equator: 12756.0 km. Equator's circumference: 40074.1 km. # Out: Earth's diameter at equator: 12756 km. Equator's circumference: 40074.12204 km.