Python 从入门到精通
01.py
print("Hello Python")
message = "Hello Python World!"
print(message)
message = "Hello Wrold"
print(message)
name = "python i love you"
print(name.title())
print(name.upper())
print(name.lower())
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)
print(f"Hello,{full_name.title()}!")
message = f"Nice to meet you,{full_name}!"
print(message)
print("\tPython")
language = "languages:\n\tpython\n\tc\n\tjava"
print(language.title())
'''
输出结果
Languages:
Python
C
Java
'''
favorite_language = 'Python '
print(favorite_language.rsplit())
favorite_language2 = ' Go'
print(favorite_language2.lstrip())
favorite_language3 = ' C '
print(favorite_language3.split())
url = "https://www.baidu.com"
print(url.removeprefix("https://"))
new_url = url.removeprefix("https://")
print(new_url)
lianxi_name1 = "Eric"
print(f"Hello {lianxi_name1}, would you like to learn some Python today?")
lianxi_name2 = "toM"
lianxi_uppername = lianxi_name2.upper()
print(lianxi_uppername)
lianxi_lowername = lianxi_name2.lower()
print(lianxi_lowername)
print('Albert Einstein once said, "A person who never made a mistake never tried anything new."')
filename = 'Python_notes.txt'
print(filename.removesuffix(".txt"))
universe_age = 14_000_000_000
print(universe_age)
x, y, z = 0, 0, 0
print(x, y, z)
MAX_CONNECTIONS = 5000
print(MAX_CONNECTIONS)
02.py
languages = ['Java', 'C', 'C#', 'python', 'Go', 'PHP', 'C++']
print(languages)
print(languages[0])
print(languages[-1])
message = f"I like {languages[3].title()}!"
print(message)
languages[0] = 'javascript'
print(languages)
languages.append('VB')
print(languages)
languages.insert(0, 'R')
print(languages)
del languages[-2]
print(languages)
print(languages.pop())
print(languages.pop(1))
print(languages)
languages.append("C")
languages.remove("C")
print(languages)
cars = ['BMW', 'Audi', 'Toyota', 'Subaru']
cars.sort()
print(cars)
cars.sort(reverse=True)
print(cars)
numbers_a = ['2', '1', '3', '5', '4']
numbers_b = sorted(numbers_a)
print(numbers_b)
print(numbers_a)
print(numbers_a)
numbers_a.reverse()
print(numbers_a)
print(len(numbers_a))
'''
将这些地方存储在一个列表中并确保其中的元素不是按字母顺序排列的。
按原始排列顺序打印该列表。不要考虑输出是否整洁只管打印原始 Python 列表就好。
使用 sorted() 按字母顺序打印这个列表不要修改它。再次打印该列表核实排列顺序未变。
使用 sorted() 按与字母顺序相反的顺序打印这个列表不要修改它。再次打印该列表核实排列顺序未变。
使用 reverse() 修改列表元素的排列顺序。打印该列表核实排列顺序确实变了。
使用 reverse() 再次修改列表元素的排列顺序。打印该列表核实已恢复到原来的排列顺序。
使用 sort() 修改该列表使其元素按字母顺序排列。打印该列表核实排列顺序确实变了。
使用 sort() 修改该列表使其元素按与字母顺序相反的顺序排列。打印该列表核实排列顺序确实变了
'''
lvyou_place = ['西安', '海南', '重庆', '杭州', '北京', '天津']
print(lvyou_place)
lvyou_place1 = sorted(lvyou_place)
print(lvyou_place1)
lvyou_place2 = sorted(lvyou_place1, reverse=True)
print(lvyou_place2)
lvyou_place2.reverse()
print(lvyou_place2)
lvyou_place2.reverse()
print(lvyou_place2)
lvyou_place2.sort()
print(lvyou_place2)
lvyou_place2.sort(reverse=True)
print(lvyou_place2)
xiaomanshenghuo = ['yeyifan', 'chenli', 'houyuanyuan', 'hejiaru', 'tiantian', 'tiandong']
for people in xiaomanshenghuo:
print(people)
animals = ['monkey', 'dog', 'cat']
for animal in animals:
print(f"{animal.title()}, hava a good time!")
print("hava fun!")
print("I'm ok! Thank you!")
for value in range(1, 5):
print(value)
numbers = list(range(1, 5))
print(numbers)
even_numbers = list(range(2, 11, 2))
print(even_numbers)
squares = []
for value in range(1, 11):
square_number = value ** 2
squares.append(square_number)
print(squares)
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))
squares1 = [value**2 for value in range(1,11)]
print(squares1)
players = ['yaoming','subingtian','sunyang','fanzhendong','yangqian','guojingjing','langping']
print(players[0:3])
print(players[:4])
print(players[2:])
print(players[-3:])
print("Here are the first three players on my team :")
for player in players[:3]:
print(player.title())
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
print("my favorite foods are :")
print(my_foods)
print("\nmy friend's favorite foods are :")
print(friend_foods)
my_foods.append("cannoli")
friend_foods.append("ice cream")
print("my favorite foods are :")
print(my_foods)
print("\nmy friend's favorite foods are :")
print(friend_foods)
03.py
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
my_t = (3,)
for dimension in dimensions:
print(dimension)
dimensions1 = (200, 50)
print("Original dimensions:")
for dimension1 in dimensions1:
print(dimension1)
dimensions1 = (400, 100)
print("\nModified dimensions:")
for dimension1 in dimensions1:
print(dimension1)
'''
使用一个 for 循环将该餐馆提供的 5 种食品都打印出来。
尝试修改其中的一个元素核实 Python 确实会拒绝你这样做。
餐馆调整菜单替换了两种食品。请编写一行给元组变量赋值的代码并使用一个 for 循环将新元组的每个元素都打印出来。
'''
print("本店的食品如下:")
foods = ('老醋六样','锅包肉','糖醋里脊','烤鱼','拔丝地瓜')
for food in foods:
print(food)
print("\n现在的食品如下:")
foods = ('麻辣兔头','锅包肉','糖醋里脊','大拉皮','拔丝地瓜')
for food in foods:
print(food)
cars = ['audi','bmw','subaru','toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
request_topping = 'mushrooms'
if request_topping != 'anchovies':
print("Hold the anchovies")
request_topping = ['mushrooms','onions','pineapple']
print('mushrooms' in request_topping)
age = 18
print(age == 18)
answer = 17
if answer != 42:
print("That is not the correct answer.Please try again")
age = 17
print(age<21)
print(age<=21)
print(age>21)
print(age>=21)
age_0 = 22
age_1 = 18
print(age_0 >= 21 and age_1>=21)
age_1 = 22
print(age_0 >=21 and age_1>=21)
age_0 = 22
age_1 = 18
print(age_0 >= 21 or age_1>=21)
age_0 = 18
print(age_0 >= 21 or age_1>=21)
banned_user = ['andrew','carolina','david']
user = 'marie'
if user not in banned_user:
print(f"{user.title()},you can post a response if you wish.")
requested_toopings = ['mushrooms','green peppers','extra cheese']
for requested_tooping in requested_toopings:
if requested_tooping == "green peppers":
print("Sorry,we are out of green peppers right now.")
else:
print(f"Adding {requested_tooping}.")
print("\nFinished making your pizza!")
requested_toopings = []
if requested_toopings:
for requested_tooping in requested_toopings:
print(f"Adding {requested_tooping}")
print("\nFinished making your pizza")
else:
print("Are you sure you want a plain pizza?")