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

Contenu du cours

Python - Sport

Python - Sport

1. Section 1
2. Section 2

book
Defining Functions

Defining your own functions in Python allows you to create reusable blocks of code tailored to specific tasks. This is particularly useful in data analytics, where you often need to perform repetitive operations efficiently.

In Python, a function is a block of organized, reusable code that performs a single, related action. Functions help break down complex problems into smaller, manageable parts, making your code more modular and easier to understand.

Why Use Functions?

  • Reusability: Write once, use multiple times. Functions allow you to reuse code without rewriting it.
  • Modularity: Break down complex tasks into simpler, more manageable pieces.
  • Readability: Functions make your code cleaner and easier to read.

To define a function in Python, use the def keyword, followed by the function name and parentheses (). Inside the parentheses, you can specify parameters that the function will accept. The function body is indented and contains the code to be executed. In this example, greet_player is a function that takes one parameter, player_name, and prints a greeting message. To execute a function, you need to call it by its name and provide any required arguments.

1234
def greet_player(player_name): print(f"Hello, {player_name}! Welcome to the game.") greet_player("LeBron James")
copy

Imagine you want to calculate the average points scored by a player over several games. You can define a function to perform this calculation.

12345678910
def calculate_average(points_list): total_points = sum(points_list) number_of_games = len(points_list) average_points = total_points / number_of_games return average_points # Usage player_points = [25, 30, 22, 28] average = calculate_average(player_points) print(f"Average points per game: {average}")
copy
Tâche

Swipe to start coding

Your goal is to define count_games_by_season function that counts the total number of games for each NFL season from a list of game data

Input:

  • games_info: A list of dictionaries, where each dictionary contains information about a game, including a key 'season' that indicates the season in which the game was played.

Steps:

  1. Define the Function: Start by defining a function named count_games_by_season that takes one parameter: games_info.

  2. Initialize an Empty Dictionary: Create an empty dictionary season_game_count to hold the count of games for each season.

  3. Iterate Over Each Game: Use a for loop to iterate over each game dictionary in the games_info list.

  4. Update the Dictionary:

    • Extract the 'season' value from each game dictionary.
    • If the season is already a key in season_game_count, increment its value by 1.
    • If the season is not a key, add it to the dictionary with an initial count of 1.
  5. Return the Dictionary: Ensure the function returns the season_game_count dictionary with the count of games for each season.

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 6
toggle bottom row

book
Defining Functions

Defining your own functions in Python allows you to create reusable blocks of code tailored to specific tasks. This is particularly useful in data analytics, where you often need to perform repetitive operations efficiently.

In Python, a function is a block of organized, reusable code that performs a single, related action. Functions help break down complex problems into smaller, manageable parts, making your code more modular and easier to understand.

Why Use Functions?

  • Reusability: Write once, use multiple times. Functions allow you to reuse code without rewriting it.
  • Modularity: Break down complex tasks into simpler, more manageable pieces.
  • Readability: Functions make your code cleaner and easier to read.

To define a function in Python, use the def keyword, followed by the function name and parentheses (). Inside the parentheses, you can specify parameters that the function will accept. The function body is indented and contains the code to be executed. In this example, greet_player is a function that takes one parameter, player_name, and prints a greeting message. To execute a function, you need to call it by its name and provide any required arguments.

1234
def greet_player(player_name): print(f"Hello, {player_name}! Welcome to the game.") greet_player("LeBron James")
copy

Imagine you want to calculate the average points scored by a player over several games. You can define a function to perform this calculation.

12345678910
def calculate_average(points_list): total_points = sum(points_list) number_of_games = len(points_list) average_points = total_points / number_of_games return average_points # Usage player_points = [25, 30, 22, 28] average = calculate_average(player_points) print(f"Average points per game: {average}")
copy
Tâche

Swipe to start coding

Your goal is to define count_games_by_season function that counts the total number of games for each NFL season from a list of game data

Input:

  • games_info: A list of dictionaries, where each dictionary contains information about a game, including a key 'season' that indicates the season in which the game was played.

Steps:

  1. Define the Function: Start by defining a function named count_games_by_season that takes one parameter: games_info.

  2. Initialize an Empty Dictionary: Create an empty dictionary season_game_count to hold the count of games for each season.

  3. Iterate Over Each Game: Use a for loop to iterate over each game dictionary in the games_info list.

  4. Update the Dictionary:

    • Extract the 'season' value from each game dictionary.
    • If the season is already a key in season_game_count, increment its value by 1.
    • If the season is not a key, add it to the dictionary with an initial count of 1.
  5. Return the Dictionary: Ensure the function returns the season_game_count dictionary with the count of games for each season.

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 6
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