python作业——变量和简单数据类型

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gleam_/article/details/79491160

第一次作业都很简单,直接贴题目和代码吧。


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

代码

#2-2
message = "Today is Thursday."
print(message)
message = "Tomorrow is Friday."
print(message)

结果

Today is Thursday.
Tomorrow is Friday.

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

代码

# 2-4
name = "Liu Haoran"
print(name.lower())
print(name.upper())
print(name.title())

结果

liu haoran
LIU HAORAN
Liu Haoran

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

代码

#2-6
famous_person = "Albert Einstein"
quote = "A person who never made a mistake never tried anything new."
message = famous_person + " once said:\"" + quote +"\""
print(message)

结果

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

Q2-7 剔除人名中的空白: 存储一个人名, 并在其开头和末尾都包含一些空白字符。 务必至少使用字符组合"\t" "\n" 各一次。打印这个人名, 以显示其开头和末尾的空白。 然后, 分别使用剔除函数lstrip() rstrip() strip() 对人名进行处理, 并将结果打印出来。 

代码

#2-7
name = "    Alice   "
print(name)
print(name.lstrip())
print(name.rstrip())
print(name.strip())

结果

    Alice   
Alice   
    Alice
Alice

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

输出应为4行, 其中每行都只包含数字8 

代码

#2-8
print(5+3)
print(10-2)
print(2*4)
print(24/3)

结果

8
8
8
8

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

代码

#2-9
favorite_number = 7
print("My favorite number is " + str(favorite_number))

结果

My favorite number is 7

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

代码

#2-11
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!


妙啊,以禅宗结束本章!emmm好东西要慢慢体会!

展开阅读全文

没有更多推荐了,返回首页