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

TUPLE

Tuples are immutable data structures in Python that maintain the order of elements and allow duplicates. They can be created using parentheses and support operations like concatenation and repetition. Tuples have limited built-in methods, can be nested, and offer advantages such as data integrity and efficiency.

Uploaded by

Akshat Joshi
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 views3 pages

TUPLE

Tuples are immutable data structures in Python that maintain the order of elements and allow duplicates. They can be created using parentheses and support operations like concatenation and repetition. Tuples have limited built-in methods, can be nested, and offer advantages such as data integrity and efficiency.

Uploaded by

Akshat Joshi
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/ 3

TUPLE

Tuples are a fundamental data structure in Python that are


similar to lists but with one key difference: they are immutable,
meaning their elements cannot be changed after they are created.
Here’s a detailed overview of tuples and how to use them:

Creating Tuples

You can create a tuple by placing comma-separated values inside


parentheses ().

python
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple) # Output: (1, 2, 3, 4, 5)

Characteristics of Tuples

1. Ordered: Tuples maintain the order of elements.


2. Immutable: Once a tuple is created, its elements cannot be
changed.
3. Allow Duplicates: Tuples can contain duplicate elements.

Accessing Tuple Elements

You can access elements in a tuple using indexing, starting from 0


for the first element.

print(my_tuple[0]) # Output: 1
print(my_tuple[1]) # Output: 2
print(my_tuple[-1]) # Output: 5 (negative indexing)

Slicing Tuples

Similar to lists, you can slice tuples to get a range of elements.

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


print(my_tuple[:3]) # Output: (1, 2, 3)
print(my_tuple[2:]) # Output: (3, 4, 5)
Tuple Operations

 Concatenation: You can concatenate two or more tuples


using the + operator.
 Repetition: You can repeat a tuple using the * operator.

tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
concatenated_tuple = tuple1 + tuple2
print(concatenated_tuple) # Output: (1, 2, 3, 4, 5, 6)

repeated_tuple = tuple1 * 2
print(repeated_tuple) # Output: (1, 2, 3, 1, 2, 3)

Tuple Methods

Tuples have only a few built-in methods, mainly because they are
immutable:

 count(): Returns the number of times a specified value


appears in the tuple.
 index(): Returns the index of the first occurrence of the
specified value.

my_tuple = (1, 2, 3, 2, 4, 2)
print(my_tuple.count(2)) # Output: 3
print(my_tuple.index(3)) # Output: 2

Nesting Tuples

Tuples can contain other tuples, which allows you to create


complex nested data structures.

python
nested_tuple = (1, (2, 3, 4), (5, 6))
print(nested_tuple) # Output: (1, (2, 3, 4), (5, 6))
print(nested_tuple[1][1]) # Output: 3
Tuple Packing and Unpacking

 Packing: Assigning multiple values to a single tuple.


 Unpacking: Extracting values from a tuple into individual
variables.

# Packing
packed_tuple = 1, 2, 3
print(packed_tuple) # Output: (1, 2, 3)

# Unpacking
a, b, c = packed_tuple
print(a, b, c) # Output: 1 2 3

Advantages of Tuples

 Immutability: Ensures data integrity by preventing


changes.
 Performance: Tuples can be more efficient than lists in
terms of memory and speed.
 Hashable: Tuples can be used as keys in dictionaries, unlike
lists.

You might also like