Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Loop Techniques | Section 2
Python - Sport
course content

Course Content

Python - Sport

Python - Sport

1. Section 1
2. Section 2

book
Loop Techniques

Nested loops are loops within loops, particularly useful when dealing with multi-dimensional data, such as a list of lists. Imagine you have a dataset representing scores from multiple games, where each game has scores for both teams.

123456789101112
games_scores = [ [24, 21], # Game 1: Team A vs Team B [30, 27], # Game 2: Team A vs Team B [17, 20] # Game 3: Team A vs Team B ] # Calculate the total score for each game for game in games_scores: total_score = 0 for score in game: total_score += score print(f"Total score for the game: {total_score}")
copy

In this example, the outer loop iterates over each game, while the inner loop iterates over the scores within that game to calculate the total score.

List comprehensions provide a concise way to create lists, often more readable and efficient than traditional loops. You can use them to perform operations on each element of a list and generate a new list. Suppose you want to calculate the average score for each game from the games_scores dataset:

12345678
games_scores = [ [24, 21], [30, 27], [17, 20] ] average_scores = [sum(game) / len(game) for game in games_scores] print(f"Average scores for each game: {average_scores}")
copy

This list comprehension iterates over each game, calculates the average score, and stores the result in a new list.

You can also use nested loops within list comprehensions to perform more complex operations. For example, if you want to flatten a list of lists into a single list of scores:

python
Task

Swipe to start coding

Complete a function filter_high_scoring_games that filters a list of game records to find all games where the total score exceeded a given threshold. Use list comprehensions to achieve this efficiently.

Inputs:

  1. games: A list of dictionaries, each representing a game with keys such as away_score and home_score.
  2. score_threshold: An integer representing the score threshold.

Steps:

  1. Filter Games: Use a list comprehension to iterate over the list of games and filter out those where the combined score of away_score and home_score exceeds score_threshold.
  2. Return Filtered List: Return the list of filtered games.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
toggle bottom row

book
Loop Techniques

Nested loops are loops within loops, particularly useful when dealing with multi-dimensional data, such as a list of lists. Imagine you have a dataset representing scores from multiple games, where each game has scores for both teams.

123456789101112
games_scores = [ [24, 21], # Game 1: Team A vs Team B [30, 27], # Game 2: Team A vs Team B [17, 20] # Game 3: Team A vs Team B ] # Calculate the total score for each game for game in games_scores: total_score = 0 for score in game: total_score += score print(f"Total score for the game: {total_score}")
copy

In this example, the outer loop iterates over each game, while the inner loop iterates over the scores within that game to calculate the total score.

List comprehensions provide a concise way to create lists, often more readable and efficient than traditional loops. You can use them to perform operations on each element of a list and generate a new list. Suppose you want to calculate the average score for each game from the games_scores dataset:

12345678
games_scores = [ [24, 21], [30, 27], [17, 20] ] average_scores = [sum(game) / len(game) for game in games_scores] print(f"Average scores for each game: {average_scores}")
copy

This list comprehension iterates over each game, calculates the average score, and stores the result in a new list.

You can also use nested loops within list comprehensions to perform more complex operations. For example, if you want to flatten a list of lists into a single list of scores:

python
Task

Swipe to start coding

Complete a function filter_high_scoring_games that filters a list of game records to find all games where the total score exceeded a given threshold. Use list comprehensions to achieve this efficiently.

Inputs:

  1. games: A list of dictionaries, each representing a game with keys such as away_score and home_score.
  2. score_threshold: An integer representing the score threshold.

Steps:

  1. Filter Games: Use a list comprehension to iterate over the list of games and filter out those where the combined score of away_score and home_score exceeds score_threshold.
  2. Return Filtered List: Return the list of filtered games.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt