Exercises Tuple with Python

Exercise 1

Question: Create a tuple containing the numbers from 1 to 5.

Solution:

tuple1 = (1, 2, 3, 4, 5)

Exercise 2

Question: Create a tuple containing three elements: an integer, a string, and a list.

Solution:

tuple2 = (10, "example", [1, 2, 3])

Exercise 3

Question: Access and print the second element of the tuple (5, ‘a’, 7.2, [1, 2]).

Solution:

tuple3 = (5, 'a', 7.2, [1, 2])
print(tuple3[1])#Output: 'a'

Exercise 4

Question: Modify the first element of the tuple (10, 20, 30) to be 100.

Solution: Tuples are immutable, so you need to create a new tuple:

tuple4 = (10, 20, 30)
tuple4 = (100,) + tuple4[1:]
print(tuple4)#Output: (100, 20, 30)

Exercise 5

Question: Create a tuple of 4 elements and use the multiplication operator to repeat the tuple 3 times.

Solution:

tuple5 = (1, 2, 3, 4)
tuple5_repeated = tuple5 * 3
print(tuple5_repeated)#Output: (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)

Exercise 6

Question: Create a tuple containing the first 5 prime numbers.

Solution:

tuple6 = (2, 3, 5, 7, 11)

Exercise 7

Question: Create a tuple containing tuples, where each sub-tuple represents a point (x, y).

Solution:

tuple7 = ((1, 2), (3, 4), (5, 6))

Exercise 8

Question: Find the length of the tuple (1, 2, 3, 4, 5).

Solution:

tuple8 = (1, 2, 3, 4, 5)
length = len(tuple8)
print(length)#Output: 5

Exercise 9

Question: Find the index of the number 20 in the tuple (10, 20, 30, 40).

Solution:

tuple9 = (10, 20, 30, 40)
index = tuple9.index(20)
print(index)#Output: 1

Exercise 10

Question: Create a tuple of strings and convert it into a single string separated by commas.

Solution:

tuple10 = ("apple", "banana", "cherry")
result = ", ".join(tuple10)
print(result)#Output: 'apple, banana, cherry'

Exercise 11

Question: Create a tuple of 3 elements and use the concatenation operator to add another tuple of 2 elements.

Solution:

tuple11 = (1, 2, 3)
tuple12 = (4, 5)
result = tuple11 + tuple12
print(result)#Output: (1, 2, 3, 4, 5)

Exercise 12

Question: Create a tuple of numbers and find the maximum and minimum number.

Solution:

tuple13 = (7, 2, 9, 1, 5)
maximum = max(tuple13)
minimum = min(tuple13)
print(maximum)#Output: 9
print(minimum)#Output: 1

Exercise 13

Question: Create a tuple of tuples where each sub-tuple contains a name and an age. Find the age of “Alice”.

Solution:

tuple14 = (("Alice", 30), ("Bob", 25), ("Charlie", 35))
age = dict(tuple14).get("Alice")
print(age)#Output: 30

Exercise 14

Question: Create a tuple containing numbers from 10 to 1 and use the sorted function to sort the tuple in ascending order.

Solution:

tuple15 = tuple(range(10, 0, -1))
sorted_tuple = sorted(tuple15)
print(sorted_tuple)#Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Exercise 16

Question: Create a tuple containing mixed elements (integers, strings, lists) and check if a specific string is in the tuple.

Solution:

tuple16 = (1, "test", [1, 2], 3.14)
contains_test = "test" in tuple16
print(contains_test)#Output: True

Exercise 17

Question: Create a tuple with repeated elements and use a list comprehension to remove duplicates.

Solution:

tuple17 = (1, 2, 2, 3, 3, 4)
unique_elements = tuple(set(tuple17))
print(unique_elements)#Output: (1, 2, 3, 4)

Exercise 18

Question: Create a tuple of integers and use a tuple comprehension to create a new tuple containing only the even numbers.

Solution:

tuple18 = (1, 2, 3, 4, 5, 6)
even_numbers = tuple(x for x in tuple18 if x % 2 == 0)
print(even_numbers)#Output: (2, 4, 6)

Exercise 19

Question: Create a tuple of numbers and print each element on a new line.

Solution:

tuple19 = (1, 2, 3, 4, 5)
for element in tuple19:
    print(element)

Exercise 20

Question: Create a tuple of tuples where each sub-tuple contains a name and a score. Calculate the average score.

Solution:

tuple20 = (("Alice", 85), ("Bob", 90), ("Charlie", 78))
scores = [score for name, score in tuple20]
average_score = sum(scores) / len(scores)
print(average_score)#Output: 84.33333333333333

Exercise 21

Question: Create a tuple of tuples where each sub-tuple represents a date (year, month, day). Sort the tuple in ascending order by date.

Solution:

tuple21 = ((2024, 5, 15), (2023, 12, 1), (2024, 1, 20))
sorted_tuple = tuple(sorted(tuple21))
print(sorted_tuple)#Output: ((2023, 12, 1), (2024, 1, 20), (2024, 5, 15))

Exercise 22

Question: Create a tuple of tuples where each sub-tuple contains coordinates (x, y). Find the distance between the first points (x1, y1) and (x2, y2).

Solution:

import math
tuple22 = ((1, 2), (4, 6))
x1, y1 = tuple22[0]
x2, y2 = tuple22[1]
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
print(distance)#Output: 5.0

Exercise 23

Question: Create a tuple of integers. Use the sum function to find the sum of the elements in the tuple.

Solution:

tuple23 = (1, 2, 3, 4, 5)
total = sum(tuple23)
print(total)#Output: 15

Exercise 24

Question: Create a tuple of strings. Use a tuple comprehension to create a new tuple where each string is reversed.

Solution:

tuple24 = ("hello", "world", "python")
reversed_tuple = tuple(s[::-1] for s in tuple24)
print(reversed_tuple)#Output: ('olleh', 'dlrow', 'nohtyp')

Exercise 25

Question: Create a tuple of integers and use a loop to create a new tuple where each element is double the value of the elements in the original tuple.

Solution:

tuple25 = (1, 2, 3, 4, 5)
doubled_tuple = tuple(x * 2 for x in tuple25)
print(doubled_tuple)#Output: (2, 4, 6, 8, 10)

Exercise 26

Question: Create a tuple of floating-point numbers and convert it into a list.

Solution:

tuple26 = (1.1, 2.2, 3.3)
list_from_tuple = list(tuple26)
print(list_from_tuple)#Output: [1.1, 2.2, 3.3]

Exercise 27

Question: Create a tuple of tuples where each sub-tuple represents a person with their name and age. Sort the tuple by age in ascending order.

Solution:

tuple27 = (("Alice", 30), ("Bob", 25), ("Charlie", 35))
sorted_by_age = tuple(sorted(tuple27, key=lambda x: x[1]))
print(sorted_by_age)#Output: (('Bob', 25), ('Alice', 30), ('Charlie', 35))

Exercise 28

Question: Create a tuple of strings. Use the count method to find how many times the string “apple” appears in the tuple.

Solution:

tuple28 = ("apple", "banana", "apple", "cherry", "apple")
count_apple = tuple28.count("apple")
print(count_apple)#Output: 3

Exercise 29

Question: Create a tuple of integers. Use min and max to find the smallest and largest values in the tuple.

Solution:

tuple29 = (10, 20, 5, 30, 15)
min_value = min(tuple29)
max_value = max(tuple29)
print(min_value)#Output: 5
print(max_value)#Output: 30

Exercise 30

Question: Create a tuple containing tuples of key-value pairs. Convert this tuple into a dictionary.

Solution:

tuple30 = (("a", 1), ("b", 2), ("c", 3))
dictionary = dict(tuple30)
print(dictionary)#Output: {'a': 1, 'b': 2, 'c': 3}

Exercise 31

Question: Create a tuple of numbers. Calculate the sum of the squares of the elements in the tuple.

Solution:

tuple31 = (1, 2, 3, 4)
sum_of_squares = sum(x**2 for x in tuple31)
print(sum_of_squares)#Output: 30

Exercise 32

Question: Create a tuple with mixed elements (integers, strings, lists). Count how many of the elements in the tuple are lists.

Solution:

tuple32 = (1, "test", [1, 2], [3, 4], 5)
list_count = sum(1 for element in tuple32 if isinstance(element, list))
print(list_count)#Output: 2

Exercise 33

Question: Create a tuple of tuples where each sub-tuple contains coordinates (x, y). Find the sum of all the x coordinates.

Solution:

tuple33 = ((1, 2), (3, 4), (5, 6))
sum_x = sum(x for x, y in tuple33)
print(sum_x)#Output: 9

Exercise 34

Question: Create a tuple of floating-point numbers. Find the product of all the elements in the tuple.

Solution:

from functools import reduce
import operator
tuple34 = (1.1, 2.2, 3.3)
product = reduce(operator.mul, tuple34, 1)
print(product)#Output: 7.986

Exercise 35

Question: Create a tuple of strings. Use a tuple comprehension to convert each string to uppercase and create a new tuple with these strings.

Solution:

tuple35 = ("hello", "world", "python")
uppercase_tuple = tuple(s.upper() for s in tuple35)
print(uppercase_tuple)#Output: ('HELLO', 'WORLD', 'PYTHON')

Exercise 36

Question: Create a tuple with tuples of coordinates (x, y). Find the point with the highest x coordinate.

Solution:

tuple36 = ((1, 2), (3, 4), (5, 6))
max_x_point = max(tuple36, key=lambda coord: coord[0])
print(max_x_point)#Output: (5, 6)

Exercise 37

Question: Create a tuple of integers and use a tuple comprehension to create a new tuple with elements that are powers of 2.

Solution:

tuple37 = (1, 2, 3, 4, 5, 8, 16)
powers_of_two = tuple(x for x in tuple37 if (x & (x - 1)) == 0)
print(powers_of_two)#Output: (1, 2, 4, 8, 16)

Exercise 38

Question: Create a tuple of tuples representing products with a name and price. Create a list sorted by names.

Solution:

tuple38 = (("Apple", 1.5), ("Banana", 1.2), ("Cherry", 2.0))
sorted_names = [name for name, price in sorted(tuple38, key=lambda item: item[0])]
print(sorted_names)#Output: ['Apple', 'Banana', 'Cherry']

Exercise 39

Question: Create a tuple of numbers and find the average of these numbers.

Solution:

tuple39 = (10, 20, 30, 40, 50)
average = sum(tuple39) / len(tuple39)
print(average)#Output: 30.0

Exercise 40

Question: Create a tuple of tuples where each sub-tuple represents a product with a name and price. Find the product with the highest price.

Solution:

tuple40 = (("Apple", 1.5), ("Banana", 1.2), ("Cherry", 2.0))
most_expensive_product = max(tuple40, key=lambda item: item[1])
print(most_expensive_product)#Output: ('Cherry', 2.0)

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *