Python编程从入门到实践答案1


这本书是我自学Python的第一本书,里面的习题都在寒假已经完成,由于当时博客尚未搭建,故里面题目的答案并未发表。现在在开发项目博客没有新内容更新,因此这段实践会陆续上传原来的习题答案。每个题目均为自己设计的代码,优化与重构方面与标准答案可能有所不同。

​ 参考教材:《Python编程从入门到实践》 【美】 Eric Matthes 著

Chapter1

2-1 简单消息:将一条信息存储到变量中,再将其打印出来。

name = 'chen xili'
print(name.title())

2-2 多条简单消息:将一条信息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。

name = "Chen"
print(name)

name = "Xili"
print(name)

2-3 个性化消息:将用户的姓名存到一个变量中,并向该用户显示一条信息。显示的消息应非常简单,如”Hello Eric,would you like to learn some Python today?”。

name = "eric"
print("Hello {},would you like to learn\
 some Python today?".format(name.title()))

2-4 调整名字的大小写:将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人名。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
name = 'chen xili'
print(name.lower())  # 打印小写
print(name.upper())  # 打印大写
print(name.title())  # 打印首字母大写

2-5 名言:找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):

Albert Einstein once said, “A person who never made a mistake never tried anything new.”

#!/usr/bin/env python
# -*- coding:utf-8 -*-
print('Albert Einstein once said, "A person who never '
      'made a mistake never tried anything new."')

2-6 名言2: 重复练习2-5,但将名人的姓名存储在变量famous_person中,再创建要显示的信息,并将存储在变量message中,然后打印这条消息。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
famous_person = 'Albert Einstein'
message = famous_person + ' once said, "A person who never made ' \
                          'a mistake never tried anything new."'
print(message)

2-7 剔除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合”\t”和”\n”各一次。

​ 打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip()、rstrip()和strip()对人名进行处理,并将结果打印出来。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
name = "\tChen xili\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:编写4个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是数字8.为使用print语句来显示结果,务必将这些表达式用括号括起来,也就是说,你应该编写4行类似于下面的代码:

print(5 + 3)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
print(5 + 3)    # 加法运算
print(10 - 2)   # 减法运算
print(2 * 4)    # 乘法运算
print(16 // 2)  # 除法运算

2-9 最喜欢的数字:将你最喜欢的数字存储在一个变量中,再使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条消息打印出来。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
favorite_num = 259
message = "My favorite number is " + str(favorite_num) + "."

print(message)

2-10 添加注释:选择你编写的两个程序,在每个程序中都至少添加一条注释。如过程序太简单,是在没有什么需要说明的,就在程序文件开口加上你的姓名和当前日期,再用一句话阐述程序的功能。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
famous_person = 'Albert Einstein'
# 将内容合并
message = famous_person + ' once said, "A person who never made ' \
                          'a mistake never tried anything new."'
print(message)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 获取喜欢的数字
favorite_num = 259
message = "My favorite number is " + str(favorite_num) + "."

print(message)

2-11 Python之禅:在Python终端会话执行命令import this,并粗略地浏览一下其他的指导原则。

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!

文章作者: 陈细利
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 陈细利 !
评论
 上一篇
Python项目1 Python项目1
项目一——外星人入侵​ 这个项目是开发一个小游戏。第一部分,先创建一艘能够根据用户输入而左右移动和设计的飞船,再创建一群作为射击目标的外星人。第二部分,让这群外星人向两边和下面移动,并删除被子弹击中的外星人,并显示玩家拥有的飞
2020-07-12
下一篇 
Python第二次上机实习 Python第二次上机实习
1.用户任意输入某年某月某日,判断这一天是这一年的第几天? #!/usr/bin/env python # -*- coding:utf-8 -*- # 先让用户分别输入年月日 year = int(input("年:")) mouth
  目录