python编程从入门到实践(第2版)第二章练习题解答

2021/10/30 17:09:51

本文主要是介绍python编程从入门到实践(第2版)第二章练习题解答,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • 一、第二章练习题解答
    • 练习2-1
    • 练习2-2
    • 练习2-3
    • 练习2-4
    • 练习2-5
    • 练习2-6
    • 练习2-7
    • 练习2-8
    • 练习2-9
    • 练习2-10
    • 练习2-11
  • 小结

一、第二章练习题解答

练习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.
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.
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章,你将学习如何在被称为列表 的变量中存储一系列信息, 以及
如何通过遍历列表来操作其中的信息。



这篇关于python编程从入门到实践(第2版)第二章练习题解答的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程