Programming
Posted on March 22, 2025
by Rajjit Laishram Admin
Updated on March 22, 2025
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 OperationsSome 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 onAdvantages of TUPLE over LIST
Thank you so much for reading! Feel free to comment your thoughts below!! 😊
NOTE: You cannot delete your comment once added, so please don't post unnecessary comments!