微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

《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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐