Tuples in Python (With Examples)

 PYTHON TUPLES:

A tuple is a built-in data structure that is similar to a list but with some key differences. Tuples are used to store an ordered collection of items, just like lists, but unlike lists, tuples are immutable. This means that once you create a tuple, you cannot change its elements, add new elements, or remove elements from it.

Tuples in Python (With Examples) | diplomawaale.blogspot.com
Python Tuples With Example


EXAMPLE:

                                   #creating a TUPLE
                                   number = (562, "Gaurav", "Gupta")

ADVANTAGE OF TUPLE:

  1. Immutability: Tuples are immutable, ensuring that once created, their elements cannot be changed. This is valuable for data integrity and preventing accidental modifications.
  2. Hashability: Tuples are hashable due to their immutability, making them suitable for use as keys in dictionaries and elements in sets.
  3. Order Preservation: Tuples maintain the order of elements, allowing you to access and process data sequentially, similar to lists.
  4. Function Returns: Tuples are commonly used to return multiple values from a function. You can pack related data into a tuple and return it as a single entity.
  5. Named Tuples: Python's collections.  named tuple allows you to create tuples with named fields, enhancing code readability and usability.
  6. Use Cases as Keys: Due to their immutability, tuples can be used as keys in dictionaries, unlike mutable data types such as lists.

FORMING OF DIFFERENT TYPES TUPLES:

A tuple in Python is an ordered collection of elements enclosed within parentheses (). It is a versatile data structure that can hold a mix of different data types, including numbers, strings, booleans, and more. Tuples are similar to lists, but they have the key characteristic of immutability, meaning once a tuple is created, its elements cannot be modified, added, or removed.

EXAMPLE:

                                          # Creating a tuple
                                          tuple = (1, 2, 3, 'apple', True)
                                          #number tuple
                                          tuple1=(2,4,5,34,98)
                                          #string tuple
                                          tuple2=("gaurav","ashirwad","zeeshan")
                                          #tuple constructor
                                          list=[1,"gaurav0","ashirwad"]
                                          tuple3=tuple(list)

                                          # Accessing elements
                                          first_element = my_tuple[0]
                                          third_element = my_tuple[2]

                                          # Printing the tuple and accessed elements
                                          print("Tuple:", my_tuple)
                                          print("First Element:", first_element)
                                          print("Third Element:", third_element)


ACCESSING VALUE FROM TUPLE:

Accessing values from a tuple in Python involves using indexing to retrieve individual elements.

EXAMPLE:

                            # Creating a tuple
                            my_tuple = (1, 2, 'apple', True, 3.14)

                            # Accessing elements using indexing
                            first_element = my_tuple[0]    # Access the first element: 1
                            third_element = my_tuple[2]    # Access the third element: 'apple'
                            last_element = my_tuple[-1]    # Access the last element: 3.14

                            # Printing the accessed elements
                            print("First Element:", first_element)
                            print("Third Element:", third_element)
                            print("Last Element:", last_element)

BASIC TUPLE OPERATION:

  1. Creating a Tuple: Defining a tuple by enclosing elements within parentheses.
  2. Accessing Elements: Retrieving individual elements using indexing.
  3. Tuple Unpacking: Assigning tuple values to separate variables.
  4. Concatenation: Combining tuples to create a new tuple.
  5. Repetition: Creating a new tuple by repeating elements.
  6. Length: Determining the number of elements in a tuple.
  7. Membership Testing: Checking if an element exists in a tuple.
  8. Slicing: Extracting a sub-tuple using index ranges.
  9. Iteration: Looping through tuple elements.
  10. Count: Counting occurrences of a value in a tuple.
  11. Index: Finding the index of a value in a tuple.

BUILT-IN FUNCTION:

Sr.No.

Function with Description

1

cmp(tuple1, tuple2)

Compares elements of both tuples.

2

len(tuple)

Gives the total length of the tuple.

3

max(tuple)

Returns item from the tuple with max value.

4

min(tuple)

Returns item from the tuple with min value.

5

tuple(seq)

Converts a list into tuple.

 

Post a Comment

0 Comments