练习2-1
:简单消息:将一条消息赋给变量,并将其打印出来。
代码:
a="hello world"
print(a)
结果:
hello world
练习2-2
:多条简单消息:将一条消息赋给变量,并将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。
代码:
b="simple is better than complex"
print(b)
b="yes"
print(b)
结果:
simple is better than complex
yes
练习2-3
:个性化消息:用变量表示一个人的名字,并向其显示一条消息。显示的消息应非常简单,下面是一个例子。
Hello Eric, would you like to learn some Python today?
代码:
a = "jack"
print("Hello " +a+ ", would you like to learn some Python today?")
结果:
Hello jack, would you like to learn some Python today?
练习2-4
:调整名字的大小写:用变量表示一个人的名字,再以小写、大写和首字母大写的方式显示这个人名。
代码:
a = "jack"
b=a.lower()
c=a.upper()
d=a.title()
print("小写:",b)
print("大写:",c)
print("首字母大写:",d)
结果:
小写: jack
大写: JACK
首字母大写: Jack
练习2-5
:名言找一句你钦佩的名人说的名言,将其姓名和名言打印出来。输出应类似于下面这样(包括引号)。
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
代码:
print('Albert Einstein once said, "A person who never made a mistake never tried anything new."')
结果:
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'
print(famous_person,'once said, "A person who never made a mistake never tried anything new."')
结果:
Albert Einstein once said, "A person who never made a mistake never tried anything new."
练习2-7
:剔除人名中的空白用变量表示一个人的名字,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t"和"\n"各一次。
打印这个人名,显示其开头和末尾的空白。然后,分别使用剔除函数lstrip()、rstrip()和strip()对人名进行处理,并将结果打印出来。
代码:
a = " \tjack sdf \n"
print(a)
print(a.lstrip())
print(a.rstrip())
print(a.strip())
结果:
jack sdf
jack sdf
jack sdf
jack sdf
lstrip()去除开头空格,rstrip()去除末尾空格,strip()去除两边空格
\t制表符,\n换行符
练习2-8
:数字8
编写四个表达式,分别使用加法、减法、乘法和除法运算,但结果都是数字8。为使用函数调用print()来显示结果,务必将这些表达式用圆括号括
起来。也就是说,你应该编写四行类似于下面的代码:
print(5+3)
输出应为四行,其中每行都只包含数字8。
代码:
print(3+5)
print(10-2)
print(2*4)
print(int(16/2))
结果:
8
8
8
8
Process finished with exit code 0
练习2-9
:最喜欢的数
用一个变量来表示你最喜欢的数,再使用这个变量创建一条消息,指出你最喜欢的数是什么,然后将这条消息打印出来。
代码:
favoritenum = 123
print(f'我最喜欢的数是{favoritenum}')
结果:
我最喜欢的数是123
Process finished with exit code 0
练习2-10
:添加注释
选择你编写的两个程序,在每个程序中至少添加一条注释。如果程序太简单,实在没有什么需要说明的,就在程序文件开头加上你的姓名和当前日期,再用一句话阐述程序的功能。
# 求两个数的和
a = int(input("请输入数字a:"))
b = int(input("请输入数字b:"))
print('两个数的和为:' ,a+b)
# 求两个数的积
a = int(input("请输入数字a:"))
b = int(input("请输入数字b:"))
print('两个数的积为:' ,a*b)
练习2-11
:Python 之禅
在Python 终端会话中执行命令import this,并粗略地浏览一下其他的指导原则。
代码:
import this
结果:
he 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!
Process finished with exit code 0
网友评论