Python Tutorial

Understanding Tuples in Python

A tuple is one of Python’s built-in data types and is defined as a sequence of comma-separated values enclosed within parentheses (). Unlike some other data types, the items in a tuple can be of different types.

Here are some examples of tuples:

tup1 = ("Rohan", "Physics", 21, 69.75)
tup2 = (1, 2, 3, 4, 5)
tup3 = ("a", "b", "c", "d")
tup4 = (25.50, True, -55, 1+2j)

An empty tuple is written as:

tup1 = ()

For a tuple containing a single element, a comma is required after the value:

tup1 = (50,)

Key Points about Tuples:

  • Ordered and Indexed: Tuples are ordered, meaning each item has a fixed position starting from index 0.
  • Different Data Types: Unlike arrays in languages like C, Java, or C++, Python tuples can contain elements of varying data types.
  • Immutability: While lists are mutable, tuples are immutable. This means once a tuple is created, its elements cannot be changed, added, or removed. You can, however, access elements by their index.

Accessing Values in Tuples

To access values in a tuple, use square brackets and provide the index or a slice:

tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7)

print(tup1[0])  # Output: 'physics'
print(tup2[1:5])  # Output: (2, 3, 4, 5)

Modifying Tuples

Since tuples are immutable, you cannot directly change their values. However, you can create a new tuple by concatenating existing ones:

tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2

print(tup3)  # Output: (12, 34.56, 'abc', 'xyz')

Deleting a Tuple

You cannot delete individual elements in a tuple, but you can delete the entire tuple using the del statement:

tup = ('physics', 'chemistry', 1997, 2000)
print(tup)
del tup

Attempting to access the tuple after deletion will raise a NameError because the tuple no longer exists.

Tuple Operations

Tuples support several operations due to their sequence nature:

  • Concatenation: Combine two tuples using the + operator.
(1, 2, 3) + (4, 5, 6)  # Output: (1, 2, 3, 4, 5, 6)
  • Repetition: Multiply a tuple to repeat its elements.
('Hi!',) * 4  # Output: ('Hi!', 'Hi!', 'Hi!', 'Hi!')
  • Membership Test: Use in and not in to check for membership.
3 in (1, 2, 3)  # Output: True

Indexing and Slicing

Tuples behave like strings when it comes to indexing and slicing:

L = ('spam', 'Spam', 'SPAM!')

print(L[2])  # Output: 'SPAM!'
print(L[-2])  # Output: 'Spam'
print(L[1:])  # Output: ('Spam', 'SPAM!')

Tuple without Enclosing Delimiters

Multiple objects separated by commas but not enclosed in any specific brackets default to a tuple:

print('abc', -4.24e93, 18+6.6j, 'xyz')
x, y = 1, 2
print("Value of x, y:", x, y)

This will produce:

abc -4.24e+93 (18+6.6j) xyz
Value of x, y: 1 2

Built-in Tuple Functions

Python provides several built-in functions for working with tuples:

  • len(tuple): Returns the length of the tuple.
  • max(tuple): Returns the maximum value in the tuple.
  • min(tuple): Returns the minimum value in the tuple.
  • tuple(seq): Converts a list or other sequence into a tuple.

By understanding these features and operations, you’ll unlock the full potential of tuples in your Python programs!