高级编程技术作业第一周2 第二章课后练习

2-1:将一条消息存储到变量中,再将其打印出来。
message = "hello world"
print(message)

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

message = "hello world"
print(message)
message = "hello Python"
print(message)

2-3:将用户的姓名存到一个变量中,并向该用户显示一条消息。

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

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

name = "kenny"
print(name.title())
print(name.upper())
print(name.lower())

2-7:剔除人名中的空白:

name = "  kenny  \t \n  "
print(name)
print(name.lstrip())
print(name.rstrip())
print(name.strip())

2-8:编写四个表达式,分别使用加减乘除,但结果都是数字8.

print(int(16/2))
print(10-2)
print(7+1)
print(2*4)

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

number = 5
message = "My favourite number is " + str(number)
print(message)

2-10:添加注释

#today is Thursday
print("Good morning")

2-11:python之禅

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!

程沁源

16328013

猜你喜欢

转载自blog.csdn.net/weixin_38742280/article/details/79478620
今日推荐