0% found this document useful (0 votes)
10 views

FINAL Revised LIST in Python

Uploaded by

harshvardhan3139
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

FINAL Revised LIST in Python

Uploaded by

harshvardhan3139
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Know Python Bytes

w w w. k n o w p y t h o n b y te s . b l o g s p o t . c o m
Mrs. Payal Bhattacharjee, PGT(C.Sc.)
K V No.1 Kanchrapara
KVS-RO(Kolkata)
CONTENTS
( Learning Outcomes ) 
LIST in Python
XI CS 2020-21
 Lists Introduction: Definition, Creation of a list, Traversal of a list.
 Operations on a list – concatenation/joining, repetition/replication,
membership, Comparison of Lists;
 Functions/methods–len(), list(),append(), extend(), insert(), count(),
index(), remove(), pop(), reverse(), sort(),min(), max(), sum()
 Lists Slicing;
 Nested lists;
 finding the maximum, minimum, mean of numeric values stored in a list
 linear search on list of numbers
 counting the frequency of elements in a list

XI IP 2020-21
Lists: list operations - creating, initializing, traversing and manipulating
What is a LIST ?
CONTENTS
• A list is a collections of items which are
Lists Intro: formed by placing a comma-separated-
List Definition list of expressions in square brackets.
Indexing in a List
• Each item/element has its own index
Creation of a list
Traversal of a list. number.
Operations on a list - • Index of first item is 0 and the last item
Functions/methods
 Lists Slicing;
is n-1. Here n is number of items in a
 Nested lists; list.
finding the maximum, • Lists are mutable sequences i.e. we can
minimum, mean
 linear search on list change elements of a list in place.
 counting the frequency • Lists can contain values of mixed data
types.
INDEXING in a LIST
CONTENTS FORWARD INDEX DIRECTION
0 1 2 3 4 5
Lists Intro:
List Definition 10 20 30 40 50 60
Indexing in a List -6 -5 -4 -3 -2 -1
Creation of a list BACKWARD INDEX DIRECTION
Traversal of a list
Operations on a list
Functions/methods
 Lists Slicing; NOTE:-
 Nested lists; The index (also called subscript) is the numbered
finding the maximum, position of a letter in the LIST. In python, indices begin
minimum, mean
from
 linear search on list
 counting the frequency 0,1,2,…. upto (length-1) in the forward
direction and
-1,-2,-3,….. (-length) in the backward direction.
where length is the length of the List.
CREATION of LIST
CONTENTS
Lists Intro:
List of numbers created without name

List Definition List of characters created without name


Indexing in a List
Creation of a list List of mixed data type created without
name
Traversal of a list
Operations on a list List of numbers with name L
Functions/methods
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean Empty List created without name
 linear search on list
 counting the frequency Empty List created with name L

Empty List created with name l using


funcion list()
Creation of LIST contd.. (Long List and Nested List)
CONTENTS
Lists Intro: Creation of a long list with name MultiTens
List Definition
Indexing in a List
Creation of a list
Traversal of a list
Operations on a list
Functions/methods
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean Creation of a Nested List with name L
 linear search on list
 counting the frequency that is List inside a List
Creation of LIST from existing sequences
CONTENTS Creating list from string Creating list from tuple

Lists Intro:
List Definition
Indexing in a List
Creation of a list Creating list by taking input from the keyboard(user)
from string,tuple,
list(input()),
eval(input())
Traversal of a list **Note: All the elements are considered as string
Assign/change values in List
Operations on a list Creating list by taking input from the keyboard [using eval()]
Functions/methods ]method]
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list **Note: eval( ) identifies the type by looking at the given
 counting the frequency expression
TRAVERSAL of a LIST
CONTENTS
Lists Intro:
List Definition
Indexing in a List
Creation of a list
Traversal of a list
Assign /change values in List
Operations on a list
Functions/methods
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
TRAVERSAL of NESTED LIST
CONTENTS
Enclosing List
Lists Intro:
List Definition
Indexing in a List Nested List 1 Nested List 2
Creation of a list
Traversal of a list
Assign /change values in List

Operations on a list Note: Denoting Nested elements


Functions/methods using Index nos.
 Lists Slicing; M[0]  10
 Nested lists; M[1]  [15,30]
finding the maximum, M[1][0]15
minimum, mean M[1][1]30
 linear search on list M[2] *80,’Mask’,60+
 counting the frequency M[2][0]80
M[2][1]‘Mask’
M[2][2]60
Assign/Change values in a LIST
Forward Index 0 1 2 3
CONTENTS -4 -3 -2 -1 Backward index

Lists Intro:
List Definition Assigning
/ Changing
Indexing in a List values of
Creation of a list elements
Traversal of a list of a List

Assign /change
values in List
Operations on a list
Functions/methods
 Lists Slicing; Assigning /
 Nested lists; Changing
finding the maximum, values of
elements of
minimum, mean a List
 linear search on list
 counting the frequency
OPERATIONS on LIST Concatenation
CONTENTS List CAN be concatenated or
joined with another List
Lists Introduction:
List CANNOT be concatenated with a number
OPERATIONS
ON List
Concatenation/Joining
Repetition/Replication
Membership List CANNOT be concatenated with a string
Comparison of Lists
Functions/methods
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean List CANNOT be joined with a complex nos.
 linear search on list
 counting the frequency

Concatenation refers
to Joining of two List CAN be concatenated or
objects joined with another List
OPERATIONS on LIST Replication
CONTENTS Replication refers to Repetition of objects as it
Lists Introduction:
is. Here, in LIST we can see NON-VECTORISED
operation with a list where the entire list as a
OPERATIONS whole is replicated.
ON List
Concatenation/Joining
NOTE: Vectorised operation, refers to the
replication of individual elements of the object.
Repetition/
Replication Replicating the unnamed List two times
Membership
Comparison of Lists
Functions/methods
 Lists Slicing;
 Nested lists;
finding the maximum, Replicating the named List RL four times
minimum, mean
 linear search on list
 counting the frequency
OPERATIONS on LIST Membership
Membership operator tests for Membership in a sequence.
CONTENTS Returns Boolean True or False
Lists Introduction: OPERATOR DESCRIPTION
OPERATIONS
in Results to True value if it finds a variable at the LHS of in
ON List operator in the sequence mentioned in the RHS of the in
operator, else it returns False
Concatenation/Joining
Repetition/ not in Results to True value if it does not find a variable at the
Replication LHS of in operator in the sequence mentioned in the RHS
of the in operator, else it returns False
Membership
in List
Comparison of Lists
Functions/methods
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
OPERATIONS on Comparison of LIST
CONTENTS Comparison of list returns Boolean True or False.

Lists Introduction: Relational operators


OPERATIONS ==,!=,<,<=,>,>= are used
in comparing two objects.
ON List Here, for example two List
Concatenation/Joining objects are compared.
Repetition/ Relational operators ==,!=
Replication can be used in comparing
Membership in List two objects eg. List with
number/string/tuple. But,
Comparison Relational operators
of Lists <,<=,>,>= always returns
Functions/methods ERROR when used in
 Lists Slicing; comparing two objects eg.
 Nested lists; List with
finding the maximum, number/string/tuple.
minimum, mean
 linear search on list Eg. List CANNOT be
 counting the frequency compared with a number
Functions / Methods on LIST-len()
CONTENTS len() function counts the no. of elements in the List
Lists Introduction:
Operations on a list
Functions/
methods
len(), list(),append(),
extend(), insert(),
count(),index(), remove(),
pop(), reverse(), sort(),
min(), max(), sum(),clear()
 Lists Slicing;
 Nested lists;
finding the maximum, TOTAL 5 elements of the List object
minimum, mean
 linear search on list
 counting the frequency
Functions / Methods on LIST-list()
CONTENTS The list() function creates a list object.
A list object is a collection which is ordered and
Lists Introduction:
changeable.
Operations on a list list() function
takes
Functions/ elements/values
inside the
methods parantheses i.e. ()

len(), list(),append(),
extend(), insert(),
count(),index(), remove(),
pop(), reverse(), sort(),
min(), max(), sum(),clear()
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list NOTE: Python language is case-sensitive.
 counting the frequency Here, Z is the List object , so, z in small letters gives an error.
Functions / Methods on LIST-append()
CONTENTS The append() function takes one single parameter
and adds exactly one element at the end of the List.
Lists Introduction: It takes exactly one element and returns no value.
Operations on a list
Functions/
methods Here, append() function takes one single
parameter . In this example, 30.
len(), list(),append(),
extend(), insert(),
count(),index(), remove(),
pop(), reverse(),
sort(),min(), max(),
sum(),clear()
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
Functions / Methods on LIST-extend()
CONTENTS The extend() function adds iterable (Eg. Another List/
Lists Introduction:
Tuple/String) at the end of the List.
Operations on a list
Functions/methods Here, trying to extend list A , with another
len(), list(),append(), unnamed List containing one element [30].
extend(), insert(), Here, trying to extend list A , with
count(),index(), remove(),
another unnamed List containing
pop(), reverse(), sort(),
three elements [4,50,77].
min(), max(), sum(),clear()
 Lists Slicing; Here, trying to extend list A , with
 Nested lists; another named List B containing
finding the max,min, mean three elements
 linear search on list *‘know’,’python’,’bytes’+
 counting the frequency

Here, trying to extend list A , with a number 10. Hence error !!!

NOTE: A no. inside parentheses () is not a tuple,


until it ends with a comma ,

Here, trying to extend list A , with an unnamed tuple with two values

Here, trying to extend list A , with a string.


Every character is considered as an element of the list.
Functions / Methods on LIST- insert()
CONTENTS insert( ) method,
Lists Introduction:
inserts an item / element at a given position.
Operations on a list
It takes two arguments and returns no value.
Functions/methods
len(), list(),append(), extend(),
ListObject.insert(<position>,<item>)
Position denotes the index no.
insert(), count(),index(), Item denotes the item/element/value to be inserted.
remove(), pop(), reverse(),
sort(),min(), max(),
sum(),clear() Forward Index 0 1 2
 Lists Slicing; -3 -2 -1 Backward index
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency

NOTE: Whenever the element is inserted,


whether forward or backward index… it is
always inserted at the left hand side of the
current index item’s position.
Prepended inserted at the beginning i.e.
index 0
Appended  inserted at the last
Functions / Methods on LIST- count()
CONTENTS count( ) method,
Lists Introduction: returns the count of the item (no. of times the
Operations on a list item /element/value appears) that is passed as
Functions/ argument. If the given item is not in the list, it
will return 0
methods
len(), list(),append(),
extend(), insert(),
count()
,index(),remove(), pop(),
reverse(), sort(),min(),
max(), sum(), clear()
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
Functions / Methods on LIST- index()
CONTENTS index( ) method,
returns the index of the first matched item.
Lists Introduction:
Operations on a list Forward
Index 0 1 2 3 4 5
Functions/
methods
len(), list(),append(), extend(),
insert(),count(), index(),
remove(), pop(), reverse(),
sort(),min(), max(), sum() ,
clear()
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
Functions / Methods on LIST- remove()
remove( ) method,
CONTENTS removes the first occurrence of given item from the
list (from left-to-right)
Lists Introduction: remove( ) will report an error if there is no such value.
Operations on a list
Functions/
methods
len(), list(),append(), extend(),
insert(),count(),index(),

remove(), pop(),
reverse(), sort(),min(), max(),
sum() , clear()
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
Functions / Methods on LIST- pop()
CONTENTS pop( ) method is used to remove the item from the list.
It takes one optional argument . The element being deleted
is returned by the method
Lists Introduction:
Operations on a list
DIFFERENCE between pop() and
Functions/ remove()
methods The pop( ) method removes an
len(), list(),append(), individual item (from the right most
extend(), end  by default) or the
insert(),count(),index(), value/element present at the index
number mentioned in the parameter
remove(), pop() , and returns it. Whereas remove( )
reverse(), sort(),min(), searches the first occurrence of the
max(), sum() , clear() item and removes it from the list

 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
Functions / Methods on LIST- reverse()
CONTENTS
Lists Introduction:
Operations on a list reverse( ) method,
Functions/ reverses the items of the list.
This doesn’t create a new list.
methods It takes no argument and return no list.
len(), list(),append(), extend(),
insert(),count(),index(), Reverses the list ‘in place’ and does not
remove(), pop(), return anything.
reverse(), sort(),min(),
max(), sum(),clear()
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
Functions / Methods on LIST- sort()
CONTENTS sort( ) method,
Lists Introduction: sort( ) method and sort(reverse=True ) sorts or
Operations on a list orders the items of the list, by default in increasing order.

Functions/ sort(reverse=True ) for getting the list in decreasing order

methods
len(), list(),append(),
extend(),
insert(),count(),index(),
remove(), pop(), reverse(),
sort(),min(), max(),
sum() , clear() NOTE: The sort( ) method
 Lists Slicing; won’t be able to sort the
 Nested lists; values of a list if it contains
finding the maximum, complex numbers/tuple
/ mixed datatype as its
minimum, mean elements.
 linear search on list
 counting the frequency
Functions / Methods on LIST- min(),max(),sum()
CONTENTS min( ) method,
Lists Introduction: Returns the minimum out of the elements/values/items of
Operations on a list the given List
Functions/
max( ) method,
methods Returns the maximum out of the elements/values/items of
len(), list(),append(), the given List
extend(),
insert(),count(),index(), sum( ) method,
remove(), pop(), reverse(),
Returns the sum of all the values/elements/items of the
min(),
sort(), given List.
max(), sum() ,
clear()
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
Functions / Methods on LIST- min(),max(),sum() contd..
List of string . Maximum
and Minimum is done on
the basis of the ASCII
values of the characters
from the beginning.
Eg. ‘A’=65, ‘a’ = 97, ‘b’=98
and so on

A List with
mixed data
type cannot
return values
for the min,
max and
sum
functions.
Functions / Methods on LIST- clear()
CONTENTS
Lists Introduction:
Operations on a list clear( ) method,
Functions/ removes all the items from the list and
methods the list becomes empty after executing
len(), list(),append(), this function.
extend(),
insert(),count(),index(),
remove(), pop(), reverse(),
sort(),min(), max(), sum() ,
clear()
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency
LIST slicing
L[start:stop] creates a list slice out of list L with elements falling between
indexes start and stop, not including value at the stop index.

L[start:stop:step] creates a list slice out of list L with elements falling


between indexes start and stop, not including value at the stop index, and skipping
step elements in between.
NOTE: In absence of any start, stop, step values, we have Default start index as the beginning,
stop index as the last index and step value as +1.

FORWARD INDEX DIRECTION


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
K N O W P Y T H O N B Y T E S
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
BACKWARD INDEX DIRECTION
LIST slicing
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
K N O W P Y T H O N B Y T E S
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Start index 3, stop index is (10-1=9)

Start index 5, stop index is 50 which is beyond the last index in forward
direction. Hence, it will stop at the last index

Start index -4(backward), stop index is (6-1=5 (forward)). Here, it is


not getting any value in the range, hence empty.
Start index -4(which is ‘Y’ as per the backward index,) stop index is 25
which is beyond the last index. Hence, it will stop at the last index.

Start index 10, stop index is 30 which is beyond the


last index in forward direction. Hence, it will stop at
the last index

Start index -10(which is ‘Y’ as per the backward index,) stop index is 12-1=11
(forward indexing). Hence, it will stop at the (stop index-1)., ‘Y’ is at 11 index no.
LIST SLICING contd.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
K N O W P Y T H O N B Y T E S
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Start index -3(backward), stop index is -12(backward). Here, it is not getting
any value in the range as no step value mentioned in reverse. Hence empty.

Start index -12(which is ‘W’ as per the backward index,)


stop index is -3 (which takes values till index -3-1= -4)

Start index -3(backward), stop index is -12(backward). Here, it is getting


value in the range as step value is -1 mentioned in reverse.

Start index -20(which is beyond backward index,) stop


index is -5 which takes value till (-5-1=-6). Here, -6 is ‘N’.

Start index 1, stop index is 13 (till 13-1=12),step value


is 2 in forward direction. Hence, it will fetch/take
alternate elements with a skip of 1 value in between

TRY TO UNDERSTAND YOURSELF. A task for you.

? Also write the slice for List to get the output as

Default start and stop is beginning and end index of the list. Here, step is 3.
NESTED LISTS
CONTENTS Enclosing List
Lists Intro
Operations on a list
Functions/methods
Nested List 1 Nested List 2
 Lists Slicing;

 Nested lists;
finding the maximum,
minimum, mean
 linear search on list
 counting the frequency

Note: Denoting Nested elements


using Index nos.
M[0]  10
M[1]  [15,30]
M[1][0]15
M[1][1]30
M[2] *80,’Mask’,60+
M[2][0]80
M[2][1]‘Mask’
M[2][2]60
Program to find the MAXIMUM element from a list of element along
with its index in the list without using max() library function

CONTENTS
Lists Intro
Operations on
a list
Functions/
methods
 Lists Slicing;
 Nested lists;
finding
the
maximum
, minimum, mean
 linear search
on list OUTPUT
 counting the
frequency
Program to find the MINIMUM element from a list of element along
with its index in the list without using min() library function

CONTENTS
Lists Intro
Operations on a
list
Functions/
methods
 Lists Slicing;
 Nested lists;
finding
the
minimum,
maximum , mean
 linear search
on list OUTPUT
 counting the
frequency
Program to find the MEAN (AVERAGE) of all the elements of
the list
CONTENTS
Lists Intro
Operations on a
list
Functions/
methods
 Lists Slicing;
 Nested lists;
finding
the mean,
maximum,
minimum
 linear search
on list
 counting the OUTPUT
frequency
Program to search an element in the List (LINEAR SEARCH)
CONTENTS
Lists Intro
Operations on a
list
Functions/
methods
 Lists Slicing;
 Nested lists;
finding the
mean, maximum,
minimum
 linear
search on
list
 counting the
frequency OUTPUT
Program to count frequency (/no. of times of occurrence) of an element in the List
(FREQUENCY COUNT)
CONTENTS
Lists Intro
Operations on a
list
Functions/
methods
 Lists Slicing;
 Nested lists;
finding the
mean, maximum,
minimum
 linear search
on list

counting
the OUTPUT
frequency
Bibliograhy and References
• Google
• Wikipedia
• XI Computer Science , By Sumita Arora,
Dhanpat Rai Publication 2020 Edition
• XI Informatics Practices , By Sumita Arora,
Dhanpat Rai Publication 2020 Edition
Programming is an ART….
Add your colours to it and make your own

You might also like