概述
从零开始学Python第一天
由于是有大学三年专业知识的学习基础的(有但是不多),外加之前断断续续看过几天的Python书,但是没有动手实践,就准备把练习题都写一下,都是很简单的题目,主要是为了增加熟练度。
第二章
2_1简单消息:将一条消息存储到变量中,再将其打印出来。
simple_message.py
simple_message="你好"
print(simple_message)
2-2 多条简单消息:将一条消息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。
simple_messages.py
simple_messages=“你好”
print(simple_messages)
simple_messages="pansz"
print(simple_messages)
2-3 个性化消息:将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应非常简单,如“Hello Eric,would you like to learn some Python today?”。
name_hello.py
name="pansz"
print("Hello"+name+",would you like to learn some Python today?")
2-4 调整名字的大小写:将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人名。
name_change.py
name="pan Sheng zheng"
print(name.upper())#全大写
print(name.title())#首字母大写
print(name.lower())#全小写
2-5+2_6 名言:找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
famous_remark.py
famous_name="Albert Einstein"
famous_word="A person who never made a mistake never tried anything new"
print(famous_name+'once said,'+'"'+famous_word+'"')#需要双引号的时候用单引号如:'"'
2-7 剔除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"t"和"n"各一次。打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数 lstrip()、rstrip()和 strip()对人名进行处理,并将结果打印出来。
blank_delete.py
name=" pansz "
print(name)
print(name.lstrip())#删除左边空格
print(name.rstrip())#删除右边空格
print(name.strip())#删除两端空格
2-8 数字 8:编写 4 个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是数字 8。为使用 print 语句来显示结果,务必将这些表达式用括号括起来:
number_8.py
print(1+7)
print(2*4)
print(10-2)
print(16/2)
2-9 最喜欢的数字:将你最喜欢的数字存储在一个变量中,再使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条消息打印出来。
favorite_number.py
favorite_numbe="520"
print("My favorite numbe is "+'"'+favorite_numbe+'"')
2-10 添加注释:选择你编写的两个程序,在每个程序中都至少添加一条注释。如果程序太简单,实在没有什么需要说明的,就在程序文件开头加上你的姓名和当前日期,再用一句话阐述程序的功能。
这题在前面的基本有需求的都加了,单行注释#,Python一般都是在前一行注释,比如
#输出hello
print("hello")
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!
具体的书上有,目前我也不是很懂哈哈哈。
第二章基础完结,希望能坚持下来每天都分享一下学习内容,也算是个学习的正反馈。
最后
以上就是尊敬裙子为你收集整理的Python编程:从入门到实践第二章练习题的全部内容,希望文章能够帮你解决Python编程:从入门到实践第二章练习题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复