Python编程:从入门到实践(第二版)随书敲代码 1~3

Hello World.py

print("Hello World")

message = "Hello Python Crash Course World!"
print(message)

name.py

name = "ada lovelace"
print(name.title())
print(name.upper())
print(name.lower())

full_name.py

first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)
print(f"Hello,{full_name.title()}!")

message = f"Hello,{full_name.title()}!"
print(message)

opostrophe.py

message = "One of python's strengths is its diverse community."
print(message)

name_cases.py

name = "Eric"
message = f"Hello {name},would you like to learn some Python today?"
print(message)

name_cases1.py

name = "Zhong guo"
print(name.title())
print(name.upper())
print(name.lower())

famous_person.py

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

famous_person1.py

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

name_trip.py

name = "\tAlbert Einstein\n"
print("For example:")
print(name)

print("\nusing trip():")
print(name.strip())

print("\nusing lstrip():")
print(name.lstrip())

print("\nusing rstrip():")
print(name.rstrip())

math.py

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

favorite_number.py

#输出最喜欢的数
fav_number = 666
msg = f"My favorite number is {fav_number}."
print(msg)

comment.py

#向大家问好。
print("Hello Python people!")

The zen of Python.py

#Python之禅
import this

bicycles.py

bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[-1].title())

message = f"My first bicycle was a {bicycles[0].title()}."
print(message)

name_friend.py

#3-1姓名:将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
names = ['yuki','sakura','yamaha','kalazan']
print(names[0])
print(names[1])
print(names[2])
print(names[3])
print(names[-1].title())

name_friend1.py

#3-2问候语:不打印每个朋友的姓名,而为每人打印一条消息。
#每条消息都包含相同的问候语,但抬头为相应的朋友姓名。
names = ['yuki','sakura','yamaha','kalazan']
message = f"Hello,nice to meet you,{names[0].title()}!"
print(message)

message = f"Hello,nice to meet you,{names[1].title()}!"
print(message)

message = f"Hello,nice to meet you,{names[2].title()}!"
print(message)

message = f"Hello,nice to meet you,{names[3].title()}!"
print(message)

commuting_mode.py

#3-3自己的列表:想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言
#如:I would like to own a Honda motorcycle.
commuting_mode = ['bicycle','motorcycle','car']
message = f"I would like to own a {commuting_mode[0].title()}."
print(message)

motocycles.py

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

motorcycles[0] = 'ducati'
print(motorcycles)

motorcycles.append('honda')
print(motorcycles)

append.py

motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)

insert.py

motorcycles = ['honda','yamaha','suzuki']
motorcycles.insert(1,'ducati')
print(motorcycles)

del.py

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

del motorcycles[0]
print(motorcycles)

pop.py

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

first_owned.py

motorcycles = ['honda','yamaha','suzuki']
first_owned = motorcycles.pop()
print(f"The first motorcycle I owned was a {first_owned.title()}.")

remove.py

motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)

motorcycles.remove('ducati')
print(motorcycles)

motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)

too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f"\nA {too_expensive.title()} is too expensive for me.")

guests.py

#如果你可以邀请任何人共进晚餐(无论是故去的还是在世的),你会邀请哪些人?请
#创建一个列表,其中包含至少三个你想邀请的人;然后,使用这个列表打印消息,邀请这
#些人来与你共进晚餐。
guests = ['yuki','sakura','kalazan']

# 邀请嘉宾与你共进晚餐。
name = guests[0].title()
print(f"{name},please come to dinner.")

name = guests[1].title()
print(f"{name},please come to dinner.")

name = guests[2].title()
print(f"{name},please come to dinner.")

#指出其中一位嘉宾无法赴约。
name = guests[1].title()
print(f"\nSorry,{name} can't make it to dinner.")

#修改嘉宾名单,替换无法赴约嘉宾为新邀请嘉宾姓名。
del(guests[1])
guests.insert(1,'kuraki')

#重新邀请嘉宾与你共进晚餐。
name = guests[0].title()
print(f"\n{name},please come to dinner.")

name = guests[1].title()
print(f"{name},please come to dinner.")

name = guests[2].title()
print(f"{name},please come to dinner.")

#找到了更大的餐桌,再邀请三位嘉宾。
print("\nWe got a bigger table!")
guests.insert(0,'yutada')
guests.insert(2,'boa')
guests.append('kaori')

name = guests[0].title()
print(f"\n{name},please come to dinner.")

name = guests[1].title()
print(f"{name},please come to dinner.")

name = guests[2].title()
print(f"{name},please come to dinner.")

name = guests[3].title()
print(f"{name},please come to dinner.")

name = guests[4].title()
print(f"{name},please come to dinner.")

name = guests[5].title()
print(f"{name},please come to dinner.")

#餐桌无法及时送达,
print("\nSorry,we can only invite two people to dinner.")

name = guests.pop()
print(f"Sorry,{name.title()} there's no room at the table.")

name = guests.pop()
print(f"Sorry,{name.title()} there's no room at the table.")

name = guests.pop()
print(f"Sorry,{name.title()} there's no room at the table.")

name = guests.pop()
print(f"Sorry,{name.title()} there's no room at the table.")

print(len(name))

cars.py

cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)

cars.sort(reverse=True)
print(cars)

cars_sorted.py

cars = ['bmw','audi','toyota','subaru']

print("Here is the original list:")
print(cars)

print("\nHere is the sorted list:")
print(sorted(cars))

print("\nHere is the original list again:")
print(cars)

cars.reverse()
print(cars)

print(len(cars))

locations.py

#3-8列出渴望去的五个地方
locations = ['japan','hongkong','xizang','himalaya','korean']

#按原始排列顺序打印列表
print("Original order:")
print(locations)

#使用sorted()按字母顺序打印列表
print("\nAlphabetical:")
print(sorted(locations))

#再次打印列表进行核实顺序未变
print("\nOriginal order:")
print(locations)

#使用sorted()临时按字母顺序相反的顺序打印列表,不修改列表。
print("\nReverse alphabetical:")
print(sorted(locations,reverse=True))

#再次打印列表进行核实顺序未变
print("\nOriginal order:")
print(locations)

#使用reverse()修改列表元素排列顺序,并打印核实
print("\nReversed:")
locations.reverse()
print(locations)

#使用reverse()再次修改列表元素排列顺序,打印并核实已恢复到原来的排序。
print("\nOriginal order:")
locations.reverse()
print(locations)

#使用sort()修改列表,按字母顺序排列并打印核实
print("\nAlphabetical:")
locations.sort()
print(locations)

#使用sort()修改列表,使其元素按字母顺序相反的顺序排列,打印并核实
print("\nReverse alphabetical:")
locations.sort(reverse=True)
print(locations)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值