python编程从⼊门到实践(第2版)第⼆章练习题解答
undermine同义词⽂章⽬录
⼀、第⼆章练习题解答
练习2-1
简单消息 将⼀条消息赋给变量, 并将其打印出来。
msg ="I love learning to use Pythoy"
网页滑条代码print(msg)
练习2-2
多条简单消息 将⼀条消息赋给变量, 并将其打印出来; 再将变量的值修改为⼀条新消息, 并将其打印出来。
msg ="I love learning to use Pythoy"
print(msg)
msg ="It's really satisfying!"
print(msg)
练习2-3
个性化消息 ⽤变量表⽰⼀个⼈的名字, 并向其显⽰⼀条消息。 显⽰的消息应⾮常简单, 下⾯是⼀个例⼦。 Hello Eric, would you like to learn some Python today?
wangming ="Hello Eric,would you like to learn some Python today?"
print(wangming)
练习2-4
调整名字的⼤⼩写 ⽤变量表⽰⼀个⼈的名字, 再以⼩写、 ⼤写和⾸字母⼤写的⽅式显⽰这个⼈名。
name ="wang ming"
print(name.title())
print(name.upper())
print(name.lower())
练习2-5
名⾔ ⼀句你钦佩的名⼈说的名⾔, 将其姓名和名⾔打印出来。 输出应类似于下⾯这样(包括引号)。
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
print('Albert Einstein once said, "A person who never made a mistake')
print('never tried anything new."')
练习2-6
名⾔2 重复练习2-5,但⽤变量famous_person 表⽰名⼈的姓名,再创建要显⽰的消息并将其赋给变量message,然后打印这条消息。
famous_person ="--孔⼦"
message ="三⼈⾏,必有我师焉,择其善者⽽从之,其不善者⽽改之。"
print(message ,"\n\t",famous_person)
输出:
三⼈⾏,必有我师焉,择其善者⽽从之,其不善者⽽改之。
--孔⼦
练习2-7
剔除⼈名中的空⽩ ⽤变量表⽰⼀个⼈的名字, 并在其开头和末尾都包含⼀些空⽩字符。务必⾄少使⽤字符组合"\t"和"\n" 各⼀次。
打印这个⼈名,显⽰其开头和末尾的空⽩。然后,分别使⽤剔除函数lstrip() 、rstrip() 和strip()对⼈名进⾏处理,并将结果打印出来。
name ="\tEric Matthes\n"
print("Unmodified:")
print(name)
print("\nUsing lstrip():")
print(name.lstrip())
print("\nUsing rstrip():")
print(name.rstrip())
print("\nUsing strip():")
print(name.strip())
练习2-8
数字8 编写四个表达式,分别使⽤加法、减法、乘法和除法运算, 但结果都是数字8。为使⽤函数调⽤print() 来显⽰结果, 务必将这些表达式⽤圆括号括起来。 也就是说, 你应该编写四⾏类似于下⾯的代码:print(5+3)
numbel =(5+3,16-8,2*4,16//2)
print(numbel)
练习2-9
最喜欢的数 ⽤⼀个变量来表⽰你最喜欢的数, 再使⽤这个变量创建⼀条消息,指出你最喜欢的数是什么, 然后将这条消息打印出来。favorite_number ="888"#喜欢的(favorite)
message ="My favorite number is"
print(f"{message}{favorite_number}")
练习2-10
添加注释 选择你编写的两个程序, 在每个程序中⾄少添加⼀条注释。如果程序太简单,实在没有什么需要 说明的,就在程序⽂件开头加上你的姓名和当前⽇期,再⽤⼀句话阐述程序的功能
'''
name
2021年10⽉30⽇
'''
name ="\tEric Matthes\n"
print("Unmodified:")
print(name)
print("\nUsing lstrip():")#剔除左边空⽩
print(name.lstrip())
print("\nUsing rstrip():")#剔除右边空⽩
print(name.rstrip())
print("\nUsing strip():")#剔除两边空⽩
二进制转换成十进制原理print(name.strip())
练习2-11
Python之禅 在Python终端会话中执⾏命令import this,并粗略地浏览⼀下其他的指导原则。
注意:python终端。
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
python基础代码练习Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
java中lastindexof用法Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
人工智能训练师培训课程
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one--and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
⼩结
在本章中,你学习了:如何使⽤变量;如何创建描述性变量名以及如何消除名称错误和语法错误; 字符串是什么,以及如何使⽤⼩写、⼤写和⾸字母⼤写⽅式显⽰字符串; 使⽤空⽩来显⽰整洁的输出, 以及如何剔除字符串中多余的空⽩; 如何使⽤整数和浮点数; ⼀些使⽤数值数据的⽅式。你还学习了如何编写说明性注释, 让代码对你和其他⼈来说更容易理解。 最后, 你了解了让代码尽可能简单的理念。
在第3章,你将学习如何在被称为列表 的变量中存储⼀系列信息, 以及如何通过遍历列表来操作其中的信息。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。