4-1
pizzas
= [
'New York Style',
'Chicago Style',
'California Style']
for pizza
in pizzas:
print(pizza)
print(
"I like "
+ pizza
+
" pizza")
print(
"I really love", pizzas[
0],
',', pizzas[
1],
"and", pizzas[
2],
"pizzas!")
4-2
animals
= [
'cat',
'dog',
'frog']
for animal
in animals:
print(animal)
print(
'a', animal,
'would make a great pet.')
print(
'Any of these animals would make a great pet!')
4-3
for numb
in
range(
1,
21):
print(numb)
4-4
numbs
= [numb
for numb
in
range(
1,
1000001)]
for numb
in numbs:
print(numb)
4-5
numbs
= [numb
for numb
in
range(
1,
1000001)]
print(
min(numbs),
max(numbs),
sum(numbs))
4-6
primes
= [num
for num
in
range(
1,
20,
2)]
for num
in primes:
print(num)
4-7
nums
= []
for num
in
range(
3,
31):
if num
%
3
==
0:
nums.append(num)
for num
in nums:
print(num)
4-8
nums
= [num
**
3
for num
in
range(
1,
11)]
for num
in nums:
print(num)
4-9
nums
= [num
**
3
for num
in
range(
1,
11)]
for num
in nums:
print(num)
4-10
items
=
[
'
pizza
',
'
falafel
',
'
cannoli
',
'
ice cream
',
'
carrot cake
'
]
print
(
"
The first three items in the list are
"
, items[:
3
])
print
(
"
Three items from the middle of the list are
"
, items[
1
:
4
])
print
(
"
The last three items in the list are
"
, items[
-
3
:])
4-11
pizzas
=
[
'
New York Style
',
'
Chicago Style
',
'
California Style
'
]
friend_pizzas
= pizzas
[:
]
pizzas.
append
(
'
Pan pizza
'
)
friend_pizzas.
append
(
'
Thick style
'
)
print
(
'
My favorite pizzas are
'
,
end
=
'
'
)
for pizza
in pizzas:
print
(pizza,
end
=
'
'
)
print
(
"
My friend's favorite pizzas are
"
,
end
=
'
'
)
for pizza
in friend_pizzas:
print
(pizza,
end
=
'
'
)
4-12
my_foods
=
[
'
pizza
',
'
falafel
',
'
carrot cake
'
]
for food
in my_foods:
print
(food)
4-13
foods
= (
'
pizza
',
'
falafel
',
'
carrot cake
',
'
cannoli
',
'
ice cream
')
for food
in foods:
print
(food)
#food[0] = 'cake'
foods
= (
'
pan pizza
',
'
New York style pizza
',
'
falafel
',
'
carrot cake
',
'
cannoli
',
'
ice cream
')
for food
in foods:
print
(food)