Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Operations on Tuples

In [18]:
# print items

tup1 = tuple(("Python", "R", "SQL", "C++"))


for x in tup1:
print(x)

Python
R
SQL
C++

In [4]:
# len()

tup1 = tuple(("Python", "R", "SQL", "C++"))


print(tup1, "\nlength of tup1 is: ", len(tup1))

('Python', 'R', 'SQL', 'C++')


length of tup1 is: 4

In [6]:
# concatenation(+)

tup1 = tuple(("Python", "R", "SQL", "C++"))


tup2 = tuple((9, 99, 999, 9999))
print("Concatenated tuple is: ", tup1 + tup2)

Concatenated tuple is: ('Python', 'R', 'SQL', 'C++', 9, 99,


999, 9999)

In [7]:
# repeatition(*)

tup2 = tuple((9, 99, 999, 9999))


reptup = tup2*3
print("Repeated tuple is: ", reptup)

Repeated tuple is: (9, 99, 999, 9999, 9, 99, 999, 9999, 9, 9
9, 999, 9999)

In [8]:
# item existence

tup2 = tuple((9, 99, 999, 9999))


print("999 is in tuple: ", 999 in tup2)
999 is in tuple: True

In [17]:
# slicing

tup1 = tuple(("Python", "R", "SQL", "C++"))


print("ML languages: ", tup1[0:2])
print("OOP language: ", tup1[3])

ML languages: ('Python', 'R')


OOP language: C++

In [9]:
# append

tup1 = ("Python", "R", "SQL", "C++")


x = list(tup1)
x.append("C#")
tup1 = tuple(x)
print("Tuple after appending: ", tup1)

Tuple after appending: ('Python', 'R', 'SQL', 'C++', 'C#')

In [11]:
# delete

tup1 = tuple(("Python", "R", "SQL", "C++"))


print(tup1)
del tup1
print("As tup1 no longer exist, following error is generated
print(tup1)

('Python', 'R', 'SQL', 'C++')


As tup1 no longer exist, following error is generated:

-------------------------------------------------------------
--------------
NameError Traceback (most rec
ent call last)
~\AppData\Local\Temp/ipykernel_20328/681008889.py in <module>
5 del tup1
6 print("As tup1 no longer exist, following error is ge
nerated:\n")
----> 7 print(tup1)

NameError: name 'tup1' is not defined

You might also like