第二章作业

1.2变量

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

 

1 numpy_message = 曹富是帅哥  #没有加引号
2  print(numpy_message)   #第一个和第二个首字母都不要缩进 

输出:

numpy_message = 曹富是帅哥
^
IndentationError: unexpected indent

 

 

 2-2 简单 将 numpy_message 改成message

 

 2-3 个性化消息: 将用户的姓名存到一个变量中, 并向该用户显示一条消息。 显示的消息应非常简单, 如“Hello Eric, would you like to learn some Python today?”。
2-4 调整名字的大小写: 将一个人名存储到一个变量中, 再以小写、 大写和首字母大写的方式显示这个人名。
2-5 名言: 找一句你钦佩的名人说的名言, 将这个名人的姓名和他的名言打印出来。 输出应类似于下面这样(包括引 号) :
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
2-6 名言2: 重复练习 2-5, 但将名人的姓名存储在变量famous_person 中, 再创建要显示的消息, 并将其存储在变量message 中, 然后打印这条消息。
2-7 剔除人名中的空白: 存储一个人名, 并在其开头和末尾都包含一些空白字符。 务必至少使用字符组合" \t" 和" \n" 各一次。
打印这个人名, 以显示其开头和末尾的空白。 然后, 分别使用剔除函数lstrip( ) 、 rstrip( ) 和strip( ) 对人名进行处理, 并将结果打印出来。

答:2-3

caofu = "”Hello Eric, would you like to learn some Python today?“"   #用那个英文的引号包含中文的双引号就行啦
print(caofu)

输出:”Hello Eric, would you like to learn some Python today?“

 

 

2-4

name = 'cao fu'
print(name.upper())
print(name.lower())
print(name.title())

输出:CAO FU
cao fu
Cao Fu

 

2-5

message = 'Albert Einstein once said, “A person who never made a mistake never tried anything new.”'
print(message)
输出:
Albert Einstein once said, “A person who never made a mistake never tried anything new.”

 

2-6

我的错了

famous_person = 'Albert Einstein'
famous_word = 'once said“A person who never made a mistake never tried anything new.”'
full_word = 'famous_person ' + ' famous_word'
message = 'full_word'
print(message)

输出:
famous_person + full_word

 

注:#这样的貌似必须要first_name last_name full_name first_name last_name full_name才可以执行

别人的正确答案

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

nam =  ' c\tao f\nu '
print(nam)
print(nam. rstrip())
print(nam. lstrip())
print(nam. strip())

输出:
 c    ao f
u 
 c    ao f
u
c    ao f
u 
c    ao f
u

 

 

2-8 数字8: 编写4个表达式, 它们分别使用加法、 减法、 乘法和除法运算, 但结果都是数字8。 为使用print 语句来显示结果, 务必将这些表达式用括号括起来, 也
就是说, 你应该编写4行类似于下面的代码:

print(4+4)
print(11-3)
print(4*2)
print(16/2)

输出:
8
8
8
8.0

 

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

favorite_number = 4
print("My favorite number is " + str(favorite_number))

2-10 添加注释: 选择你编写的两个程序, 在每个程序中都至少添加一条注释。 如果程序太简单, 实在没有什么需要说明的, 就在程序文件开头加上你的姓名和当前日
期, 再用一句话阐述程序的功能

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

 1 import this
 2 
 3 输出:
 4 The Zen of Python, by Tim Peters
 5 
 6 Beautiful is better than ugly.
 7 Explicit is better than implicit.
 8 Simple is better than complex.
 9 Complex is better than complicated.
10 Flat is better than nested.
11 Sparse is better than dense.
12 Readability counts.
13 Special cases aren't special enough to break the rules.
14 Although practicality beats purity.
15 Errors should never pass silently.
16 Unless explicitly silenced.
17 In the face of ambiguity, refuse the temptation to guess.
18 There should be one-- and preferably only one --obvious way to do it.
19 Although that way may not be obvious at first unless you're Dutch.
20 Now is better than never.
21 Although never is often better than *right* now.
22 If the implementation is hard to explain, it's a bad idea.
23 If the implementation is easy to explain, it may be a good idea.
24 Namespaces are one honking great idea -- let's do more of those!

 

posted on 2018-04-12 16:16  c富-  阅读(1767)  评论(0编辑  收藏  举报