Python编程:从入门到实践_动手试一试答案

动手试一试答案

  • 第二章
  • 第三章
  • 第四章

第二章

#2-1 简单消息
message = "Hello Python world"
print(message)
#2-2 多条简单消息
message = "Hello Python world"
print(message)
message = "Hello Python Crash Course world"
print(message)
#2-3 个性化消息
name = "Eric"
print("“Hello "+name+", would you liketo learn some Python today?”")
#2-4 调整名字的大小写
name = "Ada Lovelace"
print(name.title())
print(name.upper())
print(name.lower())
#2-5 名言
print("Albert Einstein oncesaid,“Apersonwho never madea mistake never tried anything new.”")
#2-6 名言2
name = "Albert Einstein"
message= "“Apersonwho never madea mistake never tried anything new.”"
print(name + " once said, " + message)
#2-7 剔除人名中的空白
favorite_language = "\n  Python   \t"
print(favorite_language)
favorite_language.lstrip()
favorite_language.rstrip()
favorite_language.strip()
#2-8 数字8
print(5 + 3)
print(4 * 2)
print(8 / 1)
print(9 - 1)
#2-9 最喜欢的数字
message = 888
print("My favorite number is " + str(message))
#2-11 Python之禅
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!

蒂姆·彼得斯的《Python禅》
美胜于丑。
显式比隐式好。
简单总比复杂好。
复杂总比复杂好。
平的比嵌套的好。
稀疏比密集好。
可读性很重要。
特殊情况不足以违反规则。
虽然实用胜过纯洁。
错误永远不应该悄无声息地过去。
除非明确沉默。
面对模棱两可的情况,拒绝猜测的诱惑。
应该有一个——最好只有一个——显而易见的方法来做到这一点。
虽然这种方式一开始可能并不明显,除非你是荷兰人。
现在总比没有好。
虽然永远都比现在好。
如果实现很难解释,那就不是个好主意。
如果实现很容易解释,这可能是一个好主意。
名称空间是一个非常好的主意——让我们做更多的工作吧!

第三章

#3-1 姓名
names = ['trek', 'cannondale', 'redline', 'specialized']
for name in names:
    print(name)
#3-2 问候语
names = ['trek', 'cannondale', 'redline', 'specialized']
for name in names:
    print('Hello '+name+" good morning!")
#3-3 自己的列表
names = ['car', 'bicycle', 'bus']
for name in names:
    print('my favorite vehicle is ' + name + '.')
#3-4 嘉宾名单
persons = ["唐僧","孙悟空","猪八戒"]
for person in persons:
    print('您好 '+person+' ,我想请你吃饭!')
#3-5 修改嘉宾名单
print('Sorry the '+persons[1]+' has no time these days ,please call someone else.')
persons[1]='白骨精'
for person in persons:
    print('您好 '+person+' ,我想请你吃饭!')
#3-6 添加嘉宾
print('My lovely friends ,i have find a bigger desk and now we can invite more friends')
print(persons)
persons.insert(0,'牛魔王')
persons.insert(2,'如来')
persons.append('沙悟净')
print(persons)
for person in persons:
    print('您好 '+person+' ,我想请你吃饭!')
#3-7 缩减名单
print('Oh my God ,now i have to pick two of you!')
for index in range(2,len(persons)):
    pop_person=persons.pop()
    print("sorry "+pop_person+" i can't help it ,because the book says.")
for person in persons:
    print('Dear '+person+ ' everything went according to plan.')
del persons[-1]
del persons[0]
print(persons)
#3-8 放眼世界
place = ['beijing','shandong','yunnan','qingdao','hangzhou']
print(place)
print(sorted(place))
print(place)
print(sorted(place,reverse=True))
print(place)
place.reverse()
print(place)
place.reverse()
print(place)
place.sort()
print(place)
place.sort(reverse=True)
print(place)
#3-9 晚餐嘉宾
persons = ["唐僧","孙悟空","猪八戒"]
for person in persons:
    print('您好 '+person+' ,我想请你吃饭!')

print(persons)
print(len(persons))

第四章

#4-1 比萨
pizzas = ['beef','tomato','Cheese']
for pizza in pizzas:
    print('My favorite pizza is '+pizza+'.')
print('I really like pizza.')
#4-2 动物
animals = ['dog','cat','snake']
for animal in animals:
    print('A '+animal+' is would make a great pet')
print('Any oftheseanimals would makea great pet!')
#4-3 数到20
for value in range(1,21):
    print(value)
#4-4 一百万
numbers = []
for x in range(1,10001):
    numbers.append(x)
for number in numbers:
    print(number)
#4-5 计算1~1000000的总和
numbers = []
for x in range(1,1000001):
    numbers.append(x)
print(min(numbers))
print(max(numbers))
print(sum(numbers))
#4-6 奇数
numbers = []
for x in range(1,21,2):
    numbers.append(x)
for number in numbers:
    print(number)
#4-7 3的倍数
numbers = []
for x in range(3,31,3):
    numbers.append(x)
for number in numbers:
    print(number)
#4-8 立方
numbers = []
for x in range(1,11):
    numbers.append(x**3)

for number in numbers:
    print(number)
#4-9 立方解析
numbers = [value**3 for value in range(1,11)]
print(numbers)

#4-10 切片
numbers = [x**3 for x in range(1,11)]
for number in numbers:
    print(number)

print('The first three items in the list are:')
print(numbers[0:3])
print('Threeitems fromthe middle ofthelistare:')
print(numbers[3:6])
print('“Thelast threeitems in thelistare:')
print(numbers[-3:])
#4-11 你的比萨和我的比萨
pizzas = ['apple pizza','banana pizza','chili pizza']
for pizza in pizzas:
    print('I like ' + pizza.title())
print('\nI really love pizza!')

friend_pizzas = pizzas[:]
pizzas.append('milk pizza')
friend_pizzas.append('black pizza')

print('\nMy favorite pizzas are:')
for pizza in pizzas:
    print(pizza)
print("\nMy friend's favorite pizzas are:")
for pizza in friend_pizzas:
    print(pizza)  

你可能感兴趣的:(python,开发语言,pycharm)