List PPT CH 11 As On Sunday 18th

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

COMPUTER SCIENCE-

XI
N.GEETHA MSc(IT),BE.d.,
Subject: Computer
Topic : Python
Program
Std : XII-A
Find out
Clue for today’s Topic
CHAPTER – 5
file MANIPULATION
in Python
file Manipulation
 Lists
 Creating Lists.
 Types of Lists.
 Methods in Lists.
 Operators in Lists.
 Example program using Lists.
file Manipulation
Creating and accessing Lists
 You can create a list by enclosing a comma-
separated sequence of items within square
brackets [ ].
 Lists can contain elements of different data
types.

 Python Lists are Mutable.


Creating LIST
S.N Lists Explanation
O

1 [] List with no number ,empty list


2 [1,2,3] List of integers
3 [1,1.2] List of (int and float)
4 [‘a’,’b’] Lists of characters
5 [a,1,’one’] List of mixed value types
6 [‘one,’two’,’Three’] List of strings
Types of Lists:
The Empty list #L=list [ ]
Long List #L=list[1,2,3,4,5,6,7,8,9,10,11,
12,13,14 ]
Nested List #L=[3,4,[5,6],7,]
Methods in Lists.
List
len()

index()

append()

insert()

extend()

remove()

pop()

clear()

reverse()
len():Returns the number of items in the list
index( ):You can get the Index value of an
element
Accessing index value in
Lists
• Elements of list can be accessed using index same as strings i.e.,
forward indexing as 0,1,2,3…. and backward indexing as -1,-2,-3,…

• The list name stores reference(memory location) of where its


elements are stored index wise.
index( ):You can get the Index value of an
element
index( ):You can get the Index value of an
element
Section Break
find the output
x=[50,46,82, 39,16] Answers:
 len(x)
 x[3]
 x[-2]
 x[4]
find the output
1.List can contain values of these types:
a)Integers b)floats c)Both A&B

2.Which of the following will create an empty list?


a)L = [ ] b)L = list(0) c)L = list(no )

3.List are ______________________.


a)Mutable b)Immutable c)Both A&B
find the output
find the output
find the output
List operations
• Slicing the Lists:
⮚ To create list slices as per following format:
seq = L [start : stop : step]
It creates a list slice out of list L with elements falling between indexes start
and stop, not including stop skipping step-1 elements in between.
List operations
List operations
List operations
List operations
List operations
List operations
find the output
1.Given a list L= [10, 20, 30, 40, 50, 60, 70], what would L[1 : 4]
return?
[10, 20, 30, 40]
[20, 30, 40, 50]
[20, 30, 40]
[30, 40, 50]
Ans:_______________________________
Traversing a LIST
• Traversal of a sequence means accessing and processing each
element of it.
• The for loop makes it easy to traverse or loop over the times in a list.
Traversing a LIST
comparing LISTs
• Two lists can be compared using comparison
operators like <,>,<=,>=,==,!=
• Eg:
List operations
• Joining Lists:
⮚ Concatenation operator + is used to join two lists.
⮚ Eg: [1,2,3] + [5,6,7] results in [1,2,3,5,6,7]
⮚ [1,2,3] +4 gives error
⮚ [1,2,3] + ‘abc’ gives error
List operations
• Repeating or Replicating Lists:
⮚ To replicate a list for specified number of times use * operator.
⮚ Eg: [1,2]*3 result in [1,2,1,2,1,2]
Repeating or Replicating Lists:
append ( ):-The append function is used to
add an element at end of the list
5. extend() : used for adding multiple elements to a list.
• Syntax : list . extend(<list>)
6. insert() : inserts an element somewhere in between or any
position of your choice.
But append() and extend() insert the element at the end of the list.
• Syntax : list . insert(<ind> , <item>)
7. pop() : remove the item from the list.
• Syntax : list . pop(<index>)
• Takes one optional argument and returns a value – the item being
deleted.
• If no index is specified, pop() removes and returns the last item in
the list.
8. remove() : remove the first occurrence of given item from the list
• Syntax : list . remove(<value>)
• Takes one essential argument and does not return anything.
Difference between pop() and remove()

• pop() removes an individual item and returns it, while remove()


searches for an item, and removes the first matching item from
the list and it will not return any value.
9. clear() : removes all the items from the list and the list becomes
empty list after this function. This function returns nothing.
• Syntax : list . clear()
10. count() : returns the count of the item that you passed as
argument.
• If the given item is not in the list, it returns zero.
• Syntax : list . count()
11. reverse() : reverses the item of the list.
• It does not create any new list but replaces the existing list.
• Does not return anything.
• Syntax : list . reverse()
12. min(), max(), sum(): It takes the sequence and return the minimum,
maximum and sum of the elements of the list respectively.
Syntax: min(<list>), max(<list>), sum(<list>)
Assignment
 Write a Python program to find the length of the
list.
 Write a python program to add an element at the
end of the list.
 Tuples
Tuples
 Creating Tuples
 Functions in Tuples
 Example program using Tuples
CREATING AND ACCESSING TUPLES
• You can create a tuples by enclosing a comma-separated
sequence of items within round brackets( ).
• Tuples can contain elements of different data types.
Python Tuples are Immutable.

S.NO Tuples Explanation


1 () Tuples with no number ,empty list
2 (1,2,3) Tuples of integers
3 (1,1.2) Tuples of (int and float)
4 (‘a’,’b’) Tuples of characters
5 (a,1,’one’) Tuples of mixed value types
6 (‘one,’two’,’Three Tuples of strings
’)
Difference between List and Tuples
Functions in Tuples

You might also like