Python · Lists
Lists Operations
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — List Operations
Now that you know how to add, remove, and analyze lists, let's learn some useful operations you can perform on them! In this chapter, we'll explore powerful tools for working with lists:
•
min()
and
max()
- Find the smallest and largest values •
sum()
- Add up all numbers in a list •
+
operator - Combine lists together •
*
operator - Repeat lists multiple times
These operations will make working with your Codémon teams much easier and more powerful!
Exercise 2 — min(), max(), and sum()
Python provides built-in functions to perform calculations on lists: -
min()
- Returns the smallest item -
max()
- Returns the largest item -
sum()
- Adds up all numbers in the list
# List of Codémon levels levels = [5, 12, 8, 23, 16] print("Lowest level:", min(levels)) print("Highest level:", max(levels)) print("Total levels:", sum(levels)) print("Average level:", sum(levels) / len(levels))
sum() only works with numbers
While
min()
and
max()
work with strings (alphabetically),
sum()
only works with numbers. Trying to sum strings will cause an error.
Exercise 3 — Using min(), max(), and sum()
What will be the output of the following code?
power_levels = [10, 25, 15, 30] print(sum(power_levels) - min(power_levels))
Options
• 70
• 80
• 50
• 20
Hint : Remember: sum() adds all values, min() finds the smallest. Calculate sum first, then subtract min.
Exercise 4 — List Concatenation with +
You can combine two or more lists using the
+
operator. This creates a new list containing all elements from the lists being combined, in order.
# Two teams of Codémon team_a = ["Aquachu", "Florachu"] team_b = ["Pyrochu", "Rockachu"] # Combine the teams combined_team = team_a + team_b print(combined_team) # You can add more than two lists team_c = ["Windchu"] all_teams = team_a + team_b + team_c print(all_teams)
Concatenation vs. Append
Remember that
+
creates a new list, it doesn't modify the original lists. If you want to modify an existing list, you could use the
append()
or
extend()
methods instead.
Exercise 5 — List Concatenation Results
What will be the result of the following code?
list1 = [1, 2, 3] list2 = [4, 5] list3 = list1 + list2 print(list3)
Options
• [1, 2, 3, 4, 5]
• [[1, 2, 3], [4, 5]]
• [5, 7, 8]
• [1, 2, 3, [4, 5]]
Hint : Remember: The + operator joins lists by adding all elements from the second list after the elements of the first list.
Exercise 6 — List Repetition with *
The
*
operator allows you to repeat a list multiple times. When you multiply a list by a number, it creates a new list with the original elements repeated that many times.
# A starting team with one Codémon starter_team = ["Aquachu"] # Create a team with 3 of the same Codémon triple_team = starter_team * 3 print(triple_team) # This also works with multi-element lists basic_party = ["Potion", "Revive"] full_inventory = basic_party * 2 print(full_inventory)
Exercise 7 — List Repetition Results
What will be the output of the following code?
items = ["Potion", "Ball"] print(items * 3)
Options
• ["Potion", "Ball", "Potion", "Ball", "Potion", "Ball"]
• ["PotionPotionPotion", "BallBallBall"]
• [["Potion", "Ball"], ["Potion", "Ball"], ["Potion", "Ball"]]
• ["Potion", "Ball", 3]
Hint : Remember: The * operator with a list repeats the entire list content the specified number of times.
Exercise 8 — Matching List Operations
Match each operation with its correct result.
Items to match
• [1, 2, 3] + [4, 5]
• ["a", "b"] * 2
• sum([5, 10, 15])
• max([5, 2, 8, 1])
Definitions
• [1, 2, 3, 4, 5]
• ["a", "b", "a", "b"]
• 30
• 8
Exercise 9 — Combining Operations
You're preparing Codémon teams for a tournament. What will be the final result?
team_a = ["Aquachu", "Florachu"] team_b = ["Pyrochu"] full_team = team_a + team_b final_team = full_team * 2 print(len(final_team))
Options
• 6
• 3
• 4
• 5
Hint : Remember: First combine the lists with +, then multiply with *, finally count with len().
Exercise 10 — Challenge - Team Power Calculator
You're analyzing Codémon teams for a tournament! Use list operations to complete these tasks: 1. Combine your two teams into one big team 2. Calculate the total power of all Codémon 3. Find the weakest Codémon (minimum power) 4. Find the strongest Codémon (maximum power) 5. Create a backup team by repeating team_a twice Use
+
,
*
,
sum()
,
min()
, and
max()
to complete this challenge!
Starter code
# Two Codémon teams with their power levels team_a = [15, 22, 18] team_b = [30, 12, 25] print("Team A:", team_a) print("Team B:", team_b) print("=" * 50) # Task 1: Combine both teams # Task 2: Calculate total power # Task 3: Find weakest Codémon # Task 4: Find strongest Codémon # Task 5: Create backup team (team_a repeated twice)
Back to the course