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

Python第一次作业

2018-03-08 15:04 330 查看
                                                                Python第一次作业                                                                                                                        参考书目:《Python编程从入门到实践》
                                                                                                                                     2018/03/08
2-1简单消息:将一条消息存储到变量中,再将其打印出来
message = "hello world"
print(message)
 
 
2-2多条简单消息:将一条消息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来
message = "hello world"
print(message)
message = "hello China"
print(message)
 
 
2-3个性化消息:将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应该非常简单:Hello Eric ,would you like to learn some Python today?
user_name = "Eric"
message = "Hello " + user_name +",would you like to learn some Python today? "
print(message)
 
 
2-4调整名字的大小写:将一个人名存储到一个变量中,再以小写、大写和首字母大小的方式显示这个人名
user_name = "Eric Alice"
print(user_name.lower())
print(user_name.upper())
print(user_name.title())
 
 
2-5名言:找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来(包括引号)
print('Albert Einstein said,"A personwho never made a mistake never tried anything new."')
 
 
2-6名言2:重复2-5,但将名人的姓名存储在变量famous_person中,在创建要显示的消息,并将其存储在变量message中,然后打印这条消息
famous_person = "Albert Einstein"
message = "A person who never made amistake never tried anything new."
print(famous_person + ' said,"' +message + '"')
 
 
2-7剔除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少是使用字符组合“\t”和”\n”各一次。
user_name = " \n\t Eric Alice \t"
print(user_name)
print(user_name.lstrip())
print(user_name.rstrip())
print(user_name.strip())
 
 
2-8数字8:编写四个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是数字8.为使用print语句来显示结果,务必将这些表达式用括号括起来
print(4 + 4)
print(16 - 8)
print(2 * 4)
print(int(16 / 2))
 
 
2-9最喜欢的数字:将你最喜欢的数字存储在一个变量中,再使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条消息打印出来
favourite_number = 666
print("my favourite number is " +str(favourite_number) + ".")
 
 
2-10添加注释:选择你编写的两个程序,在每个程序中至少都添加一条注释。如果程序太简单,实在没有什么需要说明的,就在程序的文件开头写上你的名字和当前的日期,再用一句话阐述程序的功能。
#2018/03/08
"""print a famous person andhis famous saying"""
fa
9044
mous_person = "Albert Einstein"
message = "A person who never made amistake never tried anything new."
print(famous_person + ' said,"' +message + '"')
 
 
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 tobreak the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse thetemptation to guess.
There should be one-- and preferably onlyone --obvious way to do it.
Although that way may not be obvious atfirst 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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: