Blog Detail

September 1, 2023

Python Lists Exercises

  • Imagine you have a shopping list for groceries, and you realize that one of the items, ‘Milk’, needs to be updated to ‘Almond milk’.
    How would you update this item in the list?

                    # Create a list of groceries
    groceries = ["Apples", "Bread", "Milk", "Eggs"]
    
    # Update the item in the list
    ''
    
    print(groceries)
  • Complete the code based on this scenario:

    In a classroom, the teacher needs to rank students based on their exam scores. By organizing the list of numerical scores in ascending order , the teacher can determine the top-performing students and reward their academic achievements.

                    # List of exam scores
    scores = [90, 85, 95, 92, 88]
    
    # Sorting the scores in ascending order
    scores.''()
    
    print(scores)
  • Complete the code based on this scenario:

    In a competitive online game, players strive to achieve high scores. By utilizing a method that arranges the numerical scores in descending order , the game platform highlights the top-ranking players, fostering a sense of accomplishment and encouraging healthy competition among gamers.

                    # List of player scores
    scores = [100, 85, 92, 78, 95]
    
    # Sorting the scores in descending order
    scores.sort('')
    
    print(scores)
  • What is the output of this code?

                    # List of book titles
    book_titles = ['The Alchemist', 'Animal Farm', 'The Hobbit', 'Brave New World']
    
    # Sorting the book titles in alphabetical order
    book_titles.sort()
    
    print(book_titles)
  • What is the output of this code?

                    # List of movie titles
    movie_titles = ["The Godfather", "The Matrix", "Inception", "Fight Club", "Pulp Fiction"]
    
    # Sorting the movie titles in reverse alphabetical order
    movie_titles.sort(reverse=True)
    
    print(movie_titles)
  • Complete the code based on this scenario:

    In a bustling kitchen, a chef is preparing a special dish. To ensure they have all the required ingredients, the chef utilizes a technique that verifies if a particular ingredient is present in the list of available ingredients. This helps maintain the recipe’s integrity and allows the chef to create a delicious and satisfying culinary masterpiece.

                    # List of available ingredients
    available_ingredients = ['flour', 'sugar', 'eggs', 'butter', 'vanilla extract']
    
    # Ingredient to check
    ingredient = 'eggs'
    
    # Checking if the ingredient is present
    is_available = ingredient '' available_ingredients
    
    # Displaying the result
    if is_available:
        print('The ingredient is available!')
    else:
        print('The ingredient is not available.')
  • Complete the code based on this scenario

    Suppose we have a list of monthly website traffic data for a year, where each item represents the number of visits in a specific month. Now, we need to extract the website traffic data for the first quarter, which includes the months of January, February, and March.

                    # List of monthly website traffic data
    traffic_data = [5000, 6000, 5500, 7000, 8000, 7500, 9000, 9500, 8500, 10000, 9500, 11000]
    
    # Extract website traffic data for the first quarter (months 1-3)
    first_quarter_traffic = traffic_data['']
    
    # Print the website traffic data for the first quarter
    print("Website traffic data for the first quarter:")
    print(first_quarter_traffic)
    : 0 3 1
  • What is the output of this program?

                    # List of tasks
    tasks = ["Update content", "Prepare report", "Call clients",
             "Review strategy", "Finalize proposal"]
    
    # Extract prioritized tasks starting from the fourth position
    prioritized_tasks = tasks[3:]
    
    # Print the prioritized tasks
    print(prioritized_tasks)
  • Python Exercises for Data Structures

    • If you’ve faced difficulties with these exercises, take a look at Data Structures Module on my Python course.


    You can also share ‘Python Data Structures’ Exercises on your:
    X (Twitter)TelegramLinkedinFacebook – or on your favorite platform…




    Happy Coding!
    Behnam Khani