Blog Detail

September 3, 2023

Python Tuples Exercises

  • What is the type of the play_list ?

                    play_list = ["Song 1", "Song 2", "Song 3", "Song 4", "Song 5"]
  • What is the type of the products ?

                    products = ("Apple", "Banana", "Orange", "Grapes", "Mango")
  • Retrieve the last item from the tuple by using its index number .

                    # Create a tuple
    my_tuple = ("Apple", "Banana", "Orange", "Grapes", "Mango")
    
    # Retrieve the last item from the tuple
    last_item = my_tuple['']
    
    # Print the last item
    print("Last item:", last_item)
  • What is the output of this code?

                    # Create a tuple
    my_tuple = ("Apple", "Banana", "Orange", "Grapes", "Mango")
    
    item = my_tuple[1]
    
    # Print the item
    print(item )
    
  • What is the output of this tuple slicing ?

                    # Create a tuple
    my_tuple = ("Apple", "Banana", "Orange", "Grapes", "Mango")
    
    # Perform slicing
    sliced_tuple = my_tuple[1:3]
    
    # Print the sliced portion
    print(sliced_tuple)
  • Retrieve and print the count of items in the tuple.

                    # Create a tuple
    my_tuple = ("Apple", "Banana", "Orange", "Grapes", "Mango")
    
    # Get the length of the tuple
    tuple_length = ''(my_tuple)
    
    # Print the length of the tuple
    print("Length of the tuple:", tuple_length)
  • What is the output of this code?

                    # Create a tuple
    my_tuple = ("Apple", "Banana", "Orange", "Grapes", "Mango")
    
    # Get the length of the tuple
    tuple_length = len(my_tuple)
    
    # Print the length of the tuple
    print(tuple_length)
  • What is the output of this program?

                    my_tuple = ("Apple", "Banana", "Orange", "Grapes", "Mango")
    
    tuple_length = len(my_tuple)
    
    item = my_tuple[tuple_length - 1]
    
    print(item)
  • What is wrong with this code?

                    my_tuple = ("Apple", "Banana", "Orange", "Grapes", "Mango")
    
    my_tuple.insert('Strawberry')
    
    print(my_tuple)
  • Pass the my_tuple to the print_tuple_items function.

                    # Define a function to print tuple items
    def print_tuple_items(tuple_items):
        for item in tuple_items:
            print(item)
    
    # Create a tuple
    my_tuple = ('Apple', 'Banana', 'Orange', 'Grapes', 'Mango')
    
    # Pass the tuple to the function and print each item
    print_tuple_items('')
  • How do you describe the calculate_sum function?

                    def calculate_sum(scores):
        total = 0
        for score in scores:
            total += score
    
        return total
    
    scores = (85, 90, 92, 88, 95)
    sum_of_scores = calculate_sum(scores)
    print(sum_of_scores)
  • What does this function returns ?

                    def get_student_info():
        name = 'Jane Smith'
        age = 19
        grade = 'B+'
        return (name, age, grade)
    
    
  • Unpack the returned tuple into three variables.

                    def get_student_info():
        name = 'Jane Smith'
        age = 19
        grade = 'B+'
        return (name, age, grade)
    
    '' = get_student_info()
    
    print('Student Name:', student_name)
    print('Student Age:', student_age)
    print('Student Grade:', student_grade)
  • Imagine you are developing a data processing application that needs to store information about students. Each student has a unique ID (integer), a name (string), and their average score (float). The data are fixed and should not be modified.

    Based on the scenario, which one is a suitable choice to represent each student’s data?

                    # Using tuples
    student1 = (1, "John Doe", 85.5)
    student2 = (2, "Jane Smith", 92.0)
    student3 = (3, "Alex Johnson", 78.9)
    
    # OR
    
    # Using Lists
    student1 = [1, "John Doe", 85.5]
    student2 = [2, "Jane Smith", 92.0]
    student3 = [3, "Alex Johnson", 78.9]
  • Python Exercises for Tuples

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


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




    Happy Coding!
    Behnam Khani