ONLINE SUMMER TRAINING Online Courses Free Tutorials 
 Placement Preparation 
Artificial Intelligence(AI) & Machine Learning(ML) Training in Jaipur
0 like 0 dislike
288 views
in RTU/BTU B.Tech(CSE-VI SEM) PYTHON LAB by Goeduhub's Expert (7.6k points)
Write a program to demonstrate tuple in python

Goeduhub's Online Courses @Udemy

For Indian Students- INR 570/- || For International Students- $12.99/-

S.No.

Course Name

Apply Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Computer Vision with OpenCV | Deep Learning CNN Projects

Apply Coupon

3.

Complete Machine Learning & Data Science with Python Apply Coupon

4.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

5.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon

6.

Complete Python Programming from scratch with Projects Apply Coupon

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Write a program to demonstrate tuple in python

What is tuple in Python with example?

Tuple is a sequence of python objects like list with immutable feature. We cannot add or remove an item in tuple. Tuples are created by ( ). It contains heterogeneous data-types. 

eg: t=(1,2,'hello',20.5)

 

# An empty tuple 

t = () 

print (t)

tup = 'python', 'tuple'

print(tup) 

Output 

() 

('python', 'tuple')

# Another for doing the same 

tup = ('python', 'GoEduHub') 

print(tup) 

Output

('python', 'GoEduHub')

Functions in  Tuple

len(tuple)

Gives the total length of the tuple.

max(tuple)Returns item from the tuple with max value.
min(tuple)

Returns item from the tuple with min value.

tuple(seq)

Converts a list into tuple.

# concatenating 2 tuples 

tuple1 = (0, 1, 2, 3) 

tuple2 = ('python', 'GoEduhub') 

print(tuple1 + tuple2) 

Output

(0, 1, 2, 3, 'python', 'GoEduhub')

#nested tupples

tuple1 = (0, 1, 2, 3) 

tuple2 = ('python', 'GoEduHub') 

tuple3 = (tuple1, tuple2) 

print(tuple3)

Output

((0, 1, 2, 3), ('python', 'GoEduHub'))

#tuple repitition

tuple3 = ('python',)*3  

print(tuple3) 

Output

('python', 'python', 'python')

#tuples are immutable 

tuple1 = (0, 1, 2, 3) 

tuple1[0] = 4

print(tuple1) 

Output

#slicing 

tuple1 = (0 ,1, 2, 3) 

print(tuple1[1:]) 

print(tuple1[::-1]) 

print(tuple1[2:4])

Output

(1, 2, 3) 

(3, 2, 1, 0) 

(2, 3)

#deleting a tuple 

tuple3 = ( 0, 1) 

del tuple3 

print(tuple3)

Output

#length of a tuple 

tuple2 = ('python', 'goeduhub') 

print(len(tuple2)) 

Output

2

#converting a list and a string into a tuple 

list1 = [0, 1, 2] 

print(tuple(list1)) 

print(tuple('python')) # string 'python' 

Output

(0, 1, 2) 

('p', 'y', 't', 'h', 'o', 'n')

#tuples in a loop 

tup = ('h','e','l','l','o') 

for i in tup:  

    print(i) 

Output

h

e

l

l

o


For more Rajasthan Technical University CSE VI Sem Python Lab Experiments Click here

Our Mentors(For AI-ML)


Sharda Godara Chaudhary

Mrs. Sharda Godara Chaudhary

An alumna of MNIT-Jaipur and ACCENTURE, Pune

NISHA (IIT BHU)

Ms. Nisha

An alumna of IIT-BHU

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...