Python Tuples Explained
Tuples are ordered, immutable collections in Python. Let's explore the basics of Python tuples:
1. Creating Tuples
Tuples are created using parentheses `()` with elements separated by commas.
# Example
my_tuple = (1, 'two', 3.0, (4, 5))
2. Accessing Elements
Individual elements in a tuple are accessed using indexing, similar to lists.
# Example
first_element = my_tuple[0]
nested_element = my_tuple[3][1]
3. Immutability
Tuples are immutable, meaning their elements cannot be changed once defined.
# Example (This will raise an error)
my_tuple[1] = 'modified'
Python Tuples
Reviewed by Naveen Gupta
on
1:12 AM
Rating:
No comments: