The top fifteen Python tuple examples
The following list of 15 Python tuples includes an input, an output, and a brief description for each:
- Input:
numbers = (1, 2, 3)
Output: (1, 2, 3)
Description: A tuple containing three integers representing numbers.
- Input:
info = ('John', 25, 'Engineer')
Output: ('John', 25, 'Engineer')
Description: Tuple with a person’s name, age, and occupation.
- Input:
coordinates = (4.5, 3.2)
Output: (4.5, 3.2)
Description: Tuple representing coordinates (float values).
- Input:
names = ('Alice', 'Bob', 'Charlie')
Output: ('Alice', 'Bob', 'Charlie')
Description: Tuple of names for a group of people.
- Input:
mixed_types = (1, 'hello', 3.14)
Output: (1, 'hello', 3.14)
Description: Tuple with elements of different data types.
- Input:
empty_tuple = ()
Output: ()
Description: An empty tuple.
- Input:
single_element_tuple = (42,)
Output: (42,)
Description: Tuple with a single element.
- Input:
colors = ('red', 'green', 'blue')
Output: ('red', 'green', 'blue')
Description: Tuple representing a sequence of colors.
- Input:
point = (0, 0)
Output: (0, 0)
Description: Tuple representing a point in a 2D space.
- Input:
temperature = (25.5, 'Celsius')
Output:(25.5, 'Celsius')
Description: Tuple with a numeric value and a unit. - Input:
grades = (90, 85, 78, 92)
Output:(90, 85, 78, 92)
Description: Tuple containing student grades. - Input:
book_info = ('Python Crash Course', 2020, 'John Smith')
Output:('Python Crash Course', 2020, 'John Smith')
Description: Tuple with information about a book. - Input:
vowels = ('a', 'e', 'i', 'o', 'u')
Output:('a', 'e', 'i', 'o', 'u')
Description: Tuple with vowel characters. - Input:
dimensions = (10, 20, 5)
Output:(10, 20, 5)
Description: Tuple representing dimensions (length, width, height). - Input:
python nested_tuple = ((1, 2), ('a', 'b'))
Output:((1, 2), ('a', 'b'))
Description: Tuple with nested tuples, showcasing a hierarchy.
The adaptability of tuples in Python for different data structures and scenarios is demonstrated by these examples.