Python作业01-变量和简单数据类型

Description

Select from exercise 2-1 to 2-11.

I will finish the code with comment about the requirement.

Code

2-3

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

name = input()
print("Hello " + name + ", would you like to learn some Python today?")

result:

D:\pyproject\homework1\venv\Scripts\python.exe D:/pyproject/homework1/2_3.py
Jobs
Hello Jobs, would you like to learn some Python today?

Process finished with exit code 0

2-4

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

name = input()
print(name.upper())
print(name.lower())
print(name.title())

result:

D:\pyproject\homework1\venv\Scripts\python.exe D:/pyproject/homework1/2_4.py
sKy raKeR
SKY RAKER
sky raker
Sky Raker

Process finished with exit code 0

2-7

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

name = "\n\tSky Raker\t\n\t"
print("-------------")
print(name)
print("-------------")
print(name.lstrip())
print("-------------")
print(name.rstrip())
print("-------------")
print(name.strip())
print("-------------")

result:

D:\pyproject\homework1\venv\Scripts\python.exe D:/pyproject/homework1/2_7.py
-------------

    Sky Raker    

-------------
Sky Raker    

-------------

    Sky Raker
-------------
Sky Raker
-------------

Process finished with exit code 0

2-8

1
2
3
4
5
6
7
8
# 2-8 数字8: 编写4个表达式,它们分别使用
# 加法、减法、乘法和除法运算,但结果都是数字8。
# 为使用print 语句来显示结果,务必将这些表达式用括号括起来,

print(3+5)
print(10-2)
print(2*4)
print(1000/125)

result:

D:\pyproject\homework1\venv\Scripts\python.exe D:/pyproject/homework1/2_8.py
8
8
8
8.0

Process finished with exit code 0

2-11

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

import this

result:

D:\pyproject\homework1\venv\Scripts\python.exe D:/pyproject/homework1/2_11.py
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!

Process finished with exit code 0