EX5: More Variables and Printing
-
例子:
my_name = 'Zed A. Shaw' my_age = 35 # not a lie my_height = 74 # inches my_weight = 180 # lbs my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown' print(f"Let's talk about {my_name}.") print(f"He's {my_height} centimeters tall.") print(f"He's {my_weight} kilograms heavy.") print(f"Actually that's not too heavy.") print(f"He's got {my_eyes} eyes and {my_hair} hair.") print(f"His teeth are usually {my_teeth} depending on the coffee.") # this line is tricky, try to get it exactly right total = my_age + my_height + my_weight print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")
-
学习内容:
f
;+
;round()
-
深入练习(Study Drills)
-
去掉变量名中的
my_
,将数据转化为厘米和千克,并取整输入:
name = 'Zed A. Shaw' age = 35 # not a lie height = 2.54 * 74 # inches weight = 0.4535924 * 180 # lbs eyes = 'Blue' teeth = 'White' hair = 'Brown' print(f"Let's talk about {name}.") print(f"He's {round(height)} centimeters tall.") print(f"He's {round(weight)} kilograms heavy.") print(f"Actually that's not too heavy.") print(f"He's got {eyes} eyes and {hair} hair.") print(f"His teeth are usually {teeth} depending on the coffee.") # this line is tricky, try to get it exactly right total = age + height + weight print(f"If I add {age}, {height}, and {weight} I get {total}.")
结果:
Let's talk about Zed A. Shaw. He's 188 centimeters tall. He's 82 kilograms heavy. Actually that's not too heavy. He's got Blue eyes and Brown hair. His teeth are usually White depending on the coffee. If I add 35, 187.96, and 81.646632 I get 304.606632.
round(height)
会对height
的数值四舍五入取整,188height
直接输出的为计算结果值,187.96
-
-
改变代码(寻找可能情况及错误)
-
f
嵌套输入:
my_age = 35 my_height = 74 my_weight = 180 print(f"{my_height},f"{my_age}"")
结果:
PS F:\python\hard way 练习> python ex5.py File "ex5.py", line 24 print(f"{my_height},f"{my_age}"") ^ SyntaxError: invalid syntax
语法错误:无效语法。
-
+
连接输入:
my_name = 'zed' my_eyes = 'blue' my_height = 74 my_weight = 180 print(f"{my_height + my_weight}") print(f"{my_height} + {my_weight}") print(f"{my_height}" + f"{my_weight}") print(f"{my_name + my_eyes}") print(f"{my_name} + {my_eyes}")
结果:
254 74 + 180 74180 zedblue zed + blue
结果中,
第一行,两个数字类型的变量会做加法运算,
+
为加号的意思。第二行,两个数字变量在两个大括号中,在
f-string
的作用下会引用变量值并和加号一起输出,+
为字符串,没有加和或者连接的作用。第三行,通过两个
f-string
的作用,两个数字变量分别变成字符串类型,+
为连接作用,连接两个字符串。第四行,在这里,
+
为连接作用,连接两个字符串。第五行,
+
作为字符串输出,不起任何作用。
-
我的GitHub中有详细的代码下载:https://github.com/YukyCookie/learn-python-three-the-hard-way.git