Python Tuples

How do tuples perform their operations in Python?

Python - Tuples:

Tuples in Python are ordered collections of items, similar to lists, but with a few key differences:

1. Immutable: Tuples are immutable, meaning once they are created, the items within them cannot be changed, added, or removed. This makes tuples useful for storing fixed collections of data.

2. Ordered: Like lists, tuples maintain the order of their elements, meaning the elements are indexed and can be accessed by their position.

3. Heterogeneous: Tuples can contain elements of different types. For example, you can have a tuple that contains integers, strings, and even other tuples.

Creating Tuples:

You can create a tuple by enclosing comma-separated values within parentheses '()':

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

You can also create an empty tuple:

empty_tuple = ()

To create a tuple with a single element, you need to include a trailing comma:

single_element_tuple = (42,)

Accessing Elements:

You can access elements of a tuple using indexing:

print(my_tuple[0])  # Output: 1
print(my_tuple[1])  # Output: 2

Tuple Unpacking:

You can assign the elements of a tuple to multiple variables in a single line using tuple unpacking:

a, b, c, d, e = my_tuple
print(a, b, c, d, e)  # Output: 1 2 3 4 5

Tuple Methods:

While tuples are immutable, they do have a few methods:

'count()': Returns the number of occurrences of a specified value in the tuple.

'index()': Returns the index of the first occurrence of a specified value.

Tuple Operations:

Tuples support concatenation using the '+' operator:

tuple1 = (1, 2, 3)
tuple2 = ('a', 'b', 'c')
concatenated_tuple = tuple1 + tuple2
# Output: (1, 2, 3, 'a', 'b', 'c')

Use Cases:

Tuples are commonly used when you want to store a fixed collection of items that should not be changed, such as coordinates, database records, or configuration settings.

coordinates = (3, 4)
database_record = ('Ayan', 'Sarkar', 28)
config_settings = ('debug', 'verbose')

When to Use - Tuples vs Lists:

Use tuples Use lists
When you want to store a fixed set of values that should not be changed. When you need a collection of items that can be modified.
When you need immutable keys for dictionaries. When you need to perform operations like adding, removing, or modifying elements.
When you want to return multiple values from a function.
When you are passing multiple values to a function and you want to ensure that they are not modified.

Let's create a simple Python program that demonstrates the use of tuples. We'll create a program that calculates the area and perimeter of a rectangle given its length and width.

PDF Copy Code
def calculate_rectangle_properties(length, width):

    area = length * width
    perimeter = 2 * (length + width)
    return area, perimeter

def main():
    # Get input from the user for length and width of the rectangle
    length = float(input("Enter the length of the rectangle: "))
    width = float(input("Enter the width of the rectangle: "))

    # Calculate the area and perimeter using the function
    area, perimeter = calculate_rectangle_properties(length, width)

    # Display the results
    print("Area of the rectangle:", area)
    print("Perimeter of the rectangle:", perimeter)

if __name__ == "__main__":
Enter the length of the rectangle: 25
Enter the width of the rectangle: 20
Area of the rectangle: 500.0    
Perimeter of the rectangle: 90.0

This program defines a function 'calculate_rectangle_properties()' that takes the length and width of a rectangle as input and returns a tuple containing the area and perimeter of the rectangle. In the 'main()' function, it prompts the user to enter the length and width of the rectangle, calculates the area and perimeter using the defined function, and then displays the results.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.