TUPLE

A tuple is a collection data type that is similar to a list but is immutable. This means that once a tuple is created, you cannot modify its contents - you can't add, remove, or change elements. Tuples are defined using parentheses ().

# Creating a tuple

my_tuple = (1, 2, 3, 'hello', 3.14)

 

# Accessing elements in a tuple

print(my_tuple[0])  # Output: 1

print(my_tuple[3])  # Output: 'hello'

 

# Slicing a tuple

print(my_tuple[1:4])  # Output: (2, 3, 'hello')

 

# Tuple unpacking

a, b, c, d, e = my_tuple

print(a, b, c, d, e)  # Output: 1 2 3 'hello' 3.14

 

# Length of a tuple

print(len(my_tuple))  # Output: 5

 

# Concatenating tuples

tuple1 = (1, 2, 3)

tuple2 = ('a', 'b', 'c')

concatenated_tuple = tuple1 + tuple2

print(concatenated_tuple)  # Output: (1, 2, 3, 'a', 'b', 'c') # Creating a tuple

my_tuple = (1, 2, 3, 'hello', 3.14)

 

# Accessing elements in a tuple

print(my_tuple[0])  # Output: 1

print(my_tuple[3])  # Output: 'hello'

 

# Slicing a tuple

print(my_tuple[1:4])  # Output: (2, 3, 'hello')

 

# Tuple unpacking

a, b, c, d, e = my_tuple

print(a, b, c, d, e)  # Output: 1 2 3 'hello' 3.14

 

# Length of a tuple

print(len(my_tuple))  # Output: 5

 

# Concatenating tuples

tuple1 = (1, 2, 3)

tuple2 = ('a', 'b', 'c')

concatenated_tuple = tuple1 + tuple2

print(concatenated_tuple)  # Output: (1, 2, 3, 'a', 'b', 'c')