0% found this document useful (0 votes)
2 views5 pages

Python Programming

The document is a Jupyter Notebook tutorial on Tuples in Python, covering their creation, access, updating, and deletion. It includes examples of basic operations, nested tuples, and various tuple methods, as well as programs for counting positive, negative, and zero values, summing even and odd numbers, and swapping values. The tutorial is authored by Dr. Selva Rani B and provides practical coding examples throughout.

Uploaded by

shristi010104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Python Programming

The document is a Jupyter Notebook tutorial on Tuples in Python, covering their creation, access, updating, and deletion. It includes examples of basic operations, nested tuples, and various tuple methods, as well as programs for counting positive, negative, and zero values, summing even and odd numbers, and swapping values. The tutorial is authored by Dr. Selva Rani B and provides practical coding examples throughout.

Uploaded by

shristi010104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1/27/25, 12:50 PM Tuples-25-09-24 - Jupyter Notebook

Tuples in Python

By
Dr. Selva Rani B, SCORE

1. Creating Tuples - (a) Empty Tuples

In [22]:  T1 = ()
print(type(T1))

T2 = tuple()

print(type(T2))

<class 'tuple'>
<class 'tuple'>

1. Creating Tuples - (b) Non Empty Tuples

In [24]:  T6 = (10,)
print(type(T6))

<class 'tuple'>

In [25]:  a, b = 10, 20
print(a,b)

10 20

In [26]:  Q, R = divmod(10,3)
print("Quotient : ", Q)
print("Remainder : ", R)

Quotient : 3
Remainder : 1

2. Accessing the elements in a Tuple

In [27]:  T3 = (1, 9, 4, 3, 0, 2, 5)
T4 = ('Python', 'C', 'C++', 'Java')
T5 = ('19BAR0007', 'Nithik V', 9.8)
print(T3[-4])
print(T4[0])
print(T5[:])

3
Python
('19BAR0007', 'Nithik V', 9.8)

localhost:8888/notebooks/[Link] 1/5
1/27/25, 12:50 PM Tuples-25-09-24 - Jupyter Notebook

3. Updating a Tuple

In [28]:  T4 = ('Python', 'C', 'C++', 'Java')


T5 = ('19BAR0007', 'Nithik V', 9.8)
T6 = T4 + T5
print(T6)

('Python', 'C', 'C++', 'Java', '19BAR0007', 'Nithik V', 9.8)

4. Deleting elements in a Tuple

In [29]:  del T4[2]

---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
<ipython-input-29-aad645217ec7> in <module>
----> 1 del T4[2]

TypeError: 'tuple' object doesn't support item deletion

In [30]:  del T4
print(T4)

---------------------------------------------------------------------
------
NameError Traceback (most recent call
last)
<ipython-input-30-0935fbc2e704> in <module>
1 del T4
----> 2 print(T4)

NameError: name 'T4' is not defined

5. Basic Operations in Tuples

localhost:8888/notebooks/[Link] 2/5
1/27/25, 12:50 PM Tuples-25-09-24 - Jupyter Notebook

In [31]:  # Length
print(len(T5))

# Concatenation
print(T3 + T5)

# Repetition
print(T5 * 2)

# Membership
print('Nithik V' in T6)
print('Pava' in T6)

# Iteration
for ele in T6:
print(ele)

# Maximum
print(max(T3))

# Minimum
print(min(T3))

# Sum
print(sum(T3))

# Tuple from String
T7 = tuple('Python')
print(T7)

# Tuple from List
T8 = tuple([10, 20, 30, 40, 50])
print(T8)

3
(1, 9, 4, 3, 0, 2, 5, '19BAR0007', 'Nithik V', 9.8)
('19BAR0007', 'Nithik V', 9.8, '19BAR0007', 'Nithik V', 9.8)
True
False
Python
C
C++
Java
19BAR0007
Nithik V
9.8
9
0
24
('P', 'y', 't', 'h', 'o', 'n')
(10, 20, 30, 40, 50)

6. Nested Tuples

localhost:8888/notebooks/[Link] 3/5
1/27/25, 12:50 PM Tuples-25-09-24 - Jupyter Notebook

In [32]:  Items = (('Zebronic', 750),('Boult', 1099),('Realme', 1599))


for i in Items:
print(i)

('Zebronic', 750)
('Boult', 1099)
('Realme', 1599)

7. Tuple Methods

In [33]:  print([Link]('h'))
print([Link](10))
print(sorted(T7))

3
1
['P', 'h', 'n', 'o', 't', 'y']

8. zip()

In [34]:  T = (1, 2, 3, 4, 5)
L = ['A', 'B', 'C', 'D', 'E']
print(list(zip(T,L)))

[(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D'), (5, 'E')]

9. Program to count the number of +s, -s, and 0s

In [ ]:  T = (9, -8, 0, 6, -4, 3, 0, -2, 1, 7)


np = 0
nn = 0
nz = 0
ne = 0
no = 0
for e in T:
if e > 0:
np = np + 1
elif e < 0:
nn = nn + 1
else:
nz = nz + 1
print("Number of Positives : ", np)
print("Number of Negatives : ", nn)
print("Number of Zeros : ", nz)

10. Program to count the number of even and odd numbers

localhost:8888/notebooks/[Link] 4/5
1/27/25, 12:50 PM Tuples-25-09-24 - Jupyter Notebook

In [ ]:  s_odd = 0
s_even = 0
for i in T:
if i % 2 == 0:
s_even = s_even + i
else:
s_odd = s_odd + i
print('Sum of Odd : ', s_odd)
print('Sum of Even : ', s_even)

11. Program to swap two values

In [ ]:  A = 10
B = 20
print("Before Swapping :")
print("A = ", A, "B = ", B)
print()
(A, B) = (B, A)
print("After Swapping :")
print("A = ", A, "B = ", B)
print()

In [ ]:  email = "selva@[Link]"
usr, dom = [Link]('@')
print("User Name : ", usr)
print("Domain : ", dom)
print()

In [ ]:  T = (1, 0, -9, 8, -4, 10, -2)


newT = ()
for i in T:
if i > 0:
newT += (i,)
print(newT)

localhost:8888/notebooks/[Link] 5/5

You might also like