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

(iv) List in Python

The document provides a comprehensive overview of lists in Python, covering their definition, creation, and various operations such as concatenation, membership testing, and comparisons. It details functions and methods associated with lists, including len(), append(), and remove(), as well as techniques for list slicing and nested lists. Additionally, it explains how to perform calculations like finding the maximum, minimum, and mean of list elements, along with searching and counting frequencies within lists.

Uploaded by

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

(iv) List in Python

The document provides a comprehensive overview of lists in Python, covering their definition, creation, and various operations such as concatenation, membership testing, and comparisons. It details functions and methods associated with lists, including len(), append(), and remove(), as well as techniques for list slicing and nested lists. Additionally, it explains how to perform calculations like finding the maximum, minimum, and mean of list elements, along with searching and counting frequencies within lists.

Uploaded by

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

Know Python Bytes

www. knowpythonbytes. blogspot. com


Mrs. Payal
Bhattacharjee, PGT( C.
CONTENT
( Learning
S
Outcomes ) 

XI CS 2020-21 LIST in Python


 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
CONTENTS
LIST
Lists Intro: ? • A list is a collections of items which are
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: 10 20 30 40 50 60
List Definition
Indexing in a List -6 -5 -4 -3 -2
Creation of a list BACKWARD INDEX
Traversal of a list -1
Operations on a list
Functions/methods DIRECTION
 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.
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 of mixed data type created without
name
list
Traversal of a List of numbers with name L
list
Operations on a list
Functions/methods
 Lists Slicing;
Empty List created without name
 Nested lists;
finding the maximum,
Empty List created with name L
minimum,
mean
 linear search on list Empty List created with name l using
 counting the funcion list()
frequency
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

Creation of a Nested List with name L


 Lists Slicing;
 Nested lists;
finding the maximum, that is List inside a List
minimum,
mean
 linear search on list
 counting the
frequency
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 **Note: eval( ) identifies the type by looking at the given
 linear search on list expression
 counting the frequency
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 M[1]
 Nested lists;  [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
Lists Introduction: joined with another
List CANNOT be concatenated with a
List
OPERATIONS number

ON List
Concatenation/Joining
Repetition/Replication
Membership List CANNOT be concatenated with a
Comparison of Lists string
Functions/methods
 Lists Slicing;
 Nested lists;
finding the maximum,
minimum, mean List CANNOT be joined with a complex
 linear search on list nos.
 counting the frequency

Concatenation
refers to Joining List CAN be
of two objects concatenated or
joined with another
OPERATIONS on LIST Replication
CONTENTS Replication refers to Repetition of objects
Lists Introduction:
as it is. Here, in LIST we can see NON-
VECTORISED operation with a list where
OPERATIONS the entire list as a whole is replicated.
ON List
Concatenation/Joining
NOTE: Vectorised operation, refers to
the replication of individual elements of
Repetition/ the object.
Replication Replicating the unnamed List two
Membership times
Comparison of Lists
Functions/methods
 Lists Slicing;
 Nested lists;
finding the maximum, Replicating the named List RL four
minimum, times
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
LHS of in operator in the sequence mentioned in the RHS
Replication 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
ON List used
in comparing two
Concatenation/Joining objects. Here, for
Repetition/ example
Relationaltwooperators
List objects
Replication ==,!= arecan
compared.
be used in
Membership in List comparing two
objects eg. List with
Comparison number/string/tuple.
of Lists But, Relational
Functions/methods operators
 Lists Slicing; <,<=,>,>= always
 Nested lists; returns ERROR when
finding the maximum, used in comparing
minimum, mean
two objects eg.
 linear search on list Eg. List CANNOT be
 counting the frequency List with
compared with a
number/string/tu
number
ple.
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() TOTAL 5 elements of the List object
 Lists Slicing;
 Nested lists;
finding the maximum,
Functions / Methods on LIST- list()
The list() function creates a list object.
CONTENTS A list object is a collection which is ordered and
changeable.
Lists Introduction:
list() function
Operations on a list 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; NOTE: Python language is case-sensitive.
finding the maximum, 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,
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, three elements
mean *‘know’,’python’,’bytes’+
 linear search on list
 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
Functions/methods
len(), list(),append(), extend(), value. ListObject.insert(<position>,<item>)
Position denotes the index no.
insert(), count(),index(), Item denotes the item/element/value to be
remove(), pop(), reverse(),
sort(),min(), max(),
inserted.
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,
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,
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()
pop( ) method is used to remove the item from the list.
CONTENTS 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()
The pop( ) method removes an
methods individual item (from the right most
len(), list(),append(), end  by default) or the
extend(), value/element present at the index
insert(),count(),index(), number mentioned in the parameter
and returns it. Whereas remove( )
remove(), pop() , searches the first occurrence of the
item and removes it from the list
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- 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(), Reverses the list ‘in place’ and does not
insert(),count(),index(), return anything.
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- 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.
sort(reverse=True ) for getting the list in decreasing order
Functions/
methods
len(), list(),append(),
extend(),
insert(),count(),index(),
remove(), pop(),
reverse(),
sort(),min(), max(), NOTE: The sort( ) method
sum() , clear() won’t be able to sort the
 Lists Slicing; values of a list if it contains
 Nested lists; complex numbers/tuple
finding the / mixed datatype as its
elements.
maximum,
minimum,
Functions / Methods on LIST- min(),max(),sum()
CONTENTS min( ) method,
Lists Introduction: Returns the minimum out of the elements/values/items
Operations on a list of the given List
Functions/ max( ) method,
methods Returns the maximum out of the elements/values/items
len(), list(),append(), of the given List
extend(),
insert(),count(),index(), sum( ) method,
remove(), pop(),
Returns the sum of all the values/elements/items of
reverse(),
the given List.
min(),
sort(),

max(), sum()
,
clear()
 Lists Slicing;
 Nested lists;
finding the
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,
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

? 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
 Lists Slicing;

 Nested lists; Nested List 2


finding the maximum,

minimum, mean
 linear search on list
counting
Note: the frequency
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+
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
maximu
m OUTPUT
, minimum,
mean
 linear search
on list
 counting the
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
minimu
m, OUTPUT
maximum ,
mean
 linear search
on list
 counting the
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 OUTPUT
on list
 counting the
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