Tuples: Creating Tuples, Tuple Operations: Length, Concatenation, Repetition, Membership, Maximum, Minimum, Tuple Methods: Count, Index

Tuple is an ordered collection of elements enclosed within parentheses ( ). Tuples are similar to lists, but they are immutable, meaning their elements cannot be changed after creation.

Features of Tuples:

  1. Ordered collection of elements.
  2. Immutable in nature.
  3. Allows duplicate values.
  4. Supports indexing and slicing.
  5. Can store different data types.

Example

student = ("Kaif", 20, 85.5, "Commerce")

Creating Tuples

Method 1: Using Parentheses

fruits = ("Apple", "Mango", "Orange")
print(fruits)

Output

('Apple', 'Mango', 'Orange')

Method 2: Without Parentheses

numbers = 10, 20, 30
print(numbers)

Output

(10, 20, 30)

Method 3: Single Element Tuple

A comma is required for a single element tuple.

number = (10,)
print(type(number))

Output

<class 'tuple'>

Method 4: Empty Tuple

empty_tuple = ()
print(empty_tuple)

Output

()

Tuple Operations:

1. Length of a Tuple

The len() function returns the number of elements in a tuple.

Syntax

len(tuple)

Example

numbers = (10, 20, 30, 40)

print(len(numbers))

Output

4

2. Concatenation

Concatenation combines two or more tuples using the + operator.

Example

tuple1 = (10, 20, 30)
tuple2 = (40, 50, 60)

result = tuple1 + tuple2

print(result)

Output

(10, 20, 30, 40, 50, 60)

Characteristics

  1. Creates a new tuple.
  2. Original tuples remain unchanged.

3. Repetition

The * operator repeats tuple elements a specified number of times.

Example

numbers = (1, 2, 3)

print(numbers * 3)

Output

(1, 2, 3, 1, 2, 3, 1, 2, 3)

4. Membership Operators

in Operator

Checks whether an element exists in a tuple.

fruits = ("Apple", "Mango", "Orange")

print("Mango" in fruits)

Output

True

not in Operator

Checks whether an element does not exist in a tuple.

fruits = ("Apple", "Mango", "Orange")

print("Banana" not in fruits)

Output

True

5. Maximum Value

The max() function returns the largest element in a tuple.

Example

numbers = (10, 50, 20, 90, 30)

print(max(numbers))

Output

90

6. Minimum Value

The min() function returns the smallest element in a tuple.

Example

numbers = (10, 50, 20, 90, 30)

print(min(numbers))

Output

10

Tuple Methods

Since tuples are immutable, they have only two built in methods.

1. count()

The count() method returns the number of occurrences of a specified element.

Syntax

tuple.count(element)

Example

numbers = (10, 20, 10, 30, 10)

print(numbers.count(10))

Output

3

Uses

  1. Counting repeated values.
  2. Frequency analysis.
  3. Data validation.

2. index()

The index() method returns the position of the first occurrence of an element.

Syntax

tuple.index(element)

Example

fruits = ("Apple", "Mango", "Orange")

print(fruits.index("Mango"))

Output

1

Characteristics

  1. Returns the first matching index.
  2. Generates an error if the element is absent.

Summary Table

Operation/Method Purpose
len() Returns number of elements
+ Concatenates tuples
* Repeats tuple elements
in Checks presence of element
not in Checks absence of element
max() Returns largest element
min() Returns smallest element
count() Counts occurrences of an element
index() Returns position of an element

Difference Between List and Tuple

Basis List Tuple
Symbol [ ] ( )
Nature Mutable Immutable
Modification Allowed Not Allowed
Methods Many Methods Only count() and index()
Performance Slower Faster

Leave a Reply

error: Content is protected !!