Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Lists and Loops | Section 1
Python - Sport
course content

Contenu du cours

Python - Sport

Python - Sport

1. Section 1
2. Section 2

book
Lists and Loops

Just like in sports, where repetition and practice are key to mastering skills, understanding how to use lists and loops in Python is essential for efficient data handling and analysis. Whether you're managing a list of player statistics or iterating through game data, mastering these concepts will significantly enhance your coding abilities.

In Python, a list is a versatile data structure that allows you to store multiple items in a single variable. Think of it as a team roster, where each player (or item) has a specific position. Lists are defined using square brackets [], and items are separated by commas.

python

Lists are incredibly useful for organizing data, and you can easily access, modify, and iterate over their elements. For instance, you can access the first player in the list using an index:

123
team_roster = ["LeBron James", "Anthony Davis", "Russell Westbrook"] first_player = team_roster[0] print(first_player)
copy

Loops in Python allow you to repeat a block of code multiple times. The most common type of loop is the for loop, which is perfect for iterating over lists.

This loop will print each player's name from the team_roster list. It's like calling out each player's name during a roll call.

123
team_roster = ["LeBron James", "Anthony Davis", "Russell Westbrook"] for player in team_roster: print(player)
copy

You can also use loops to perform calculations or modify list elements. Imagine you have a list of scores and want to calculate the total score:

python
Tâche

Swipe to start coding

Your goal is to complete calculate_average_score function that takes a list of scores and returns the average score

Inputs:

  • scores: A list of integers representing the scores.

Steps:

  1. Initialize a Sum Variable: Create a variable total_score and initialize it to 0. This will hold the sum of all scores in the list.

  2. Iterate Through the List: Use a for loop to iterate over each score in the scores list. Add each score to total_score.

  3. Calculate the Average: After the loop, calculate the average score by dividing total_score by the number of scores in the list. Use the len() function to get the number of scores.

  4. Return the Average Score: Ensure the function returns the calculated average score as a float.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 4
toggle bottom row

book
Lists and Loops

Just like in sports, where repetition and practice are key to mastering skills, understanding how to use lists and loops in Python is essential for efficient data handling and analysis. Whether you're managing a list of player statistics or iterating through game data, mastering these concepts will significantly enhance your coding abilities.

In Python, a list is a versatile data structure that allows you to store multiple items in a single variable. Think of it as a team roster, where each player (or item) has a specific position. Lists are defined using square brackets [], and items are separated by commas.

python

Lists are incredibly useful for organizing data, and you can easily access, modify, and iterate over their elements. For instance, you can access the first player in the list using an index:

123
team_roster = ["LeBron James", "Anthony Davis", "Russell Westbrook"] first_player = team_roster[0] print(first_player)
copy

Loops in Python allow you to repeat a block of code multiple times. The most common type of loop is the for loop, which is perfect for iterating over lists.

This loop will print each player's name from the team_roster list. It's like calling out each player's name during a roll call.

123
team_roster = ["LeBron James", "Anthony Davis", "Russell Westbrook"] for player in team_roster: print(player)
copy

You can also use loops to perform calculations or modify list elements. Imagine you have a list of scores and want to calculate the total score:

python
Tâche

Swipe to start coding

Your goal is to complete calculate_average_score function that takes a list of scores and returns the average score

Inputs:

  • scores: A list of integers representing the scores.

Steps:

  1. Initialize a Sum Variable: Create a variable total_score and initialize it to 0. This will hold the sum of all scores in the list.

  2. Iterate Through the List: Use a for loop to iterate over each score in the scores list. Add each score to total_score.

  3. Calculate the Average: After the loop, calculate the average score by dividing total_score by the number of scores in the list. Use the len() function to get the number of scores.

  4. Return the Average Score: Ensure the function returns the calculated average score as a float.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 4
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt