Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
798 views
in Computer by (54.6k points)

What is tuple in Python and how can we access values in tuples ?

1 Answer

+1 vote
by (89.9k points)
selected by
 
Best answer

Tuple is a sequence of immutable Python object. Tuples are sequences, just like lists. The only difference is that tuples can’t be changed i.e.,tuples are immutable and tuples use parentheses and lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values and optionally you can put these comma -separated values between parentheses also. For example : 

tup1 = (‘physics’, ‘chemistry1,1997,2000) ; 

tup2 = (1, 2, 3, 4, 5); 

tup3 = “a”, “b”, “c”, “d”;

The empty tuple is written as two parentheses containing nothing : 

tup1 = ( ) ;

To write a tuple containing a single value you have to include a comma, even though there is only one 

tup1 = (50,);

Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on. Accessing Values in Tuples :

To access value in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index. Following is a simple example :

# !/user/bin/python 

tup1 = (‘physics’, ‘chemistry’, 1997,2000); 

tup2 = (1, 2, 3,4,5,6, 7); 

print “tup1[0]”, tup1[0] 

print “tup2[1:5]:”, tup2[1:5]

When the above code is executed, it produces the following result : 

tup1[0] : physics 

tup2[1:5]: [2, 3,4,5]

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Mar 21, 2023 in Python by kvdevika (111k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...