Think of at least three kinds of your favorite pizza. Store these pizza names in a list, and then use a for loop to print the name of each pizza.
Modify your for loop to print a sentence using the name of the pizza, instead of printing just the name of the pizza. For each pizza, you should have one line of output containing a simple statement like I like pep- peroni pizza.
pizzas = ['cheese pizza','tikka pizza','fajita']for pizza in pizzas:print(f" I like {pizza.title()}. Please order it for me")
I like Cheese Pizza. Please order it for me
I like Tikka Pizza. Please order it for me
I like Fajita. Please order it for me
Add a line at the end of your program, outside the for loop, that states how much you like pizza. The output should consist of three or more lines about the kinds of pizza you like and then an additional sentence, such as I really love pizza!
for pizza in pizzas:print(f" I like {pizza.title()}. Please order it for me")print(3*"I cannot live without pizza\n")
I like Cheese Pizza. Please order it for me
I like Tikka Pizza. Please order it for me
I like Fajita. Please order it for me
I cannot live without pizza
I cannot live without pizza
I cannot live without pizza
4-2 Animals:
Think of at least three different animals that have a common char- acteristic. Store the names of these animals in a list, and then use a for loop to print out the name of each animal
Animals= ['cat','dog','buffalow']for animal in Animals:print(animal)
cat
dog
buffalow
Modify your program to print a statement about each animal, such as A dog would make a great pet.
for animal in Animals:print(f"A {animal} would make a great pet")
A cat would make a great pet
A dog would make a great pet
A buffalow would make a great pet
Add a line at the end of your program, stating what these animals have in common. You could print a sentence, such as Any of these animals would make a great pet!
for animal in Animals:print(f"A {animal} 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
A buffalow would make a great pet
Any of these animals would make a great pet!
4-3 Counting to Twenty:
Use a for loop to print the numbers from 1 to 20, inclusive
Make a list of the numbers from one to one million, and then use a for loop to print the numbers. (If the output is taking too long, stop it by pressing CTRL-C or by closing the output window.)
We will only use 100 here for convenience.
millions_list =list(range(1,100))for number in millions_list:print(number)
Make a list of the numbers from one to one million, and then use min() and max() to make sure your list actually starts at one and ends at one million. Also, use the sum() function to see how quickly Python can add a million numbers.
millions_list =list(range(1,1000_000))# sum of millions_listsum(millions_list)# min of millions_listmin(millions_list)# max of millions_listmax(millions_list)
999999
4-6 Odd Numbers:
Use the third argument of the range() function to make a list of the odd numbers from 1 to 20. Use a for loop to print each number.
for value inrange(1,20,2):print(value)
1
3
5
7
9
11
13
15
17
19
4-7 Threes:
Make a list of the multiples of 3, from 3 to 30. Use a for loop to print the numbers in your list.
for multiple inrange(3,30,3):print(multiple)
3
6
9
12
15
18
21
24
27
4-8. Cubes:
A number raised to the third power is called a cube. For example, the cube of 2 is written as 2**3 in Python. Make a list of the first 10 cubes (that is, the cube of each integer from 1 through 10), and use a for loop to print out the value of each cube.
for value inrange(1,10):print(value**3)
1
8
27
64
125
216
343
512
729
4-9. Cube Comprehension:
Use a list comprehension to generate a list of the first 10 cubes.
cubes = [value**3for value inrange(1,11)]cubes
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
4-10. Slices:
Using one of the programs you wrote in this chapter, add several lines to the end of the program that do the following:
Print the message The first three items in the list are:. Then use a slice to print the first three items from that program’s list.
print(f"The first three items in the list are {cubes[:3]})")
The first three items in the list are [1, 8, 27])
Print the message Three items from the middle of the list are:. Then use a slice to print three items from the middle of the list.
# for finding middle of list we will divide its length by 2middle_of_cubes =len(cubes)/2f"answer is 5.5 so our list is of odd elements"f"Three items from middle of the list are: {cubes[4:7]}"
'Three items from middle of the list are: [125, 216, 343]'
Print the message The last three items in the list are:. Then use a slice to print the last three items in the list.
f"last three items in the list are {cubes[-3:]}"
'last three items in the list are [512, 729, 1000]'
4-11. My Pizzas, Your Pizzas:
Start with your program from Exercise 4-1 (page 56). Make a copy of the list of pizzas, and call it friend_pizzas. Then, do the following:
Prove that you have two separate lists. Print the message My favorite pizzas are:, and then use a for loop to print the first list. Print the message My friend’s favorite pizzas are:, and then use a for loop to print the second list. Make sure each new pizza is stored in the appropriate list.
f"My favorite pizzas are {[pizza for pizza in pizzas]}"f"My friend's favourite pizzas are {[pizza for pizza in friend_pizzas]}"
"My friend's favourite pizzas are ['cheese pizza', 'tikka pizza', 'fajita', 'Afghani pizza', 'vegetarian pizza']"
4-12. More Loops:
All versions of foods.py in this section have avoided using for loops when printing, to save space. Choose a version of foods.py, and write two for loops to print each list of foods.
for cube in cubes:print(cube)
1
8
27
64
125
216
343
512
729
1000
4-13. Buffet:
A buffet-style restaurant offers only five basic foods. Think of five simple foods, and store them in a tuple.
pizzas =tuple(pizzas)for piz in pizzas:print(piz)
cheese pizza
tikka pizza
fajita
Afghani pizza
Try to modify one of the items, and make sure that Python rejects the change.
pizzas[3] = “vegi pizza”
TypeError: 'tuple' object does not support item assignment
The restaurant changes its menu, replacing two of the items with different foods. Add a line that rewrites the tuple, and then use a for loop to print each of the items on the revised menu.
pizzasnew_menu_of_pizzas =tuple(friend_pizzas)new_menu_of_pizzasfor piz in new_menu_of_pizzas:print(piz)
Look through the original PEP 8 style guide at https://python.org/ dev/peps/pep-0008. You won’t use much of it now, but it might be interesting to skim through it.
4-15. Code Review:
Choose three of the programs you’ve written in this chapter and modify each one to comply with PEP 8.
Already followed it
Use four spaces for each indentation level. Set your text editor to insert four spaces every time you press the TAB key, if you haven’t already done so (see Appendix B for instructions on how to do this).
VS code configured for 4 spaces = tab key
Use less than 80 characters on each line, and set your editor to show a vertical guideline at the 80th character position.
In the VS code press ctrl+shift+P on Windows or cmd_shift+P on MacOS and select Preferences (UI settings) as shown below
Then write rulers in search bar and below window appears
Click on settings.json
and write in new line as shown below
save the settings.json and that’s it. You will get this kind of line in VS code