欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第一周python 作业:第二章课后练习题

程序员文章站 2022-05-22 08:02:59
...

2-1 简单消息: 将一条消息存储到变量中,再将其打印出来。

代码:

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

结果:

hello Python world!


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

代码:

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

message = "HELLO PYTHON WORLD!"
print(message)

结果:

hello Python world!
HELLO PYTHON WORLD!


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

代码:

full_name = "Eric"

print('"Hello ' + full_name + ', would you like to learn some Python today?"')

结果:

"Hello Eric, would you like to learn some Python today?"

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

代码:

full_name = "Eric"

print(full_name.lower())
print(full_name.upper())
print(full_name.title())

结果:

eric
ERIC
Eric


2-5 名言:找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):

 Albert Einstein once said, "A person who never made a mistake never tried anything new."

代码:

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-6 名言2: 重复2-5的练习,但将名人的姓名存储到变量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 删除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符,务必至少使用字符组合"\t"和"\n"各一次。打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip(), rstrip(), 和strip()对人名进行处理,并将结果打印出来。

代码:

full_name = " Albert Einstein  "

print(full_name+"\n")
print("\t"+full_name)
print(full_name.lstrip())
print(full_name.rstrip())
print(full_name.strip())

结果:

 Albert Einstein  

	 Albert Einstein  
Albert Einstein  
 Albert Einstein
Albert Einstein


2-8 数字8: 编写4个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是数字8。

代码:

print(5+3)
print(9-1)
print(2*4)
print(int(16/2))

结果:

8
8
8
8


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

代码:

message = 100

print("My favorite number is " + str(message) + ".")

结果:

My favorite number is 100.

2-10  加注释: 在程序开头加上姓名和日期

#zengzhichao march 11th  

message = 100

print("My favorite number is " + str(message))

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

第一周python 作业:第二章课后练习题