Ex4:
注意浮点数的使用
Ex5:
1.变量名必须以字母开头;
2.如下所示变量在print时,注意加 f 和 {}
my_name = 'Lynn'
print(f"Let's talk about {my_name}.")
3.round() 返回浮点数的四舍五入值
Ex6:
变量赋值字符串的几种形式:
1.直接赋值为字符串
binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}."
不同于Ex5中的print,上述只需要print(y)即可;
但如果加在其他中,也需f和{}
print(f"I also said: '{y}'")
2.布尔值
hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious))
注:这里hilarious赋为数值、字符串、布尔值都可以print出来,记住这种print形式。
Ex7:
print("Its fleece was white as {}.".format('snow'))
注意时常练习break it
Ex8:
formatter = "{} {} {} {}"
print(formatter.format("one", "two", "three", "four"))
注意 .format() 这种形式
Ex9:
print("""
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
""")
print("""
"")
这种方式,中间可以插入无限行。
months = "Jan\nFen\nMar\nApr\nMay\nJun\nJul\nAug"
\n 为换行字符(new line character)
Ex10:
转义字符(escape sequences)
print("I am 6'2\" tall.") # escape double-quote inside string
print('I am 6\'2" tall.') # escape single-quote inside string
单双引号在字符串总应用时的转义。