Page 92 - tp_Modula_v2.0
P. 92
08 TUPLE IN PYTHON
Your Aim
to learn about:
Creating a Tuple Accessing a Value in a Tuple
Unpacking Tuple Traversing Elements in a Tuple
Python Tuple Methods Python Functions
Operations on a Tuple Changing a Tuple
Deleting a Tuple Difference between List and Tuple
Some More Programs
In the previous chapter, you have learned about list. Similar to list, a tuple is a sequence of
values enclosed in parentheses and its indices start with 0. Unlike list, tuple cannot be changed
which means it is immutable. In this chapter, we will discuss how to create, access and perform
operations on tuples.
CREATING A TUPLE
To create a tuple, write different values and separate them with comma. You can put the
comma-separated values between the parentheses ‘( )’. Parentheses (round bracket) indicate the
start and end of the tuple.
Tuple contains any number of elements of heterogeneous type (integer, float, string, list, etc).
Example of tuple:
tup1=('sst', 'chemistry', 2010, 2022); #tuple with dissimilar items
tup2=(1, 2, 3, 4, 5 ); #tuple with similar items
tup3=(1, 2.5, 3.6, 4, 5 );
Program 1: To create a tuple.
Program1.py
File Edit Format Run Options Window Help
tuple1=(3, 4, 6, 7) # Homogeneous data elements
print(tuple1)
tuple2=(3, "Orange", 45.5) # Heterogeneous data elements
print(tuple2)
tuple3=(162, [7, "Orange", 15.5], (2, "Orange Education", 70.5)) # Nested tuple
print(tuple3)
90 Touchpad MODULAR (Version 2.0)

