You are on page 1of 10

c la

ss
XI
COMPUTER SCIENCE I

with
PYTHON

Computer Science Department(Krishna Public


School)
CHAPTER- 1
Sujeet Tiwari Review of Python Basics: Tuple
Mr. Liju K John
Mrs Rubeena Mirza (PART-VII)
Mrs Disha Dhupar
Topics
1. Flow of Execution
2. String
3. List
4. Tuples
5. Dictionary
6. Sorting Techniques

Computer Science with PYTHON class XII Chaper-1: Tuple 2


4. Tuple
Definition: Tuple is a sequence of immutable python objects.
Syntax:
Tuple_Variable=(Item1,item2….)
Items can be as following,
1. (1,2,3,4,….)
2. (a,b,c,d,….)
3. (“I1”,”I2”,”I3,…)
4. ((item1,itm2,itm3),(itm4,itm5),(….),(….))
5. Combination of above or other.
Computer Science with PYTHON class XII Chaper-1: Tuple 3
4. tuple: Indexing
• In tuple, to specify each element uniquely an index value is used
Ex: TUPLE=(10,20,30,40,50)
0 1 2 3 4 Positive Index
10 20 30 40 50 TUPLE
-5 -4 -3 -2 -1 Negative Index

To retrieve data directly from list, index used as following


>>>print(TUPLE[2])
Output=>30
>>>print(TUPLE[-3])
Output=>30
Computer Science with PYTHON class XII Chaper-1: Tuple 4
4. Tuple: Indexing into nested list
• In Tuple, to specify each element uniquely an index value is used
Ex: TUPLE=((10,20,30),(40,50,60)) Tuple-2
Tuple-1
0 1 Positive Index
0 1 2 0 1 2
10 20 30 40 50 60 Tuple
-3 -2 -1 -3 -2 -1
-1 -2 Negative Index

To retrieve data directly from Tuple, index used as following


>>>print(TUPLE[1])
Output=>(40,50,60)
>>>print(TUPLE[1][2])
Output=>60
Computer Science with PYTHON class XII Chaper-1: Tuple 5
4. Tuple: Slicing
syntax: Tuple[start: stop: step]
Error, while passing empty index

Only start index, it only gives specific index value


Only stop index, which take value from zero index to
stop index-1 , output=>[10,20]
Start and stop index, which take value from start index
up to stop index-1, output=>[30,40,50,60,70]
Start index, stop index and step, here it start from start
index, takes value up to stop index -1 with difference of
index from start index +/- step value, here index 3 and
5skeepd, so we get output=>[30,50,70] of index 2,4,6
Copy one list to another list

Try the Trick: tup2=tup1[ : :-1]


Output=> ?
Computer Science with PYTHON class XII Chaper-1: Tuple 6
4. Tuple: Built-in Functions and Methods
Sr. Function Description

1 len(tuple) Return the total length of


the tuple

2 max(tuple) Returns the maximum item


value of tuple

3 min(tuple) Returns the minimum item


value of tuple

4 tuple(seq) Converts list into list


5 sum(list) Sum up all the numeric
values present in the tuple

Computer Science with PYTHON class XII Chaper-1: Tuple 7


4. Tuple: Built-in Functions and Methods
Sr. Method Description Program>>>t1=(40,3,20,0,14)
1 index(item) Returns index of specific item, when no >>>t1.index(20)
item found a ValueError exception occur

Computer Science with PYTHON class XII Chaper-1: Tuple 8


4. Difference between Tuple & List
Sr. Tuple List
1 Mutability Immutable Mutable

2 Syntax Ues (…) to create tuple Use […] to create list


Ex: tup1=(1,2,3) Ex:List1=[1,2,3]

Note: Once tuple is created, it can’t be modified, but we can recreate it again
Ex:
>>>Tup1=(1,2,3,4)
>>>print(Tup1) This line help us to
>>>Tup1=Tup1+(5,) recreate new tuple
>>>print(Tup1) with existing tuple
value
Here, we are not adding new value to tuple, but we are creating same tuple again, so it overlaps old tuple

Computer Science with PYTHON class XII Chaper-1: Tuple 9


Thankyou
Computer Science Department
(Krishna Public School, Koni, Bilaspur)
Sujeet Tiwari
Mr. Liju K John
Mrs Rubeena Mirza
Mrs Disha Dhupar

Computer Science with PYTHON class XII Chaper-1: Tuple 10

You might also like