“笨办法”学Python 3 ——练习24 更多练习

练习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. 字符串格式化的方法:
    (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
  1. 常用的转义字符:
    “”“…”“”:字符串格式不变:具体参考:https://blog.csdn.net/little_limin/article/details/127216493 练习9;
    其他转义字符,具体参考:https://blog.csdn.net/little_limin/article/details/127233547 练习10。
  2. 函数里的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. 代码注释见源代码中。

常见问题

1. 为什么给变量叫 jelly_beans 但是后面又用的是 beans 这个名字?
这是函数如何运行的一部分。记住,在函数内部变量是暂时的。当你返回它的时候,它可以被分配给一个变量以便之后使用。我只是创建了一个新的变量 beans 来保存返回的值。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
笨办法Python3》是一本教授Python编程基础的书籍,它分为七个部分,分别是打印与输入、文件操作、函数、数据容器与程序结构、面向对象的类、项目骨架与自动测试以及搭建简易的网站。这本书通过一系列的练习和示例,帮助读者逐步Python的基本语法和编程概念。 在第一部分《打印与输入》中,你将习如何使用print函数打印输出内容,以及如何使用input函数获取用户的输入。 在第二部分《文件操作》中,你将习如何打开、读取和写入文件,以及如何处理文件中的数据。 在第三部分《函数》中,你将习如何定义和调用函数,以及如何传递参数和返回值。 在第四部分《数据容器与程序结构》中,你将习如何使用列表、字典和元组等数据容器,以及如何使用条件语句和循环结构控制程序的执行流程。 在第五部分《面向对象的类》中,你将习面向对象编程的基本概念,包括类的定义、对象的创建和方法的调用。 在第六部分《项目骨架与自动测试》中,你将习如何使用项目骨架来组织你的代码,并习如何编写自动化测试来验证代码的正确性。 在第七部分《搭建简易的网站》中,你将习如何使用Python搭建一个简单的网站,并习一些与网站开发相关的知识。 通过《笨办法Python3》,你可以系统地Python编程的基础知识,并通过练习提升自己的编程能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值