In this article Basic Tuples Operations we give the information about Tuple is an immutable data structure in Python. A variety of basic operations can be performed on a tuple. Here are the main operations performed with Tuples:
Basic Tuples Operations:
- Concatenation
The + operator can be used to join two tuples.
Example:
tuple1 = (1, 2, 3)
tuple2 = (4, 5)
# Adding tuple
result = tuple1 + tuple2
print(result) # Output: (1, 2, 3, 4, 5)
- Multiplying Tuple (Repetition)
The * operator can be used to multiply a tuple by a number.
Example:
my_tuple = (1, 2, 3)
# multiplying tuple 3 times
result = my_tuple * 3
print(result) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
- Getting the Length of Tuple
The len() function is used to find how many elements a tuple contains.
Example:
my_tuple = (10, 20, 30, 40)
# length of tuple
length = len(my_tuple)
print(length) # Output: 4
-
Minimum and Maximum Value in Tuple
min() and max() functions are used to find the smallest and largest value in a tuple.
Example:
my_tuple = (5, 10, 15, 20)
# minimum and maximum values
print(min(my_tuple)) # Output: 5
print(max(my_tuple)) # Output: 20
- Repeated occurrence of a value in a tuple (Count)
count() method is used to find the number of a particular value.
Example:
my_tuple = (1, 2, 2, 3, 4, 2)
#2 How many times has it come
count = my_tuple.count(2)
print(count) # Output: 3
- Knowing the position of a value in a tuple (Index)
index() method is used to find the first position (index) of a particular value.
Example:
my_tuple = (10, 20, 30, 40, 20)
#20 first place
index = my_tuple.index(20)
print(index) # Output: 1
-
Membership Testing
in and not in are used to check whether a particular value is present in a Tuple or not.
Example:
my_tuple = (1, 2, 3, 4)
# checking membership
print(2 in my_tuple) # Output: True
print(5 not in my_tuple) # Output: True
- Iterating through Tuple
For loop is used to access all the values of a tuple one by one.
Example:
my_tuple = (10, 20, 30)
# iterating the tuple
for item in my_tuple:
print(item)
#Output:
#10
#20
#30
- Sorting the Tuple
Sorting is done by first converting the Tuple into a list because Tuple is immutable.
Example:
my_tuple = (30, 10, 20)
# Sorting Tuple by converting it into List
sorted_tuple = tuple(sorted(my_tuple))
print(sorted_tuple) # Output: (10, 20, 30)
- Adding or Removing Elements from a Tuple
- To add or remove a tuple, first transform the tuple into a list.
- Then convert the list into Tuple.
Example:
my_tuple = (1, 2, 3)
# Change and add to List
temp_list = list(my_tuple)
temp_list.append(4)
my_tuple = tuple(temp_list)
print(my_tuple) # Output: (1, 2, 3, 4)
Conclusion:
- Many operations can be performed on a Tuple like addition, multiplication, length checking, min/max values, and membership.
- Tuple cannot be changed directly because it is immutable.
- If a Tuple needs to be converted, first convert it to a list and then back to a Tuple.
Indexing, Slicing:
Indexing and Slicing are used to access data in data structures like Tuple, List, and String in Python.
- Indexing
It is used to access a particular element of a Tuple (or other data structures).
- Index always starts from zero (0).
- You can use both Positive Indexing and Negative Indexing.
Example:
my_tuple = (10, 20, 30, 40, 50)
# Positive Indexing
print(my_tuple[0]) # Output: 10 (first element)
print(my_tuple[3]) # Output: 40 (fourth element)
# Negative Indexing (counting from the last)
print(my_tuple[-1]) # Output: 50 (last element)
print(my_tuple[-3]) # Output: 30 (3rd element)
Error Handling:
If you use an Index that does not exist in the Tuple, an IndexError will occur.
my_tuple = (1, 2, 3)
print(my_tuple[5]) # Output: IndexError: tuple index out of range
-
Slicing
Slicing is used to extract a sub-part from a tuple (or other data structures).
Syntax:
tuple_name[start:end:step]
- start: The location from which slicing will start (default: 0).
- end: The location up to which slicing will occur (this location will not be included).
- step: At how many steps data will be taken (default: 1).
Example:
my_tuple = (10, 20, 30, 40, 50)
# Use of Slicing
print(my_tuple[1:4]) # Output: (20, 30, 40)
print(my_tuple[:3]) # Output: (10, 20, 30) (from start to 3rd index)
print(my_tuple[2:]) # Output: (30, 40, 50) (from second index to last)
- Slicing with Negative Indexing
You can also do slicing using a negative index.
Example:
my_tuple = (10, 20, 30, 40, 50)
# Use of Negative Index
print(my_tuple[-4:-1]) # Output: (20, 30, 40)
print(my_tuple[-3:]) # Output: (30, 40, 50)
-
Use of Step (Skipping Elements)
In step slicing it determines how many elements to skip at a time.
Example:
my_tuple = (10, 20, 30, 40, 50)
# slicing with step
print(my_tuple[::2]) # Output: (10, 30, 50) (takes every other element)
print(my_tuple[1::2]) # Output: (20, 40) (takes every second element from the first index)
Use of Negative Step:
Tuple can be reversed by using negative step.
my_tuple = (10, 20, 30, 40, 50)
# Inverting the tuple
print(my_tuple[::-1]) # Output: (50, 40, 30, 20, 10)
- Using Slicing to Change Part of a Tuple (Indirect Update)
Tuple cannot be updated directly because it is immutable. But new Tuple can be created using slicing.
Example:
my_tuple = (1, 2, 3, 4, 5)
# Removing the second and fourth elements
new_tuple = my_tuple[:1] + my_tuple[3:]
print(new_tuple) # Output: (1, 4, 5)
Key Points of Indexing and Slicing:
- A particular element of a Tuple is accessed using Indexing.
- A sub-part of the Tuple is accessed using Slicing.
- Tuple can be accessed in reverse or custom way using Negative Indexing and Step.
- Tuple cannot be updated directly, but new tuple can be created using slicing and concatenation.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining