You are on page 1of 1

Powered by AI

Bing

Sure, here are some notes on Tuples in Python:

Definition

A tuple in Python is a built-in data type that allows you to store a collection of values in a single
variable.
It is an ordered sequence of elements, which can be of any data type such as integers, floats, strings, or
even other lists.
Tuples are immutable, meaning you cannot change their content.

Operations

Tuple operations: You can perform various operations on tuples such as concatenating tuples, repeating
the content of a tuple, reversing a tuple, and sorting a tuple.
Tuple slices: You can access a portion of the tuple using slicing.
Packing and Unpacking Tuples: You can pack multiple values into a single tuple or unpack values
from a tuple into multiple variables.

Properties

Immutability: Tuples are immutable which means you cannot change their content.
Aliasing: If a variable b is assigned with a variable a, then both a and b will be pointing to the same
tuple. If you make changes to a, then b will also get changed.
Cloning tuples: You can make a copy of a tuple with slicing so that modifying the copied tuple doesn’t
affect the original tuple.

Nested Tuples

Tuples can be nested which means you can have tuples within a tuple.

Here is an example of how to use tuples in Python:

# Creating a tuple
my_tuple = (1, 2, 3)

# Accessing elements from the tuple


print(my_tuple[0]) # Output: 1

# Slicing a tuple
print(my_tuple[1:3]) # Output: (2, 3)

# Nested Tuple
nested_tuple = ((1, 2), (3, 4))
print(nested_tuple[0]) # Output: (1, 2)

I hope this helps! Let me know if you have any other questions. 😊

You might also like