How do I create tuples in Python with examples?

In Python, a tuple is a collection data type that is ordered and immutable. Tuples are defined by placing the items inside parentheses, separated by commas. They can hold multiple types of data, including integers, strings, and other objects.

Creating Tuples

Here are some ways to create tuples in Python:

# Example of a simple tuple
my_tuple = (1, 2, 3)
print(my_tuple)

# Tuple with different data types
mixed_tuple = ("Hello", 42, 3.14, True)
print(mixed_tuple)

# Nested tuple
nested_tuple = (1, 2, (3, 4), 5)
print(nested_tuple)

# Tuple with single element (note the comma)
single_element_tuple = (42,)
print(single_element_tuple)

Keywords: Python Tuples Create Tuples Immutable Collections Python Data Structures