Wednesday, September 4, 2024

PYTHON - TUPLES

 Tuples:


Tuples are similar to lists but are immutable (cannot be modified once created).
They are defined using parentheses ().
Tuples are often used for data that should not change during the program's execution.

In Python, a tuple is an ordered, immutable (unchangeable), and iterable collection of elements. Tuples are similar to lists but have the key difference that once you create a tuple, you cannot modify its elements. Tuples are defined using parentheses (), and elements within the tuple are separated by commas. Here, I'll explain in detail about tuples and their operations in Python.

Example:

my_tuple = (1, 2, 3, "Python", 5.0)

1. Creating Tuples:
You can create a tuple by enclosing a sequence of values or objects within parentheses. Tuples can contain elements of any data type, including numbers, strings, other tuples, or a combination of types.

my_tuple = (1, 2, 3, "Python", 5.0)

2. Accessing Elements:
You can access individual elements in a tuple by their index, just like with lists. Python uses zero-based indexing.

first_element = my_tuple[0]  # Accesses the first element (1)
third_element = my_tuple[2]  # Accesses the third element (3)

3. Immutability:
Tuples are immutable, which means you cannot change their elements after they are created. Attempting to modify a tuple will result in an error.

my_tuple[3] = "Hello"  # This will raise a TypeError


4. Tuple Packing and Unpacking:
You can pack multiple values into a single tuple or unpack a tuple into individual variables.

# Packing values into a tuple
my_packed_tuple = 1, "Python", 3.14
print(my_packed_tuple)

# Unpacking a tuple into variables
x, y, z = my_packed_tuple

5. Finding Elements:
You can use the index() method to find the index of the first occurrence of a specific value in the tuple. If the element is not found, it raises a ValueError.

index_of_2 = my_tuple.index(2)  # Finds the index of the element 2 (1)

6. Length of a Tuple:
You can find the number of elements in a tuple using the len() function.

length = len(my_tuple)  # Returns the length of the tuple (5)

7. Iterating Through Tuples:

You can use a for loop to iterate through the elements of a tuple.

for item in my_tuple:
    print(item)

8. Concatenating Tuples:
You can concatenate two or more tuples using the + operator to create a new tuple.

tuple1 = (1, 2, 3)
tuple2 = ("a", "b", "c")
concatenated_tuple = tuple1 + tuple2

9. Repetition:
You can create a new tuple by repeating an existing tuple using the * operator.

my_tuple = (1, 2)
repeated_tuple = my_tuple * 3  # Creates (1, 2, 1, 2, 1, 2)

Tuples are commonly used when you need to represent a collection of elements that should not be modified, such as coordinates, record fields, or function return values. The immutability of tuples can help ensure data integrity in your programs.

Common operations on tuples include:

Accessing elements: indexing
Unpacking: (a, b) = (1, 2)
Finding length: len()
Iterating: for item in my_tuple

No comments:

Post a Comment