Python编程从入门到实践 -----第2章:变量和简单数据类型(课后习题答案)

本博客通过一系列Python编程实例,如消息存储与打印、名字显示、调整大小写、引用名人名言、处理空白字符、数学运算及注释添加等,展示了Python编程的基础技巧。同时介绍了如何在Python中使用变量、字符串操作、格式化输出以及基本的算术运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

py = “hello,你好吗?”
print(py)
输出:hello,你好吗?

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

py = “hello,你好吗?”
print(py)
输出:hello,你好吗?
you = “python”
print(you)
输出:python

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

  1. name = “chen”
  2. print("Hello " + name + “, would you like to learn some Python today?”)

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

  1. name = “nico”
  2. #字母全部大写
  3. print(name.upper())
  4. #字母全部小写
  5. print(name.lower())
  6. #首字母大写
  7. print(name.title())

2-5 名言: 找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):Albert Einstein once said, “A person who never made a mistake never tried anything new.”

  1. name = “罗曼・罗兰:”
  2. said =’“活最沉重的负担不是工作,而是无聊。”’
  3. print(name + said)

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

  1. famous_person = “罗曼・罗兰:”
  2. message = ‘“活最沉重的负担不是工作,而是无聊。”’
  3. messgaes = (famous_person + message)
  4. print(messgaes)

2-7 剔除人名中的空白: 存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t" 和"\n" 各一次。打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip() 、rstrip() 和strip() 对人名进行处理,并将结果打印出来。

  1. name = “\tEric Matthes\n”
  2. print(“Unmodified:” )
  3. print(name)
  4. #删除前端空格
  5. print("\nUsing lstrip()?
  6. print(name.lstrip())
  7. #删除末尾空格
  8. print("\nUsing rstrip():" )
  9. print(name.rstrip())
  10. #删除两端空格
  11. print("\nUsing strip():" )
  12. print(name.strip())

输出:
Unmodified:
Eric Matthes

Using lstrip():
Eric Matthes

Using rstrip():
Eric Matthes

Using strip():
Eric Matthes

2-8 数字8: 编写4个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是数字8。为使用print 语句来显示结果,务必将这些表达式用括号括起来,也就是说,你应该编写4行类似于print(5 + 3)的代码:

  1. print(4 + 4)
  2. print(8 - 0)
  3. print(2 * 4)
  4. print(16 / 2)

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

  1. fav_num = 295
  2. msg = ("My favorite number is " + str(fav_num) + “.”)
  3. print(msg)

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

  1. fav_num = 295
  2. #单行注解:我最喜欢的数字是
  3. msg = ("My favorite number is " + str(fav_num) + “.”)
  4. print(msg)

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

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值