#4 Operating on lists#4.1 Traversing the entire list
print("\n4.1")
magicians=['alice','david','carolina']
for magician in magicians:
print(magician.title())
#4.1.1 Delving into loops#4.1.2 Performing more operations within a for loop
magicians=['alice','david','carolina']
for magician in magicians:
print(magician.title()+", that was a great trick!")
magicians=['alice','david','carolina']
for magician in magicians:
print(magician.title()+", that was a great trick!")
print("I can't wait to see your next trick, "+magician.title()+".\n")
#4.1.3 Performing some operations after a for loop ends
magicians=['alice','david','carolina']
for magician in magicians:
print(magician.title()+", that was a great trick!")
print("I can't wait to see your next trick, "+magician.title()+".\n")
print("Thank you everyone.That was a great magic show!")
# Homework 4-1: pizzas
pizzas=['neapolitan pizza','chicago pizza','pepperoni pizza']
for pizza in pizzas:
print('I like '+pizza+'.')
print('I like '+pizzas[0]+','+pizzas[1]+' and '+pizzas[2]+'.'+'I really love pizza!')
# Homework 4-2: Animals
animals=['dog','cat','bird']
for animal in animals:
print(animal)
for animal in animals:
print('A '+animal+' would make a great pet.')
print('Any of these animals would make a great pet!')
# 4.3 Creating numerical lists#4.3.1 Using the range() function
print(range(1,6))
for value in range(1,5):
print(value)
for value in range(1,6):
print(value)
# 4.3.2 Creating lists of numbers using range()# 01
numbers=list(range(1,6))
print(numbers)
#02
even_numbers =list(range(2,11,2))
print(even_numbers)
#03
squares=[]
for value in range(1,11):
square=value**2
squares.append(square)
print(squares)
#or
squares=[]
for value in range(1,11):
squares.append(value**2)
print(squares)
# 4.3.3 Performing simple statistical calculations on a list of numbers
digits=[1,2,3,4,5,6,7,8,9,0]
print(min(digits))
print(max(digits))
print(sum(digits))
# 4.3.4 List comprehension
squares=[value**2for value in range(1,11)]
print(squares)
# Homework 4-3: Counting to 20for number in range(1,21):
print(number)
# Homework 4-4: One Million#numbers=list(range(1,1000000))#for number in numbers:#print(number)# Homework 4-4: Calculate the sum of numbers from 1 to 1000000
numbers=list(range(1,100001))
print(min(numbers))
print(max(numbers))
print(sum(numbers))
# Homework 4-6: Odd Numbers
numbers=list(range(1,21,2))
for number in numbers:
print(number)
# Homework 4-7: Multiples of 3
numbers=list(range(3,31,3))
for number in numbers:
print(number)
# Homework 4-8: Cubes
squares=[]
for value in range(1,11):
square=value**3
squares.append(square)
print(squares)
#or
squares=[]
for value in range(1,11):
squares.append(value**3)
print(squares)
#or
squares=[value**3for value in range(1,11)]
print(squares)
for square in squares:
print(square)
# Homework 4-9: Cube Comprehension
squares=[value**3for value in range(1,11)]
print(squares)
# 2025-3-25-1# 4.4 Using a part of a list# 4.4.1 Slice
players=['charles','martina','michael','florence','eli']
print(players[0:3])
print(players[1:4])
print(players[:4])
print(players[2:])
print(players[-3:])
# 4.4.2 Traversing a slice
players=['charles','martina','michael','florence','eli']
print('Here are the first three players on my team:')
for player in players[:3]:
print(player.title())
# 4.4.3 Copying a list
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)
# Adding food items
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_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)
# Error example
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_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)
# Homework 4-10: Slices
players=['charles','martina','michael','florence','eli']
print("The first three items in the list are:")
print(players[:3])
print("Three items from the middle of the list are:")
print(players[1:4])
print("The last three items in the list are:")
print(players[2:])
# Homework 4-11: Your Pizzas and My Pizzas
my_pizzas=['neapolitan pizza','chicago pizza','pepperoni pizza']
friend_pizzas=pizzas[:]
my_pizzas.append('pizza al taglio')
friend_pizzas.append('california pizza')
print("My favorite pizzas are:")
for my_pizza in my_pizzas:
print(my_pizza)
print("My friend's favorite pizzas are:")
for friend_pizza in friend_pizzas:
print(friend_pizza)
#Homework 4-12: Using Multiple Loops
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
for my_food in my_foods:
print(my_food)
for friend_food in friend_foods:
print(friend_food)
# 4.5 Tuple# 4.5.1 Defining a tuple
dimensions=(200,50)
print(dimensions[0])
print(dimensions[1])
# 4.5.2 Traversing all values in a tuple
dimensions=(200,50)
for dimension in dimensions:
print(dimension)
# 4.5.3 Modifying a tuple variable
dimensions=(200,50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions=(400,10)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
# Homework 4-13: Buffet
foods=('apple','milk','bread','egg','noodle')
for food in foods:
print(food)
#foods[0]='pear'
foods=('pear','milk','bread','egg','porridge')
for food in foods:
print(food)
# 4.6 Formatting code# 5 if statement# 5.1 A simple example
cars=['audi','bmw','subaru','toyota']
for car in cars:
if car=='bmw':
print(car.upper())
else:
print(car.title())
# 5.2 Conditional tests# 5.2.1 Check for equality
car='bmw'if car=='bmw':
print("car=='bmw' is True")
car='audi'if car=='bmw':
print("car=='bmw' is True")
# 5.2.2 Checking for equality without considering case
car='Audi'if car=='audi':
print("car=='audi' is False")
car='Audi'if car.lower()=='audi':
print("car.lower()=='audi' is Ture")
# 5.2.3 Check for inequality
requested_topping='mushrooms'if requested_topping!='anchovies':
print("Hold the anchovies!")
# 2025-3-25-2#5.2.4 Comparing numbers
age=18if age==18:
print("age==18 is True")
answer=17if answer!=42:
print("That is not the correct answer.Please try again!")
age=19if age<21:
print("age<21 is True")
if age<=21:
print("age<=21 is True")
if age>21:
print("age>21 is False")
if age>=21:
print("age>=21 is False")
# 5.2.5 Checking multiple conditions# 1. Using and to check multiple conditions
age_0=22
age_1=18if age_0>=21and age_1>=21:
print("age_0>=21 and age_1>=21 is False")
age_0=22
age_1=22if age_0>=21and age_1>=21:
print("age_0>=21 and age_1>=21 is True.")
# 2.Using or to check multiple conditions
age_0=22
age_1=18if age_0>=21or age_1>=21:
print("age_0>=21 or age_1>=21 is True")
age_0=18
age_1=18if age_0>=21or age_1>=21:
print("age_0>=21 or age_1>=211 is False.")
# 5.2.6 检查特定值是否包含在列表中# *笔记# *2.3.1 使用方法修改字符串大小写# *2.3.1.1 使用方法title()让字符串开头大写
print("\n*2.3.1.1")
name="ada lovelace"
print(name.title())
# *2.3.1.2 使用方法upper()让字符串全部大写
print("\n*2.3.1.2")
name="ada lovelace"
print(name.upper())
# *2.3.1.3 使用方法lower()让字符串全部小写
print("\n*2.3.1.3")
name="ada lovelace"
print(name.lower())
# *2.3.3使用制表符或换行符添加空白# *2.3.3.1 \t:制表符
print("\n*2.3.3.1")
print("Python")
print("\tPython")
# *2.3.3.2 \n:换行符
print("\n*2.3.3.2")
print("Python")
print("Languages:\nPython\nC\nJavaScript")
# *2.3.4 删除空白# *2.3.4.1 剔除末尾空白
print("\n*2.3.4.1")
favorite_language=' Python '
favorite_language=favorite_language.rstrip()
print(favorite_language)
# *2.3.4.2 剔除开头空白
print("\n*2.3.4.2")
favorite_language=' Python '
favorite_language=favorite_language.lstrip()
print(favorite_language)
# *2.3.4.3 剔除字符两端空白
print("\n*2.3.4.3")
favorite_language=' Python '
favorite_language=favorite_language.strip()
print(favorite_language)
# *2.4.3 使用函数str()避免类型错误
print("\n*2.4.3")
age=23
message='Happy '+str(age)+'rd Birthday'
print(message)
# *3.2.1 修改列表元素
print("\n*3.2.1")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)
# *3.2.2 在列表中添加元素# *3.2.2.1 使用方法append()在列表末尾添加元素
print("\n*3.2.2.1")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
# *3.2.2.2 使用方法append()创建空列表
print("\n*3.2.2.2")
motorcycles=[]
print(motorcycles)
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
# *3.2.2.3 使用方法insert()在列表中插入元素
print("\n*3.2.2.3")
motorcycles=['honda','yamaha','suzuki']
motorcycles.insert(0,'ducati')
print(motorcycles)
# *3.2.3 从列表中删除元素# *3.2.3.1 使用del语句删除函数# *3.2.3.1.1 删除首项
print("\n*3.2.3.1.1")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
# *3.2.3.1.2 也可以删除其他项
print("\n*3.2.3.1.2")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)
# *3.2.3.2 使用方法pop()删除元素# *3.2.3.2.1 使用方法pop()删除元素,删除的元素可以保留
print("\n*3.2.3.2.1")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycles=motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)
# *3.2.3.2.2 使用方法pop()删除元素,删除的元素可以保留并使用
print("\n*3.2.3.2.2")
motorcycles=['honda','yamaha','suzuki']
last_owned=motorcycles.pop()
print("The last motorcycle I owned was a "+last_owned.title()+".")
# *3.2.3.2.3 使用方法pop()删除元素,也可以弹出列表中任何位置处的元素
print("\n*3.2.3.2.3")
motorcycles=['honda','yamaha','suzuki']
first_owned=motorcycles.pop(0)
print("The first motorcycle I owned was a "+first_owned.title()+".")
# *3.2.3.3 使用方法remove(),根据值删除元素# *3.2.3.3.1 使用方法remove()以删除元素
print("\n*3.2.3.3.1")
motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
# *3.2.3.3.2 使用方法remove()删除元素的元素可以保留并使用
print("\n*3.2.3.3.2")
motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles)
too_expensive='ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print("A "+too_expensive.title()+" is too expensive for me.")
# **b站AI大模型全栈:100道Python练习题# 001 两数求和
print("\n**001")
number1=20
number2=23
result=number1+number2
print(result)
# 002 求100以内偶数
print("\n**002")
numbers=list(range(2,101,2))
print(numbers)
#003 求100以内奇数
print("\n**003")
numbers=list(range(1,101,2))
print(numbers)
# 004 判断素数# 《Python编程:从入门到实践》埃里克·马瑟斯(2015)# 目录# 第一部分 基础知识# 第1章 起步# 1.1 搭建编程环境# 1.1.1 Python2和Python3# 1.1.2 运行Python代码片段# 1.1.3 Hello World程序# 1.2# 第2章 变量和简单数据类型# 2.1 运行hello_world.py时发生的情况# 第3章 列表简介# 第4章 操作列表# 第5章 if语句# 第6章 字典# 2.2 变量# 作业2-2: 多条简单消息
print("\n2-2")
massage="Explicit is better than implicit."
print(massage)
massage="Simple is better than complex."
print(massage)
# 2.3字符串# 2.3.1 使用方法修改字符串大小写# 2.3.1.1 使用方法title()让字符串开头大写
print("\n2.3.1.1")
name="ada lovelace"
print(name.title())
# 2.3.1.2 使用方法upper()让字符串全部大写
print("\n2.3.1.2")
name="ada lovelace"
print(name.upper())
# 2.3.1.3 使用方法lower()让字符串全部小写
print("\n2.3.1.3")
name="ada lovelace"
print(name.lower())
# 2.3.2 合并(拼接)字符串
print("\n2.3.2")
first_name="ada"
last_name="lovelace"
full_name=first_name+" "+last_name
print(full_name)
# 2.3.3使用制表符或换行符添加空白# 2.3.3.1 \t:制表符
print("\n2.3.3.1")
print("Python")
print("\tPython")
# 2.3.3.2 \n:换行符
print("\n2.3.3.2")
print("Python")
print("Languages:\nPython\nC\nJavaScript")
# 2.3.4 删除空白# 2.3.4.1 剔除末尾空白
print("\n2.3.4.1")
favorite_language=' Python '
favorite_language=favorite_language.rstrip()
print(favorite_language)
# 2.3.4.2 剔除开头空白
print("\n2.3.4.2")
favorite_language=' Python '
favorite_language=favorite_language.lstrip()
print(favorite_language)
# 2.3.4.3 剔除字符两端空白
print("\n2.3.4.3")
favorite_language=' Python '
favorite_language=favorite_language.strip()
print(favorite_language)
# 2.3.5 使用字符串时避免语法错误# 2.3.6 Python2中的print语句# 作业2-3: 个性化消息
print("\n2-3")
name=("Eric")
massage=('Hello'+' '+name+","+"would you like to learn some Python today?")
print(massage)
# 作业2-4: 调整名字的大小写
print("\n2-4")
name=("ada lovelace")
print(name.lower())
print(name.upper())
print(name.title())
# 作业2-5: 名言
print("\n2-5")
Famous_person="Albret Einstein"
Famous_Quote=Famous_person+' '+'once said,'+'"A person who never made a mistake never tried anything new."'
print(Famous_Quote)
# 作业2-6: 名言2
print("\n2-6")
Famous_person="Albret Einstein"
Famous_Quote=Famous_person+' '+'once said,'+'"A person who never made a mistake never tried anything new."'
message=Famous_Quote
print(message)
# 作业2-7: 剔除人名中的空白
print("\n2-7")
name=" \tAlbret \t\nEinstein "
print(name)
print('01')
print(name.lstrip())
print('02')
print(name.rstrip())
print('03')
print(name.strip())
# 2.4 数字# 2.4.1 整数# 2.4.1.1乘方
print("\n2.4.1.1")
print(10**6)
# 2.4.2 浮点数
print("\n2.4.2")
print(0.2+0.1)
print(3*0.1)
# 2.4.3 使用函数str()避免类型错误
print("\n2.4.3")
age=23
message='Happy '+str(age)+'rd Birthday'
print(message)
# 2.4.4 Python2中的整数# 作业2-8:数字8
print("\n2-8")
print('01')
print(3.1415926+4.8584074)
print('02')
print(10.1-2.1)
print('03')
print(2**3)
print('04')
print(5.6/0.7)
# 作业2-9:最喜欢的数字
print("\n2-9")
favorite_number=6
massage="My favorite number is "+str(favorite_number)
print(massage)
# 2.5 注释# 2.6 Python之禅# 这些原则是Python编程哲学的核心,强调代码的可读性、简洁性和实用性。
print("\n2.6")
import this
# 3 列表基础# 3.1 列表是什么
print("\n3.1")
bicycles=["trek","cannondale","redline",'specialized']
print(bicycles)
# 3.1.1 访问列表元素
print("\n3.1.1")
print(bicycles[1])
print(bicycles[1].title())
# 3.1.2 索引从0而不是1开始# 3.1.3 使用列表中的各个值
print("\n3.1.3")
bicycles=["trek","cannondale","redline",'specialized']
massage="My first bicycle was a "+bicycles[0].title()+"."
print(massage)
# 作业3-1: 名字
print("\n3-1")
name=["Mkie","John","Mary"]
print(name[0])
print(name[1])
print(name[2])
# 作业3-2: 问候语
print("\n3-2")
massage='Hello, '+name[0]+' !'
print(massage)
massage='Hello, '+name[1]+' !'
print(massage)
massage='Hello, '+name[2]+' !'
print(massage)
# 作业3-3: 自己的列表
print("\n3-3")
My_favorite_mode_of_commuting=["Walk","cycle","ride an electric bike"]
message='I like to '+My_favorite_mode_of_commuting[0]+' to work.'
print(message)
message='I like to '+My_favorite_mode_of_commuting[1]+' to work.'
print(message)
message='I like to '+My_favorite_mode_of_commuting[2]+' to work.'
print(message)
# 3.2 修改、添加和删除元素# 3.2.1 修改列表元素
print("\n3.2.1")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)
# 3.2.2 在列表中添加元素# 3.2.2.1 使用方法append()在列表末尾添加元素
print("\n3.2.2.1")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
# 3.2.2.2 使用方法append()创建空列表
print("\n3.2.2.2")
motorcycles=[]
print(motorcycles)
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
# 3.2.2.3 使用方法insert()在列表中插入元素
print("\n3.2.2.3")
motorcycles=['honda','yamaha','suzuki']
motorcycles.insert(0,'ducati')
print(motorcycles)
# 3.2.3 从列表中删除元素# 3.2.3.1 使用del语句删除函数# 3.2.3.1.1 删除首项
print("\n3.2.3.1.1")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
# 3.2.3.1.2 也可以删除其他项
print("\n3.2.3.1.2")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)
# 3.2.3.2 使用方法pop()删除元素# 3.2.3.2.1 使用方法pop()删除元素,删除的元素可以保留
print("\n3.2.3.2.1")
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycles=motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)
# 3.2.3.2.2 使用方法pop()删除元素,删除的元素可以保留并使用
print("\n3.2.3.2.2")
motorcycles=['honda','yamaha','suzuki']
last_owned=motorcycles.pop()
print("The last motorcycle I owned was a "+last_owned.title()+".")
# 3.2.3.2.3 使用方法pop()删除元素,也可以弹出列表中任何位置处的元素
print("\n3.2.3.2.3")
motorcycles=['honda','yamaha','suzuki']
first_owned=motorcycles.pop(0)
print("The first motorcycle I owned was a "+first_owned.title()+".")
# 3.2.3.3 使用方法remove(),根据值删除元素# 3.2.3.3.1 使用方法remove()以删除元素
print("\n3.2.3.3.1")
motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
# 3.2.3.3.2 使用方法remove()删除元素的元素可以保留并使用
print("\n3.2.3.3.2")
motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles)
too_expensive='ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print("A "+too_expensive.title()+" is too expensive for me.")
# 作业3-4 嘉宾名单
print("\n3-4")
names=['mike','john','mary']
print(names[0].title()+", I'm inviting you to dinner.")
print(names[1].title()+", I'm inviting you to dinner.")
print(names[2].title()+", I'm inviting you to dinner.")
# 作业3-5 修改嘉宾名单
print("\n3-5")
names=['mike','john','mary']
print(names[0].title()+" can't make it.")
del names[0]
names.insert(0,'robert')
print(names[0].title()+", I'm inviting you to dinner.")
print(names[1].title()+", I'm inviting you to dinner.")
print(names[2].title()+", I'm inviting you to dinner.")
# 作业3-6 添加嘉宾
print("\n3-6")
names=['mike','john','mary']
print("I've found a bigger dinner table.")
names.insert(0,'robert')
names.insert(2,'emma')
names.append('ava')
print(names)
print(names[0].title()+", I'm inviting you to dinner.")
print(names[1].title()+", I'm inviting you to dinner.")
print(names[2].title()+", I'm inviting you to dinner.")
print(names[3].title()+", I'm inviting you to dinner.")
print(names[4].title()+", I'm inviting you to dinner.")
print(names[5].title()+", I'm inviting you to dinner.")
# 作业3-7 缩减名单
print("\n3-7")
names=['robert', 'mike', 'emma', 'john', 'mary', 'ava']
print("I can only invite two guests to dinner.")
print(names.pop().title()+",I'm sorry, but I can't invite you to dinner.")
print(names.pop().title()+",I'm sorry, but I can't invite you to dinner.")
print(names.pop().title()+",I'm sorry, but I can't invite you to dinner.")
print(names.pop().title()+",I'm sorry, but I can't invite you to dinner.")
print(names[0].title()+",you're still on the guest list.")
print(names[1].title()+",you're still on the guest list.")
del names[0]
del names[0]
print(names)
# 3.3 组织列表# 3.3.1 使用方法sort()对列表进行永久性排序# 3.3.1.1 与字母顺序相同:.sort()
print("\n3.3.1.1")
cars=['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
# 3.3.1.2 与字母顺序相反:.sort(reverse=True)
print("\n3.3.1.1")
cars=['bmw','audi','toyota','subaru']
cars.sort(reverse=True)
print(cars)
# 3.3.2 使用函数sorted()对列表进行临时排序# 3.3.2.1 与字母顺序相同:sorted()
print("\n3.3.2.1")
cars=['bmw','audi','toyota','subaru']
print("Here is the original list:")
print(cars)
print("Here is the sorted list:")
print(sorted(cars))
print("Here is the original list again:")
print(cars)
# 3.3.2.2 与字母顺序相反:sorted(,reverse=True)
print("\n3.3.2.2")
cars=['bmw','audi','toyota','subaru']
print("Here is the original list:")
print(cars)
print("Here is the sorted(,reverse=True) list:")
print(sorted(cars,reverse=True))
print("Here is the original list again:")
print(cars)
# 3.3.3 倒着打印列表# 3.3.3.1 使用方法reverse()倒着打印列表
print("\n3.3.3.1")
cars=['bmw','audi','toyota','subaru']
print(cars)
cars.reverse()
print(cars)
# 3.3.3.2 再次使用方法reverse()以还原列表
print("\n3.3.3.2")
cars=['bmw','audi','toyota','subaru']
print(cars)
cars.reverse()
print(cars)
cars.reverse()
print(cars)
# 3.3.4 使用函数len()确定列表的长度
print("\n3.3.4")
cars=['bmw','audi','toyota','subaru']
length=len(cars)
print(length)
# 作业3-8 放眼望世界
print("\n3-8")
print("01")
locations=['beijing','iceland','new york','egypt','tokyo']
print(locations)
print("02")
locations=['beijing','iceland','new york','egypt','tokyo']
print(sorted(locations))
print(locations)
print("03")
locations=['beijing','iceland','new york','egypt','tokyo']
print(sorted(locations,reverse=True))
print(locations)
print("04")
locations=['beijing','iceland','new york','egypt','tokyo']
locations.reverse()
print(locations)
locations.reverse()
print(locations)
print("05")
locations=['beijing','iceland','new york','egypt','tokyo']
locations.sort()
print(locations)
locations=['beijing','iceland','new york','egypt','tokyo']
locations.sort(reverse=True)
print(locations)
# 作业3-9 晚餐嘉宾
print("\n3-9")
names=['robert', 'mike', 'emma', 'john', 'mary', 'ava']
print("I have invited "+str(len(names))+" guests to join me for dinner.")
# 3-10 尝试使用各个函数
print("\n3-10")
print("01 修改元素")
names=['robert', 'mike', 'emma', 'john', 'mary', 'ava']
names[0]='locy'
print(names)
print("02 添加元素")
names=['robert', 'mike', 'emma', 'john', 'mary', 'ava']
names.append('locy')
print(names)
print("02 添加元素")
names=['robert', 'mike', 'emma', 'john', 'mary', 'ava']
names.insert(1,'locy')
print(names)
print("03 删除元素")
names=['robert', 'mike', 'emma', 'john', 'mary', 'ava']
del names[0]
print(names)
print("03 删除元素")
names=['robert', 'mike', 'emma', 'john', 'mary', 'ava']
del names[0]
print(names)