第二章
练习2-1: 简单消息 将一条消息赋给变量,并将其打印出来。
msg = "Hello Python World!"
print(msg)
输出:
Hello Python World!
练习2-2: 多条简单消息 将一条消息赋给变量,并将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。
msg = "How are you?"
print(msg)
msg = "I'm fine, Thank you."
print(msg)
输出:
How are you?
I'm fine, Thank you.
练习2-3: 个性化消息用变量表示一个人的名字,并向其显示一条消息。显示的消息应非常简单,下面是一个例子。
Hello Eric, would you like to learn some Python today?
name = "Eric"
message = f"Hello {name}, would you like to learn some Python today?"
print(message)
输出:
Hello Eric, would you like to learn some Python today?
练习2-4: 调整名字的大小写 用变量表示一个人的名字,再以小写、大写和首字母大写的方式显示这个人名。
name = "aimi"
print(name)
print(name.lower())
print(name.upper())
print(name.title())
输出:
aimi
aimi
AIMI
Aimi
练习2-5: 名言 找一句你钦佩的名人说的名言,将其姓名和名言打印出来。输出应类似于下面这样(包括引号)。
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
print('Friedrich Nietzsche once said, "What does not kill me, makes me stronger."')
输出:
Friedrich Nietzsche once said, "What does not kill me, makes me stronger."
练习2-6: 名言2 重复练习2-5,但用变量famous_person 表示名
人的姓名,再创建要显示的消息并将其赋给变量message ,然后打
印这条消息。
famous_person = "Friedrich Nietzsche"
famous_person_saying = "What does not kill me, makes me stronger."
message = f'{famous_person} once said, "{famous_person_saying}"'
print(message)
输出:
Friedrich Nietzsche once said, "What does not kill me, makes me stronger."
练习2-7: 剔除人名中的空白 用变量表示一个人的名字,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t"和"\n" 各一次。
打印这个人名,显示其开头和末尾的空白。然后,分别使用剔除函数lstrip() 、rstrip() 和strip() 对人名进行处理,并将结果打印出来。
name = "\ntang san\t"
print("Unmodifide:")
print(name)
print("\nUsing lstrip():")
print(name.lstrip())
print("\nUsing rstrip():")
print(name.rstrip())
print("\nUsing strip():")
print(name.strip())
输出:
Unmodifide:
tang san
Using lstrip():
tang san
Using rstrip():
tang san
Using strip():
tang san
练习2-8: 数字8 编写四个表达式,分别使用加法、减法、乘法和除法运算,但结果都是数字8。为使用函数调用print() 来显示结果,务必将这些表达式用圆括号括起来。也就是说,你应该编写四行类似于下面的代码:print(5+3) 输出应为四行,其中每行都只包含数字8。
print(5+3)
print(9-1)
print(2*4)
print(int(8/1))
输出:
8
8
8
8
练习2-9: 最喜欢的数 用一个变量来表示你最喜欢的数,再使用这个变量创建一条消息,指出你最喜欢的数是什么,然后将这条消息打印出来。
fav_num = 6;
msg = f"My favorite number is {fav_num}."
print(msg)
输出:
My favorite number is 6.
练习2-10: 添加注释 选择你编写的两个程序,在每个程序中至
少添加一条注释。如果程序太简单,实在没有什么需要说明的,就
在程序文件开头加上你的姓名和当前日期,再用一句话阐述程序的
功能。
# 姓名:ABU
# 日期:2022年6月7日
# 打招呼
print("Hello!")
# 计算5+5的值
print(5+5)
输出:
Hello!
10
练习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!