Brief Explanation On List and Dictionaries of Python

nikhita4775 27 views 17 slides Oct 10, 2024
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

This is a brief explanatory ppt on the LISTS AND DICTIONARIES in PYTHON language


Slide Content

Work is Worship 1 Exploring Python Lists Presented by : Divyanshu Mohanty & Nikhita Majhi(XI-C) LR DAV PUBLIC SCHOOL, CUTTACK

Work is Worship 2 LEARNING OBJECTIVES : Participants will be able to: remember the need for a List and create different types of Lists understand List mutability analyse Indexing & Slicing on lists evaluate List Operations(Join, Replication ) apply List Functions

Work is Worship 3 Need For a LIST : A variable can store one value in it. W hen we put a new value in it - the old value is over written. But a List can store more than one values of any type . For Example : Student’s information which include roll, name, mark etc. all together as a single unit can be stored in a list. Example(Variable): >>> mark = 55 >>> mark = 95 >>> print(mark) 95 Example(List): s_info = [ 1, ’ Ranjan ’, 99] marks = [ 90, 80, 75, 60, 59 ] Video

Work is Worship 4 Creating A List : To create a List, we have to keep all expressions in square brackets. Square brackets indicate the start and end of the list. Comma is used to separate the items in a list. A list element can be any Python object- even another list . A list can be empty .

Work is Worship 5 Different Types of List: 1. Empty List An Empty list is a list, which does not contain any value . An Empty list can be created using the following syntax: eli = [ ] OR eli = list( ) 2. Long List A Long list is a list which contains many elements. We can split it across several lines. Example of long list : longli = [ 1,2,3,4,5,6,7,8,9, 10,11,12,13,14,15 ] 3. Nested List When a list contains another list as its member . It is known as nested list. Example of nested list : nesli = [ 1, 2, 3, [ 4, 5, 6] , 7 ,8 ,9 ]

Work is Worship 6 4. Creating List from Existing Sequence We can create a list from an existing sequence as per the syntax given below. li = list(sequence) Here the sequence may be a string/tuple. Example: >>> st = 'Python' >>> li = list( st ) >>> li ['P', 'y', 't', 'h', 'o', 'n'] >>> tu=(10,20,30,40,50) >>> li=list(tu) >>> li [10, 20, 30, 40, 50] Can we create a list from a dictionary ? >>> d={1:'one', 2:'two'} >>> d {1: 'one', 2: 'two'} >>> li=list(d) >>> li [1, 2] >>> d1={'one':1,'two':2} >>> li=list(d1) >>> li ['one', 'two']

5. Creating List from Keyboard Input We can use the following methods to create a list via keyboard input. Using list( ) li=list(input('Enter elements')) Enter elements1,'Pyhon',3.9 >>> li ['1', ',', "'", 'P', 'y', 'h', 'o', 'n', "'", ',', '3', '.', '9'] all elements of the list will be of string Here all elements of the list will be of string type. Using eval ( ) >>> li= eval (input('Enter elements')) Enter elements[1,'Pyhon',3.9] >>> li [1, ' Pyhon ', 3.9] t Here eval ( ) tries to identify the type of data. Work is Worship 7

Work is Worship 8 Lists are Mutable Lists are " mutable " - we can change an element of a list using the index operator. >>> li=[10, 2 , 30, 9] >>> li[1] = 7 >>> li [10, 7 , 30, 9] >>> name=[ ' Raj' ,'Kamal ', ' Lalit '] >>> name[0]=' Nupur ' >>> name [ ' Nupur ' , 'Kamal', ' Lalit '] Find the output? >>> li=[12,34] >>> li[2]=31 Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> li[2]=31 IndexError : list assignment index out of range

List are sequences. List elements are indexed as forward indexing and backward indexing.. We can get any single element from a list using its index specified in square brackets. Work is Worship 9 Indexing Lists(Looking Inside a List) >>> lis =[10,50,90.5,'Kajol'] >>> lis [1] 50 >>> lis [-1] ' Kajol ' >>> lis [2] 90.5 10 1 50 2 90.5 -4 -3 -2 3 Kajol -1

Work is Worship 10 Lists can be sliced using(:) Slices are the sub part of a list extracted out. Indexes of a list elements are used to create list slices as per the following format. sli = li[ start:stop:step ] ( Remember : Here start, stop and step are optional and the stop is "up to but not including“) lis =[11,22,33,44,55,66] lis [1:6:2] [22, 44, 66] >>> lis [:4] [11, 22, 33, 44] >>> lis [3:] [44, 55, 66] >>> lis [:] [11, 22, 33, 44, 55, 66] Find the output? >>> lis [-1:] [66] >>> lis [::-1] [66, 55, 44, 33, 22, 11] >>> lis [2:6:-1] [ ] >>> lis [-2:-5] [ ] >>> lis [-2:-5:-1] [55, 44, 33]

Work is Worship 11 List Operations(Join(+), Replication(*)) We can create a new list by adding two existing lists together using + operator. We can replicate a list specified number of times by using * operator. >>> li1=[1,2,3,4,5] >>> li2=[10,20,25] >>> li1+li2 [1, 2, 3, 4, 5, 10, 20, 25] >>> li2*3 [10, 20, 25, 10, 20, 25, 10, 20, 25] Find the output ? >>li1+5 >>li1*li2 Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> li1+5 TypeError : can only concatenate list (not " int ") to list Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> li1*li2 TypeError : can't multiply sequence by non- int of type 'list'

Work is Worship 12 Membership Operators(in & not in) : Python provides two operators that check if an item is in/not in list. These are logical operators that return True or False. They do not modify the list. >>> no=[12,34,56,76,89,90] >>> 33 in no False >>> 56 not in no False >>> [12,34] in no False >>> 89 in no True Find the output ? >>> 76,89 in no (76, True) >>10,20,30 in no (10, 20, False)

Work is Worship 13 Comparison in List Sequences: We can compare two lists using standard comparison operators of Python i.e. >, <, >=, <=, ==, != etc. Python internally compares individual elements of lists. >>> [ 1 , 2, 8, 9] < [ 9 , 1] True >>> [1, 2, 8, 9 ] < [1, 2, 8, 1 ] False >>> [1, 2, 8, 9 ] > [1, 2, 8, 4 , 5] True Find the output? >>> [1, 2, 8, 9] > [1, 2, 8] True >>> [' anis ']>[' ajit ','raj'] True >>> li1=[1,2,3,4] >>> li2=[1,2] >>> li1==li2 False

Python offers many built-in functions for list manipulation. Work is Worship 14 List Functions: The index( ) function returns the index of first matched item from the list. Syntax: List.index (<item>) The min( ) function returns the minimum value from the list. Syntax: min(List) The max( ) function returns the maximum value from the list. Syntax: max(List) The sum( ) function returns the sum of all elements present in the list. Syntax: sum(List) The len ( ) function returns the total no of elements present in the list. Syntax: len (List) The count( ) function returns the count of the item passed as argument. Syntax: List.count (<item>) The reverse( ) function reverses the items of the list. Syntax: List.reverse ( ) The sort( ) function arranges the items of the list. Syntax: List.sort ( ) List.sort(reverse=True)

Work is Worship 15 The append( ) method adds an item to the end of the list. Syntax: List.append (<item>) The extend( ) method is used to add multiple elements to the end of the list. Syntax: List.extend (<list>) The insert( ) method is used to insert an element at any position of the list. Syntax: List.insert (< pos >, <item>) The copy( ) method returns a new list. Syntax: newlist =copy(List ) The pop( ) method is used to remove an element from the given position in the list. Syntax: List.pop (<index>) The remove( ) method is used to remove the first occurrence of given item from the list. Syntax: List.remove (<value>) The del( ) method is used to remove an element from the given position in the list. Syntax: del List[position] The clear( ) method removes all the elements from the list and the list becomes empty. Syntax: List.clear ( ) List Functions:

Work is Worship 16

Work is Worship 17