CH2. Python Revision Tour - II: Cbse - Class - Xii Computer Science With Python (NEW) (Subject Code: 083)
CH2. Python Revision Tour - II: Cbse - Class - Xii Computer Science With Python (NEW) (Subject Code: 083)
CH2. Python Revision Tour - II: Cbse - Class - Xii Computer Science With Python (NEW) (Subject Code: 083)
Strings in Python
Lists in Python
Tuples in Python
Dictionaries in Python
Sorting- bubble sort & insertion
sort
4
5
Tuples in 4
6
Python
Tuple is a standard data type of Python that can store a
sequence of values belonging to any type.
Tuples are depicted through parenthesis i.e. round brackets.
Tuples are immutable sequence i.e. element cannot be changed
in place.
Example
–t1=(1,2,3,4,5)
–t2=('p','r','o','b','l','e','m‘)
–t3=('pan','ran','oggi','blade','lemon','egg','mango‘)
Creating 4
7
Tuples
Tuples can be created by assigning a variable with the values
enclosed in round bracket separated by comma.
Example: t1=(1,2,3,4,5)
Tuples can be created in different ways:
Empty Tuple:
Tuple with no item is a empty tuple. It can be created with the function
T=tuple( ). it generates the empty tuple with the name T. This list is
equivalent to 0 or ‘ ’.
Single Element
Tuple:
Tuple with one item.
T1=(9,) or T1=9, comma is required because Python treats T1=(9) as value
not as tuple element.
Creating 4
8
Tuples
Creating Tuple from Existing
Sequence:
T1=tuple(<sequence>)
Example:
T1=tuple(‘Computer’)
>>>T1
(’C’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)
Creating Tuple from Keyboard 4
9
Input
Tuples vs 5
0
List
Similarities:
Length
Indexing Slicing
Membership operators
Concatenation and Replication operators
Accessing Individual elements
Differences
Mutability: Tuples are not mutable, while list
are.
Tuple 5
1
Operations
Joining Tuples: Two tuples can be joined through addition.
>>>t1=(1,2,3)
>>>t2=(4,5,6)
>>>t3=t1+t2
>>>t3
(1,2,3,4,5,6)
The max( ) Method: This function returns the element from the
tuple having maximum value .
max(<tuple>)
The min( ) Method: This function returns the element from the
tuple having minimum value .
min(<tuple>)
Tuple Functions and 5
7
Methods
Tuple Functions and 5
8
Methods
The index( ) Method: This function returns the index of first
matched item from the tuple.
Tuple.index(<item>)
If item is not in the list it raises exception value
The count( ) Method: This function returns the count of the item
passed as argument. If given item is not in the tuple it returns
zero.
tuple.count(<item>)
Tuple Functions and 5
9
Methods
len () | min () | max () | Count
index() ()
Tuple Functions and 6
0
Methods
The tuple( ) Method: This function creates tuples from different types of
values.
tuple(<sequence>)
tuple( ) can receive argument of
sequence type only, like string or list
or dictionary. Any other type of
value will lead to an error.