python Programming: from introduction to practice exercises 4-1 ~ 4-12

Posted by pneudralics on Tue, 18 Jan 2022 10:58:53 +0100

 

 

4-1 pizza: think of at least three pizzas you like, store their names in a list, and then use the for loop to print out the names of each pizza.

Modify this for Loop so that it prints a sentence containing the name of the pizza, not just the name of the pizza. For each pizza, one line of output is displayed, such as "I like pepperoni pizza" .
pizzas = ["chicken pizza", "Beef Pizza", "vegetable pizza"]
for pizza in pizzas:
    print("I like "+ pizza.title() + "!")
I like Chicken Pizza!
I like Beef Pizza!
I like Vegetable Pizza!
Add a line of code at the end of the program, which is not in for In the loop, point out how much you like pizza. The output should contain a message for each pizza and a concluding sentence, such as "I really love pizza!" .
pizzas = ["chicken pizza", "Beef Pizza", "vegetable pizza"]
for pizza in pizzas:
    print("I like "+ pizza.title() + "!")
print("I really love pizza!")
I like Chicken Pizza!
I like Beef Pizza!
I like Vegetable Pizza!
I really love pizza!

4-2 animals: Think of at least three animals with common characteristics, store the names of these animals in a list, and then use the for loop to print out the names of each animal.

Modify this program to print a sentence for each animal, such as "adogwould make a great pet". Add a line of code at the end of the program to point out what these animals have in common, such as printing such as "any of the animals would make a great pet!" Such a sentence.

animals = ["cat", "dog", "pig"]
for animal in animals:
    print("A "+ animal.title() + " 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 Pig would make a great pet.
Any of these animals would make a great pet!

Process finished with exit code 0

4-3 to 20: Use one for Cycle print numbers 1~20 (including).
for number in range(1,21):
    print(number, end=" ")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
Process finished with exit code 0

4-4 million: Create a list that contains numbers 1~1 000 000 , use another one for Cycle to print out these numbers (press if the output time is too long) Ctrl+ C Stop the output, or close the output window).
numbers = list(range(1,1000001))
print(numbers)
[1, 2, 3, 4, 5, 6, 7, 8, 9, ..., 1000000]

Process finished with exit code 0
4-5 calculate the sum of 1 ~ 1 000 000: Create a list that contains numbers 1~1 000 000 , reuse min() and max() Verify that the list is indeed from 1 Start, to 1 000 000 It's over. Also, for this list
Call function sum() , look Python How long does it take to add up a million numbers.
numbers = list(range(1,1000001))
print(min(numbers),max(numbers))
print(sum(numbers))
1 1000000
500000500000

Process finished with exit code 0
4-6 odd: By giving a function range() Specify the third parameter to create a list that contains 1~20 Odd number of; Use another one for Loop to print out these numbers.
A = list(range(1, 21, 2))
for a in A:
    print(a, end=" ")
1 3 5 7 9 11 13 15 17 19 
Process finished with exit code 0
Multiples of 4-7 3: Create a list that contains 3~30 Internal energy 3 Divisible number; Use another one for The loop prints out all the numbers in the list.
A = list(range(3, 31, 3))
for a in A:
    print(a, end=" ")
3 6 9 12 15 18 21 24 27 30 

Process finished with exit code 0
4-8 cubic meters: Multiplying the same number three times is called cubic. For example, in Python In, 2 Cube for 2**3 express. Please create a list that contains the previous 10 Integer (i.e 1~10 )Cube, use another one for Follow
The ring prints out all these cubes.
B = []
for b in range(1, 11):
    i = b**3
    B.append(i)
print(B)
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

Process finished with exit code 0

4-9 cubic analysis: Use list parsing to generate a list that contains the previous 10 A cube of integers.
C = [c**3 for c in range(1,11)]
print(C)
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

Process finished with exit code 0
4-10 slice: Select a program you wrote in this chapter and add a few lines of code at the end to complete the following tasks.
Print the message "the first threeitems in thelistare:" Then use the slice to print the first three elements of the list.
Print the message "three items from the middle of the list:" Then use the slice to print the three elements in the middle of the list.
Print the message "Thelast threeitems in thelistare:" Then use the slice to print the three elements at the end of the list.
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("The first three items in the list are :" )
print(players[0:3])
print("\nThree items from the middle of the list are:")
print(players[2:5])
print("\nThe last three items in the list are:")
print(players[-3:])
The first three items in the list are :
['charles', 'martina', 'michael']

Three items from the middle of the list are:
['michael', 'florence', 'eli']

The last three items in the list are:
['michael', 'florence', 'eli']

Process finished with exit code 0

4-11 your pizza my pizza: Before you finish the exercise 4-1 In the program written, create a copy of the pizza list and store it in a variable friend_pizzas And then complete the following tasks. Add a pizza to the original pizza list.
In the list friend_pizzas Add another pizza.
Verify that you have two different lists. To do this, print the message "my favorite pizza are:" , use another one for Loop to print the first list; Print message "My friend's favorite pizzasare:" , use another individual for Loop to print the second list. Verify that the new pizza is added to the correct list.
pizzas = ["chicken pizza", "Beef Pizza", "vegetable pizza"]
for pizza in pizzas:
    print("I like "+ pizza.title() + "!")

friend_pizzas = pizzas[:]
pizzas.append("susi")
friend_pizzas.append("juice")
print("\nMy favorite pizzas are:")
for pizza in pizzas:
    print(pizza)
print("\nMy friend's favorite pizzasare:")
for friend_pizza in friend_pizzas:
    print(friend_pizza)
I like Chicken Pizza!
I like Beef Pizza!
I like Vegetable Pizza!

My favorite pizzas are:
chicken pizza
Beef Pizza
vegetable pizza
susi

My friend's favorite pizzasare:
chicken pizza
Beef Pizza
vegetable pizza
juice

Process finished with exit code 0
4-12 use multiple cycles: In this section, to save space, the program foods.py Each version of is not used for Loop to print the list. Please select a version foods.py , write two of them for Cycle to
Print out all the food lists.
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
for my_food in my_foods:
    print(my_food)
print("\nMy friend's favorite foods are:")
for friend_food in friend_foods:
    print(friend_food)
My favorite foods are:
pizza
falafel
carrot cake

My friend's favorite foods are:
pizza
falafel
carrot cake

Process finished with exit code 0
4-13 buffet: There is a cafeteria that serves only five simple foods. Please think of five simple foods and store them in a tuple.
Use a for Cycle to print out all five kinds of food provided by the restaurant.
Try modifying one of the elements to verify Python I will refuse you to do so.
The restaurant adjusted its menu to replace two of the foods it offered. Write a code block that assigns a value to a tuple variable and uses a for The loop prints out each element of the new element group.
foods = ("apple", "banana", "susi", "orange", "juice")
for food in foods:
    print(food)
# food[0] = "fish" prompt error

foods = ("fish", "beef", "susi", "orange", "juice")
for food in foods:
    print(food)
apple
banana
susi
orange
juice
fish
beef
susi
orange
juice

Process finished with exit code 0

Topics: Python