1. 环境介绍
我使用的环境是windows/notepad++/Python 3.7.2
2. 代码
# coding:utf-8
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'
'''
python2
print"Let's talk about %s." % my_name
'''
# python3
print(f"Let's talk about {my_name}.")
print (f"He's {my_height} inches tall.")
print (f"He's {my_weight} pounds heavy.")
print ("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} deppending 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}.")
3.输出

4.附加练习
练习1:将代码中的my_去掉
# coding:utf-8
# 习题1:将my_去掉
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
print(f"Let's talk about {name}.")
print (f"He's {height} inches tall.")
print (f"He's {weight} pounds heavy.")
print ("Actually that's not too heavy.")
print (f"He's got {eyes} eyes and {hair} hair.")
print (f"His teeth are usually {teeth} deppending 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}.")
5.总结
- python2和python3中不同的地方展现出来了,敲完代码后我感觉Python3的逻辑相较于Python2要更好一些。不知道是否是我个人的感受。更加合理了。
- 变量嵌入字符串中使用中括号{}括起来
- Python3中格式化字符串需要用f进行表示
- 格式化字符串的意义就是将变量放到文字中
网友评论