Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CH2. Python Revision Tour - II: Cbse - Class - Xii Computer Science With Python (NEW) (Subject Code: 083)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

CH2.

Python Revision Tour - II


CBSE – CLASS – XII
COMPUTER SCIENCE WITH PYTHON
(NEW)
(SUBJECT CODE : 083)
Objective 2

 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)

 Repeating or Replicating Tuples: Multiply(*) operator replicates


the
tuple specified number of times
 >>>t1=(1,2,3)
 >>>t1*3
 (1,2,3,1,2,3,1,2,3)
Tuple 5
2
Operations
 Slicing the Tuples: Tuple slices are the subpart of a tuple
extracted out. Tuple slices can be created through the use of
indexes.
 Seq=Tuple[start:stop] : creates tuple slice out of t1 with
element falling in between indexes start and stop not
including stop.
 >>>t1=(1,2,3,4,5,6,7,8)
 >>>seq=t1[2:-3]
 >>>seq
 (3,4,5)
Tuple 5
3
Operations
 tuples also supports slice steps.
 Example,
 Seq=Tuple[start:stop:step] creates tuple slice out of tuple with
element falling in between indexes start and stop not including
stop, skipping step-1 element in between.
 >>>t1=(1,2,3,4,5,6,7,8)
 >>>seq=t1[2:7:2]
 >>>seq (3,5,7)
Unpacking 5
4
Tuples
Forming a tuple from individual values is called packing and

creating
individual values from a tuple’s elements is called unpacking.
Deleting 5
5
Tuples
 We cannot delete individual item of a tuple.
 del statement deletes the complete tuple.----
Why?
Tuple Functions and 5
6
Methods
 The len( ) Method: This function returns the length of the tuple,
i.e. the count of elements in the tuple.
 len(<tuple>)

 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.

You might also like