作业二——变量与运算符练习题

        本章主要讲解了如何表示字符串类型("..." 或 '...')及字符串某些方法(len(str), title(), upper(), lower(), rstrip()),还有整数类型及其运算符。


2 - 1 简单信息 

    题目描述:

        将一条消息存储在变量中,再将其打印出来。

    INPUT:

        None

    OUTPUT:   

This is a message.

代码展示:

# 将消息存储在message中
message = "This is a message."
print(message)


2 - 2 多条简单信息 

    题目描述:

        将一条消息存储在变量中,再将其打印出来;再将其变量修改为另一条新消息,并将它再次打印出来。

    INPUT:

        None

    OUTPUT:    

This is a message.
This is a new message.

    代码展示:

# 将消息存储在message中
message = "This is a message."
print(message)

# 修改message的值
message = "This is new a message."
print(message)


2 - 3 个性化信息 

    题目描述:

        将用户的姓名存到一个变量之中,并向用户显示一条信息。

    INPUT:

        None

    OUTPUT:    

Hello Peter,would you like to learn some Python today?

    代码展示:

# 将用户姓名存储在userName中
userName = "peter"
print("Hello " + userName.title() + ", would you like to learn some Python today?")


2 - 4 调整名字的大小写

    题目描述:

        将用户的姓名存到一个变量之中,再以小写,大写和首字母大写来显示这个人名。

    INPUT:

        None

    OUTPUT:    

Print in upper case: PETER
Print in lower case: peter
Print in title case: Peter

    代码展示:

# 将用户姓名存储在userName中
userName = "peTEr"
print("Print in upper case: " + userName.upper())
print("Print in lower case: " + userName.lower())
print("Print in title case: " + userName.title())


2 - 5 名言

    题目描述:

        找一句自己喜欢的名人名言,将这个名人和其名句打印出来。

    INPUT:

        None

    OUTPUT:    

William Shakespeare once said,"To be or not to be, that's a question."

    代码展示:

print("William Shakespeare once said,\"To be or not to be, that's a question.\")


2 - 6 名言 2    

    题目描述:

      重复2-5,但将名人的姓名存储在变量famous_person中,再创建要显示的消息,并将其存储在变量message中,然后打印这条消息

    INPUT:

        None

    OUTPUT:    

William Shakespeare once said,"To be or not to be, that's a question."

    代码展示:

# 将名人姓名存储在famous_person中,将名句存储在message之中
name = "William Shakespeare"
quote = "\"To be or not to be, that's a question.\""
print(name.title() + " once said," + quote)


2 - 7 剔除人名中的空白

    题目描述:

        存储一个个人名,并在其开头和末尾都包含一些空白字符,打印这个人名然后使用剔除函数对人名进行处理,务必在其中包含'\t','\n',并将结果打印出来。

    INPUT:

        None

    OUTPUT:    

Print original name:    William Shakespeare  
  
Print name without space on left:William Shakespeare  
  
Print name without space on right:    William Shakespeare
Print name without space on both side:William Shakespeare

    代码展示:

name = "\tWilliam Shakespeare  \n  "
print("Print original name:" + name)
print("Print name without space on left:" + name.lstrip())
print("Print name without space on right:" + name.rstrip())
print("Print name without space on both side:" + name.strip())

2 - 8 数字8

    题目描述:

        编写4个表达式,它们分别使用加法、减法、乘法和除法运算,使其结果均为8。

    INPUT:

        None

    OUTPUT:    

8
8
8
8

    代码展示:

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

2 - 9 最喜欢的数字

    题目描述:

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

    INPUT:

        None

    OUTPUT:    

My Favorite number is 666 !

    代码展示:

myNum = 666
print("My Favorite number is " + str(myNum) + " !")


2-10 添加注释

    题目描述:

       选择你编写的两个程序,在每个程序中都至少添加一条注释

    INPUT:

        None

    OUTPUT:    

         None

    代码展示:

myNum = 666
#为了显示数字,必须转换成字符串的形式
print("My Favorite number is " + str(myNum) + " !")
# 将用户姓名存储在userName中
userName = "peTEr"
print("Print in upper case: " + userName.upper())
print("Print in lower case: " + userName.lower())
print("Print in title case: " + userName.title())

2-11 Python之禅

 题目描述:

       在Python终端会话中执行命令import this

    INPUT:

        None

    OUTPUT:    

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!

    代码展示:

import this


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值