《Python编程从入门到实践》练习选做

第二章

练习 2-1

message = "Hello!"
print(message)

练习 2-2

message = "Hllo"
print(message)
message = "Hello"
print(message)

练习 2-3

username = "eric"
print(f"Hello {username.title()},would you like to learn some Python today?")

练习 2-4

username = 'hoshiuz'
print(username.lower())
print(username.upper())
print(username.title())

练习 2-5

print('Albert Einstein once said,"A person who never made a mistake never tried anthing new"')

练习 2-6

famous_person = 'Albert Einstein'
sentence = 'A person who nerver made a mistake never tried anything new.'
print(f'{famous_person} once said,"{sentence}"')

练习 2-7

name = '\tHoshiuZ\t'
print("Unmodified:")
print(name)
print("\nUsing lstrip():")
print(name.lstrip())
print("\nUsing rstrip():")
print(name.rstrip())
print("\nUsing strip():")
print(name.strip())

练习 2-8

print(1+7)
print(9-1)
print(2*4)
print(16/2)

练习 2-9

fav_num = 13
print("My favorite numnber is:")
print(fav_num)

第三章

练习 3-1

names = ['gzy', 'lys', 'tqz', 'zxy', 'zz']
for name in names:
    print(name)

练习 3-2

names = ['gzy', 'lys', 'tqz', 'zxy', 'zz']
for name in names:
    print(f"Hello,{name}!")

练习 3-4

names = ['gzy', 'lys', 'tqz', 'zxy', 'zz']
for name in names:
    print(f"{name.title()},please come to dinner")

练习 3-5

names = ['gzy', 'lys', 'tqz', 'zxy', 'zz']
for name in names:
    print(f"{name.title()},please come to dinner")
print(f"{names[3].title()} can not come to dinner.")
names[3] = 'xk'
for name in names:
    print(f"{name.title()},please come to dinner")

练习 3-6

names = ['gzy', 'lys', 'tqz']
for name in names:
    print(f"{name.title()},please come to dinner")
print("I found a bigger table!")
names.insert(0, 'zz')
names.insert(2,'xk')
names.append('zxy')
for name in names:
    print(f"{name.title()},please come to dinner")

练习 3-7

names = ['gzy', 'lys', 'tqz']
for name in names:
    print(f"{name.title()},please come to dinner")
print("I found a bigger table!")
names.insert(0, 'zz')
names.insert(2,'xk')
names.append('zxy')
for name in names:
    print(f"{name.title()},please come to dinner")
print("Oh,the table is broken.I can only invite two bros.")
name = names.pop()
print(f"Sorry {name.title()},there is no room at the table")
name = names.pop()
print(f"Sorry {name.title()},there is no room at the table")
name = names.pop()
print(f"Sorry {name.title()},there is no room at the table")
name = names.pop()
print(f"Sorry {name.title()},there is no room at the table")
for name in names:
    print(f"{name.title()},please come to dinner")

练习 3-8

locations = ['C', 'A', 'E', 'B', 'D']
print(locations)
print(sorted(locations))
print(sorted(locations,reverse=True))
print(locations)
locations.reverse()
print(locations)
locations.reverse()
print(locations)
locations.sort()
print(locations)
locations.sort(reverse=True)
print(locations)

练习 3-9

names = ['gzy', 'lys', 'tqz', 'zxy', 'zz']
for name in names:
    print(f"{name.title()},please come to dinner")
print(f"{names[3].title()} can not come to dinner.")
num = len(names)
print("The number of bros who I invite is:")
print(num)

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...