您的位置:首页 > 编程语言

高级编程技术 课后作业二

2018-03-08 10:46 316 查看
2-1 简单消息:将一条消息存储到变量中,再将其打印出来
>>> message = "Hello World! "
>>> print(message)
Hello World!
2-2 多条简单消息:将一条消息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来
>>> message = "Hello World! "
>>> print(message)
Hello World!
>>> message = "Hello Python! "
>>> print(message)
Hello Python!
2-3 个性化信息:将用户的姓名存到一个变量中,并向该用户显示一条消息
>>> name = "Eric"
>>> print("Hello "+name+", would you like to learn some Python today?")
Hello Eric, would you like to learn some Python today?


2-4 调整名字的大小写:将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示人名
>>> name = "alice smith"
>>> print(name.title())
Alice Smith
>>> print(name.upper())
ALICE SMITH
>>> print(name.lower())
alice smith


2-5 名言:找一句你钦佩的名人说的名言
>>> print('Albert Einstein once said,"A person who never made a mistake never tried anything new."')
Albert Einstein once said,"A person who never made a mistake never tried anything new."


2-6 名言2:将名人的姓名存储在变量famous_person中,再创建要显示的消息,并将其存储在变量message中,然后打印这条消息
>>> famous_person = "Albert Einstein"
>>> message = "A person who never made a mistake never tried anything new."
>>> print(famous_person + ' once said,"' + message + '"')
Albert Einstein once said,"A person who never made a mistake never tried anything new."


2-7 剔除人名中的空白:存储一个个人名,并在其开头和末尾都包含一些空白字符,打印这个人名然后使用剔除函数对人名进行处理,并将结果打印出来
>>> name = "\t\t Albert Einstein \n"
>>> print(name)
Albert Einstein

>>> print(name.lstrip())
Albert Einstein

>>> print(name.rstrip())
Albert Einstein
>>> print(name.strip())
Albert Einstein

2-8 数字8:编写4个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是8,使用print语句
>>> print(3 + 5)
8
>>> print(10 - 2)
8
>>> print(2 * 4)
8
>>> print(int(16 / 2))
8

2-9 最喜欢的数字:将你最喜欢的数字存储在一个变量中,再使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条消息打印出来
>>> number = 8086
>>> print("My favorite number is " + str(number) + ".")
My favorite number is 8086.


2-10 添加注释:选择你编写的两个程序,在每个程序中都至少添加一条注释
# 向大家问好
message = "Hello Python people! "
print(message)
'''
4个分别使用加法、减法、乘法和除法运算的表达式
结果都是8,且都使用print语句
'''
print(3 + 5)
print(10 - 2)
print(2 * 4)
print(int(16 / 2))


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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: