0% found this document useful (0 votes)
2 views6 pages

Python Tuples: A Complete Guide

Uploaded by

Sneha Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Python Tuples: A Complete Guide

Uploaded by

Sneha Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Comprehensive Guide to Python Lists

Introduction to Python Tuples:

- A Python tuple is an ordered, immutable collection of elements.

- Tuples are defined using parentheses `()` or the `tuple()` function.

Example:

my_tuple = (1, 2, 3, 'apple')

print(my_tuple)

1. Accessing Tuple Elements:

- Elements can be accessed using indexes (0-based).

Example:

my_tuple = (10, 20, 30, 40)

print(my_tuple[1]) # 20

print(my_tuple[-1]) # 40 (last element)

2. Nested Tuples:

- Tuples can contain other tuples.

Example:

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

Page 1
Comprehensive Guide to Python Lists

print(nested_tuple[1]) # (2, 3)

print(nested_tuple[1][0]) # 2

3. Tuple Slicing:

- Use slicing to access a subrange of a tuple.

Example:

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

print(my_tuple[1:4]) # (1, 2, 3)

print(my_tuple[::-1]) # (5, 4, 3, 2, 1, 0) (reversed tuple)

4. Immutability of Tuples:

- Tuples cannot be modified after creation.

Example:

my_tuple = (1, 2, 3)

# my_tuple[0] = 10 # Error: Tuples are immutable

5. Tuple Concatenation and Repetition:

- Tuples can be concatenated using `+` and repeated using `*`.

Page 2
Comprehensive Guide to Python Lists

Example:

tuple1 = (1, 2)

tuple2 = (3, 4)

combined = tuple1 + tuple2 # (1, 2, 3, 4)

repeated = tuple1 * 3 # (1, 2, 1, 2, 1, 2)

6. Tuple Unpacking:

- Elements of a tuple can be unpacked into variables.

Example:

my_tuple = (10, 20, 30)

a, b, c = my_tuple

print(a, b, c) # 10 20 30

7. Single-Element Tuples:

- A tuple with one element must include a trailing comma.

Example:

single_element_tuple = (10,) # This is a tuple

not_a_tuple = (10) # This is an integer

8. Tuple Methods:

Page 3
Comprehensive Guide to Python Lists

- `count()`: Counts the occurrences of an element.

- `index()`: Returns the index of the first occurrence.

Example:

my_tuple = (1, 2, 2, 3)

print(my_tuple.count(2)) # 2 occurrences

print(my_tuple.index(3)) # Index of 3 3

9. Checking Membership in Tuples:

- Use `in` and `not in` to check if an element exists in a tuple.

Example:

my_tuple = (1, 2, 3)

print(2 in my_tuple) # True

print(5 not in my_tuple) # True

10. Tuples as Keys in Dictionaries:

- Tuples can be used as keys in dictionaries (since they are immutable).

Example:

coordinates = {(0, 0): "Origin", (1, 2): "Point A"}

Page 4
Comprehensive Guide to Python Lists

print(coordinates[(1, 2)]) # "Point A"

11. Tuple vs List:

- Tuples are immutable and generally faster than lists.

- Lists are mutable and more flexible for operations that involve adding or removing elements.

Example:

my_list = [1, 2, 3] # Mutable

my_tuple = (1, 2, 3) # Immutable

12. Creating Tuples with `tuple()`:

- Convert an iterable into a tuple using `tuple()`.

Example:

my_tuple = tuple([1, 2, 3]) # Converts list to tuple (1, 2, 3)

13. Nested Tuple Unpacking:

- You can unpack nested tuples.

Example:

nested_tuple = (1, (2, 3))

Page 5
Comprehensive Guide to Python Lists

a, (b, c) = nested_tuple

print(a, b, c) # 1 2 3

Page 6

You might also like