Tuple and its Basic Operations in Python - Python Basics for Beginners

Programming

Posted on March 22, 2025
by Rajjit Laishram Admin

Updated on March 22, 2025

Cover image for Tuple and its Basic Operations in Python - Python Basics for Beginners

Tuple and its Basic Operations in Python - Python Basics for Beginners A tuple is a sequence in which elements are written as a list of comma separated values between parenthesis. Let's say an example, there is a tuple named tup_var = (1, 2, 3, 4, 5, 6).

It is an immutable data type, where its elements cannot be changed. We can also access the values in a tuple, slice the values, update and even delete them.

Let us look at an example below:
new_Tup = (1, 2, 3, 4,5 ,6, 7, 8, 9, 10)
print(type(new_Tup))
# access + slicing
print("First:", new_Tup[0])
print("Slicing [3:6]: ", new_Tup[3:6])
print("[:8]", new_Tup[:8])

# update or concatenation
new_Tup2 = (31, 34, 56, 77)
new_Tup3 = new_Tup + new_Tup2
print(new_Tup)

# delete
try:
    del new_Tup
    print("new_Tup deleted successfully")
except NameError as e:
    print(e)
print(new_Tup)
Nested Tuple:

It is a tuple within another tuple. We will understand the nested tuple using an example.

nested_tup = (1, 2, 3, 4, ("hello", "mister"), (3.6, 88.08), "Welcome to RJ Coding Tips")
print(nested_tup)
Basic Operations

Some operations include: length, repetition, concatenation, membership, iteration, comparison, max, min and conversion to tuple. Let's see one by one using example.

#len
lenOfTup = len(new_Tup)
print(lenOfTup)

# repetition
new_Tup2 = ("Hello"*2, "Programmers"*3)
print(new_Tup2)

# membership
check_mem = 5 in new_Tup
print(check_mem)

# iteration
for i in new_Tup:
    print(i)

# comparison # <, > , ==
new_Tup2 = (1, 2,3, 4, 5, 6, 7, 8, 9, 10)
print(new_Tup == new_Tup2)

# max and min
maxTup = max(new_Tup2)
minTup = min(new_Tup2)
print(maxTup)
print(minTup)

# convert to tuple
newString = "Welcome to RJ Coding tips. Hit that Subscribe Button"
newTup = tuple(newString)
print(newTup)

Don't forget to watch the full video on YouTube:
Some extra notes on
Advantages of TUPLE over LIST
  1. Tuples perform much better than a list because tuples are immutable and iteration is faster.
  2. Tuples are used for storing data that is write-protected. That is, once written, cannot be changed.
  3. Tuples are used for string formatting.
  4. Multiple values from a function can be returned using a tuple.
  5. Tuples can be used where the number of values is known and small.

Thank you so much for reading! Feel free to comment your thoughts below!! 😊
No comments found. Be the first one to comment!

Add a comment

NOTE: You cannot delete your comment once added, so please don't post unnecessary comments!