0% found this document useful (0 votes)
13 views8 pages

10) Python Review Tuple in Python

Uploaded by

devanshdtt
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)
13 views8 pages

10) Python Review Tuple in Python

Uploaded by

devanshdtt
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
You are on page 1/ 8

TUPLE

 Tuple is a standard data type of Python that can store a sequence of values of any data
type in parenthesis( ). Tuple is immutable i.e. we can not change the elements of tuple.
Examples:
( ) – empty tuple
(2,3,4,5) – tuple of integer
(2.5, 3.2, 4.5) – tuple of decimal number(float)
(‘a’, ’b’, ’c’) – tuple of characters
(2, ’a’, 3.2, 8, 9 , ’b’) – tuple of mixed values

 Creating Tuple :- We have the following ways to create tuple:


1. By specifying values in parenthesis ( ):-
T1 = (2,5,7,5,9)
T2 = (‘h’,’ e’, ’l’, ’l’, ’o’)

2. Using tuple() method :- It create a individual element of tuple from the individual
element of the specified sequence as givren below.
Syntax : T = tuple(sequence)
Here sequence can be string, list or tuple
Examples:-
>>>T = tuple(“abc”) >>>L=[1,3,5,7] >>>T1=(3,2,8,7)
>>>T >>>T = tuple(L) >>>T2 = tuple(T1)
(‘a’, ’b’, ’c’) >>>T >>>T2
(1,3,5,7) (3,2,8,7)

3. Using input() method :- We can also enter tuple elements during the execution of
program as follows:
Syntax : T = tuple(input(“Enter elements of tuple : ”))

As we know that input() take the value from the user in form of string So the above
syntax will create a tuple of character. Because tuple() will take the argument as string.
Example:

T = tuple(input(“Enter elements of tuple”)) Output:


print(T) Enter elements of tuple: 1234
(‘1’, ‘2’, ‘3’, ‘4’)
4. Using eval() method :- The eval() identifies the type of the value entered by user and
assign the entered value in the same format to the variable i.e.
Syntax : a = eval(input(“Enter eny value : ”))

How to enter list How to enter tuple

a = eval(input(“Enter any value”)) a = eval(input(“Enter any value”))


print(a) print(a)

Output: Output:
Enter any value: [4,2,6,7,8] Enter any value: (2,3,4,5)
[4,2,6,7,8] (2,3,4,5)

 Nested Tuple:-
If the elemet of tuple is itself a tuple then that tuple is called Nested tuple.
Example:-
P = ( 2, 4, 5, (1,2), (5,8,9) )
 Indexing, Accessing and Traversing tuple elements:-
Indexing in tuple is similar to indexing in List. The individual element of tuple is
accessed by the help of its index. There are two types of indexing:
Forward Indexing and Backword Indexing
Example:- T = (4,7,9,8,20,15,12)
Forward indexing
0 1 2 3 4 5 6
4 7 9 8 20 15 12
-7 -6 -5 -4 -3 -2 -1

Backword indexing
Traversing tuple elements Using len( ) function:
T = (2,4,6,8) Output: T = (2,4,6,8) Output:
for a in T: 2 p = len(T) 2
print(a) 4 for a in range(0,p): 4
6 print(a) 6
8 8
TUPLE OPERATIONS
 Joining the Tuples using concatenation operator(+) :- This operator creates a new tuple
by joining two tuples.
Example:

>>>T1 = (2,4,6,8) >>>T1 = (1,3,5)


>>>T2 = (1,3,5) >>>T2 = T1+8
>>>T3 = T1+T2 error
>>>T3
(2,4,6,8,1,3,5)
In case of concatenation both operands must be tuple.
 Replicating tuple/Replication Operator (*) :- Like list we can replicate a tuple to
specified number of times. In cace of replicating tuple/list second argument must be
integer.
example:
>>>T = (1, 2, 3)
>>>T*2
(1, 2, 3,1, 2, 3)
TUPLE SLICING
 Slicing of tuple is similar to slicing in string/list. We can use indexes of tuple
elements to create tuple slice and slicing of tuple always return a tuple.
Syntax:
L = tuple_name[start:end:step]
• start - starting index from where slicing will starts
• end - Position till which slicing will takes place. Slicing stops at end-1.
• step – It determines the increment between each index for slicing.
Examples:- 0 1 2 3 4 5 6
T = (5,12,15,30,10,25,8)
5 12 15 30 10 25 8
-7 -6 -5 -4 -3 -2 -1
>>>T[2 : 6] >>>T[0 : 6 : 2]
(15, 30, 10, 25) (5, 15, 10)
>>>T[-6 : -2] >>>T[1 : 15 : 3]
(12, 15, 30, 10) (12, 10)
>>>T[3 : 10] >>>T[: : 2]
(30, 10, 25, 8) (5, 15, 10, 8)
TUPLE FUNCTIONS
len() This function return the length of tuple i.e. number of items in tuple.
>>>T=(1,3,5,9,2,4)
>>>n = len(T)
>>>n
5
max() This function return the maximum(largest) element of the specified tuple.
>>>T=(5,3,9,15,2,4)
>>>T.max()
15
min() This function return the minimum(smallest) element of the specified tuple.
>>>T=(5,3,9,15,2,4)
>>>T.min()
2
Note:- In case of max() and min() the type of the elements of tuple must
be same.
index() This function return the index number of the first occurrence of the
specified element in the tuple.
Example:
>>>T=(3,4,5,9,1,5,4)
>>>T.index(5)
2
TUPLE FUNCTIONS

count() This function returns the total number of occurrence of the


specified element in the tuple.
If the specified element is not present then it will return zero.

>>>T=(1,2,3,5,9,2,4,2) >>>T=(5,3,5,9,2,4)
>>>T.count(2) >>>T.count(8)
3 0

You might also like