第二章
练习 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)
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/272837.html