0% found this document useful (0 votes)
4 views5 pages

05 Tuples

A tuple in Python is an immutable, ordered collection of elements that can store multiple items and allows duplicate members. It is one of four built-in data types in Python, alongside lists, sets, and dictionaries, each with distinct characteristics. Key features of tuples include their immutability, ability to store heterogeneous data types, and limited methods for interaction, making them suitable for fixed data storage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

05 Tuples

A tuple in Python is an immutable, ordered collection of elements that can store multiple items and allows duplicate members. It is one of four built-in data types in Python, alongside lists, sets, and dictionaries, each with distinct characteristics. Key features of tuples include their immutability, ability to store heterogeneous data types, and limited methods for interaction, making them suitable for fixed data storage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Tuple:

 List is a collection which is ordered and changeable. Allows duplicate members.

 Tuple is a collection which is ordered and unchangeable. Allows duplicate


members.

 Set is a collection which is unordered, unchangeable*, and unindexed. No


duplicate members.

 Dictionary is a collection which is ordered** and changeable. No duplicate


members.

1) Python Tuple:
i) Python Tuple
ii) Access Tuple
iii) Update Tuple
iv) Unpack Tuple
v) Loop Tuple
vi) Join Tuple
vii) Tuple Methods

Tuple

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of data, the other
3 are List, Set, and Dictionary, all with different qualities and usage.

A tuple in Python is an immutable, ordered collection of elements enclosed in


parentheses ().

 Tuples can store heterogeneous data types (e.g., integers, strings, other tuples).
 Once created, elements cannot be added, removed, or
modified (immutability).

 Tuples are hashable (if all elements are hashable) and can be used as keys in
dictionaries.

Example:

ii) Access Tuple Items

Theory:
Similar to lists, tuples use zero-based indexing and slicing.

 Positive Indexing: tuple[0] (first item).

 Negative Indexing: tuple[-1] (last item).

 Slicing: tuple[start:end:step] (end index is exclusive).

Examples:

iii) Update Tuple

Theory:
Tuples are immutable, so you cannot modify elements directly. To "update" a tuple:

1. Convert the tuple to a list.

2. Modify the list.

3. Convert it back to a tuple.

Examples:
iv) Unpack Tuple

Theory:
Unpacking assigns tuple elements to variables in a single line.

 Extended Unpacking: Use * to assign multiple elements to a list.

Examples:

v) Loop Through a Tuple

Theory:
Use loops to iterate over tuple elements:

 for loop: Direct iteration.

 while loop: Use indexing.

Examples:

vi) Join Tuples

Theory:
Combine tuples using:

 + operator: Concatenation.

 * operator: Repetition.
 zip(): Merge tuples element-wise.

Examples:

vii) Tuple Methods

Theory:
Tuples have only two built-in methods:

1. count(item): Returns the number of occurrences of item.

2. index(item): Returns the first index of item.

Examples:

Key Takeaways:

 Immutability: Tuples cannot be modified after creation.

 Performance: Faster than lists for read-only operations.

 Use Cases: Ideal for fixed data (e.g., coordinates, database records).

 Methods: Limited to count() and index(), but other functions


like len(), sorted(), max(), and min() work with tuples.

You might also like