python
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
print('-' * 100)
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print('-' * 100)
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you, everyone. That was a great magic show!")alice
david
carolina
----------------------------------------------------------------------------------------------------
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
----------------------------------------------------------------------------------------------------
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you, everyone. That was a great magic show!
python
# 4.1
pizza_names = ['pepperoni', 'lalala', 'xixixi', 'hahaha']
for pizza_name in pizza_names:
print(f"I like {pizza_name} pizza.")
print("I really love pizza!")I like pepperoni pizza.
I like lalala pizza.
I like xixixi pizza.
I like hahaha pizza.
I really love pizza!
python
# 4.2
pets = ['cat', 'dog']
for pet in pets:
print(f"A {pet} would make a great pet.")
print("Any of these animals would make a great pet!")A cat would make a great pet.
A dog would make a great pet.
Any of these animals would make a great pet!
python
for value in range(1, 5):
print(value)
print('-'*100)
for value in range(5):
print(value)1
2
3
4
----------------------------------------------------------------------------------------------------
0
1
2
3
4
python
numbers = list(range(1, 6))
print(numbers)[1, 2, 3, 4, 5]
python
even_numbers = list(range(2, 11, 2))
print(even_numbers)[2, 4, 6, 8, 10]
python
squares = []
for value in range(1, 11):
squares.append(value ** 2)
print(squares)
squares = [value ** 2 for value in range(1, 11)]
print(squares)[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
python
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))0
9
45
python
# 4.3
for i in range(1, 21):
print(i, end=' ')1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
python
# 4.4
list_of_wan = [i for i in range(1, 1000001)]
for i in list_of_wan:
print(i, end=' ')略
python
# 4.5
list_of_wan = [i for i in range(1, 1000001)]
print(min(list_of_wan))
print(max(list_of_wan))
print(sum(list_of_wan))1
1000000
500000500000
python
# 4.6
odds = [odd for odd in range(1, 20, 2)]
for odd in odds:
print(odd, end=' ')1 3 5 7 9 11 13 15 17 19
python
# 4.7
list_of_three = [i for i in range(3, 31) if i % 3 == 0]
print(list_of_three)
list_of_three = [i if i % 3 == 0 else 0 for i in range(3, 31)]
print(list_of_three)[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[3, 0, 0, 6, 0, 0, 9, 0, 0, 12, 0, 0, 15, 0, 0, 18, 0, 0, 21, 0, 0, 24, 0, 0, 27, 0, 0, 30]
python
# 4.8
list_of_tri = []
for i in range(1, 11):
list_of_tri.append(i ** 3)
for i in list_of_tri:
print(i, end=' ')1 8 27 64 125 216 343 512 729 1000
python
# 4.9
list_of_tri = [i ** 3 for i in range(1, 11)]
for i in list_of_tri:
print(i, end=' ')1 8 27 64 125 216 343 512 729 1000
python
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
print(players[1:4])
print(players[:4])
print(players[2:])
print(players[-3:])
print(players[::2])
print('-' * 100)
for player in players[:3]:
print(player.title(), end=' ')['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']
['michael', 'florence', 'eli']
['michael', 'florence', 'eli']
['charles', 'michael', 'eli']
----------------------------------------------------------------------------------------------------
Charles Martina Michael
python
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)
print('-' * 100)
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)My favorite foods are:
['pizza', 'falafel', 'carrot cake']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']
----------------------------------------------------------------------------------------------------
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
python
# 4.10
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("The first three items in the list are:")
print(players[:3])
print(players[1:4])
print(players[-3:])The first three items in the list are:
['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['michael', 'florence', 'eli']
python
# 4.11
pizza_names = ['pepperoni', 'lalala', 'xixixi', 'hahaha']
friend_pizzas = pizza_names[:]
pizza_names.append('aaa')
friend_pizzas.append('bbb')
print("My favorite pizzas are:")
print(pizza_names)
print("My friend's favorite pizzas are:")
print(friend_pizzas)My favorite pizzas are:
['pepperoni', 'lalala', 'xixixi', 'hahaha', 'aaa']
My friend's favorite pizzas are:
['pepperoni', 'lalala', 'xixixi', 'hahaha', 'bbb']
python
# 4.12
my_foods = ['pizza', 'falafel', 'carrot cake']
for food in my_foods:
print(food)pizza
falafel
carrot cake
python
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
for dimension in dimensions:
print(dimension, end=' ')200
50
200 50
python
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)Original dimensions:
200
50
Modified dimensions:
400
100
python
# 4.13
foods = ('a', 'b', 'c', 'd', 'e',)
for food in foods:
print(food, end=' ')
# foods[0] = 'f'
print()
foods = ('f', 'b', 'c', 'd', 'g',)
for food in foods:
print(food, end=' ')a b c d e
f b c d g