Python Tuples

A tuple is a collection of ordered and immutable (unchangeable) elements.

It is similar to a list in Python, but the main difference is that tuples are immutable, meaning that their elements cannot be modified once they are created.

Tuples are often used to store related pieces of data, such as the x and y coordinates of a point in a 2D space.

Creating a Tuple

A tuple can be created by enclosing elements within parentheses ( ), separated by commas ,.

For example:

# creating a tuple with 3 elements
my_tuple = (1, "hello", 3.14)

You can also create a tuple without using parentheses, by separating the elements with commas:

# creating a tuple without using parentheses
my_tuple = 1, "hello", 3.14

It's also possible to create a tuple with only one element, called a singleton tuple:

# creating a singleton tuple
my_tuple = (1,)

Note that a single element tuple with parentheses and without parentheses is different, the first is a tuple the second is an int or any type:

# Examples
tup = (1) # not tuple
tup = (1,) # tuple

Accessing Tuple Elements

You can access individual elements of a tuple by using indexing, just like with lists.

The indexing starts at 0, so the first element has an index of 0, the second has an index of 1, and so on.

For example:

# accessing the first element of a tuple
print(my_tuple[0]) # Output: 1

# accessing the second element of a tuple
print(my_tuple[1]) # Output: "hello"

# accessing the third element of a tuple
print(my_tuple[2]) # Output: 3.14

You can also use negative indexing to access elements from the end of the tuple.

For example:

# accessing the last element of a tuple
print(my_tuple[-1]) # Output: 3.14

# accessing the second to last element of a tuple
print(my_tuple[-2]) # Output: "hello"

Tuple Slicing

You can also use slicing to access a range of elements in a tuple.

The syntax for slicing is similar to that of lists, but since tuples are immutable, you cannot use slicing to change elements in a tuple.

For example:

# accessing the first two elements of a tuple
print(my_tuple[0:2]) # Output: (1, "hello")

# accessing the last two elements of a tuple
print(my_tuple[-2:]) # Output: ("hello", 3.14)

Tuple Unpacking

You can also use tuple unpacking to assign the elements of a tuple to separate variables.

For example:

x, y, z = my_tuple
print(x) # Output: 1
print(y) # Output: "hello"
print(z) # Output: 3.14

Tuple Concatenation and Repetition

You can use the + operator to concatenate two tuples together, creating a new tuple that contains all the elements from both original tuples.

For example:

# concatenating two tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
tuple3 = tuple1 + tuple2
print(tuple3) # Output: (1, 2, 3, 4, 5, 6)

You can also use the * operator to repeat a tuple a certain number of times.

For example:

# repeating a tuple 3 times
tuple4 = tuple1 * 3
print(tuple4) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)

Tuple Methods

Although tuples are immutable, they do have some built-in methods that can be used to perform certain operations.

Some of the most commonly used tuple methods are:

count(): returns the number of times a specified element appears in the tuple.

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

index(): returns the index of the first occurrence of a specified element in the tuple.

print(tuple5.index(4)) # Output: 4

len(): returns the number of elements in the tuple.

print(len(tuple5)) # Output: 6

In conclusion, Python tuples are a useful data structure that allows you to store multiple related pieces of data in an ordered and immutable way.

They are similar to lists in many ways, but offer the added benefit of immutability, which can be useful in certain situations.

With the knowledge of creating, accessing, slicing, unpacking, concatenating and repetition, and methods of tuple you are ready to use tuple in your projects.

Python Basics