练习24 源代码
print("Let's practice everything.") #打印
print('You\'d need to know \'bout escapes with \\ that do:') #转义字符\,\'为',\\为\
print('\n newlines and \t tab.') #转义字符\n,换行;\t,空格等于tab键
#字符串打印,格式不变,其中转义字符\t,\n用法同上
poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""
print("---------------------")
print(poem)
print("---------------------")
five = 10 - 2 + 3 - 6
print(f"This should be five: {five}") #使用f""将变量格式化字符串
#创建函数,started为参数
def secret_formula(started):
jelly_beans = started * 500 #乘法
jars = jelly_beans / 1000 #除法
crates = jars / 100
return jelly_beans, jars, crates #return使用,结果返回这三个变量
start_point = 10000
beans, jars, crates = secret_formula(start_point) #调用函数,输出结果按顺序对应的变量
# remember that this is another way to format a string
#{},字符串.format(变量)的方式将变量格式化为字符串
print("With a starting point of : {}".format(start_point))
# it's just like with an f"" string
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
start_point = start_point / 10
print("We can also do that this way:")
formula = secret_formula(start_point) #调用函数,结果输出为formula变量
print(formula) #输出为元组,tuple
#this is an easy way to apply a list to a format string
# format函数,格式化列表和元组时,使用*对列表和元组拆分,使用**对字典拆分。
print("We'd have {} beans, {} jars, and {} crates.".format(*formula))
输出结果
Let's practice everything.
You'd need to know 'bout escapes with \ that do:
newlines and tab.
---------------------
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explanation
where there is none.
---------------------
This should be five: 5
With a starting point of : 10000
We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
We can also do that this way:
(500000.0, 500.0, 5.0)
We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.
知识点:
- 字符串格式化的方法:
(1)f " {变量}" ,具体参考:https://blog.csdn.net/little_limin/article/details/127197911 练习6;
(2)“{}字符串”.format(变量or字符串),具体参考:https://blog.csdn.net/little_limin/article/details/127198953 练习7 或者 https://blog.csdn.net/little_limin/article/details/127215542 练习8
(3)format函数,格式化列表和元组时,使用*对列表和元组拆分,使用**对字典拆分。
具体可参考:https://blog.csdn.net/qq_42855293/article/details/118480087 python中format的用法详解 其中的“6. list、tuple的拆分”,具体如下:
在format格式化时,可使用* 或者 ** 进行对list、tuple拆分。
1 foods = ['fish', 'beef', 'fruit']
2 s = 'i like eat {} and {} and {}'.format(*foods)
3 # i like eat fish and beef and fruit
4 print(s)
5 foods = ['fish', 'beef', 'fruit']
6 s = 'i like eat {2} and {0} and {1}'.format(*foods)
7 # i like eat fruit and fish and beef
8 print(s)
9 dict_temp = {'name': 'Lily', 'age': 18}
10 # 字典需要用 ** 进行拆分
11 s = 'My name is {name}, i am {age} years old'.format(**dict_temp)
12 print(s) # My name is Lily, i am 18 years old
————————————————
版权声明:本文为CSDN博主「月夜风雨磊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42855293/article/details/118480087
- 常用的转义字符:
“”“…”“”:字符串格式不变:具体参考:https://blog.csdn.net/little_limin/article/details/127216493 练习9;
其他转义字符,具体参考:https://blog.csdn.net/little_limin/article/details/127233547 练习10。 - 函数里的return可以返回一组tuple给脚本的变量
例如:
def secret_formula(started):
jelly_beans = started * 500 #乘法
jars = jelly_beans / 1000 #除法
crates = jars / 100
return jelly_beans, jars, crates #return使用,结果返回这三个变量
4.函数里的变量是临时的
原因见常见问题1,同一个值的变量名称可以是不一样的,同一个返回值可以被保存在别的变量名下。
附加练习
- 代码注释见源代码中。
常见问题
1. 为什么给变量叫 jelly_beans 但是后面又用的是 beans 这个名字?
这是函数如何运行的一部分。记住,在函数内部变量是暂时的。当你返回它的时候,它可以被分配给一个变量以便之后使用。我只是创建了一个新的变量 beans 来保存返回的值。