LIST OPERATION, INDEXING,SLICING, REPETITION, CONCATENATION,MEMBERSHIP TESTING(IN AND NOT IN OPERATOR), ARITHMETIC OPERATORS USE IN LIST
TO DOWNLOAD YOUTUBE VIDEO:
https://youtu.be/EJKEa5arphM
TO DOWNLOAD PDF:
https://computerassignmentsforu.blogspot.com/p/listpart2.html
2. Accessing List Elements
A list is a collection of items which are stored according to its index. Its work like String.
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes.
List-name [start: end] will give you elements between indices start to end-1.
The first item in the list has the index zero (0).
3. Accessing List Elements
Example:
Let’s store no’s in a list
no=[10,20,30,40,50,60,70,80]
0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
-8 -7 -6 -5 -4 -3 -2 -1
Positive index
Negative index
4. Accessing List Elements
Positive index
Negative index
Example:
Let’s store Month’s in a list
month=[‘JAN’,’FEB’,’MAR’,’APR’,’MAY’,’JUN’,’JUL’,’AUG’,’SEP’,’OCT’,’NOV’,’DEC’]
0 1 2 3 4 5 6 7 8 9 10 11
JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
5. Now To access these list let us discuss
operations of list
List operations
Indexing Slicing Repetition Concatenation Membership Testing
These operators we discuss in the String video also
6. Indexing
Indexing specify the position of elements in a list or sequence and help us
to access the value from the sequence separately.
For Example:
if we want to access the number 50 from a list given below using positive
index number.
0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
-8 -7 -6 -5 -4 -3 -2 -1
Positive index
Negative index
7. Indexing
0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
-8 -7 -6 -5 -4 -3 -2 -1
Positive index
Negative index
no=[10,20,30,40,50,60,70,80]
print(no[4])
--------output--------
50
if we want to access the number 30 from a list given below
using negative index number.
no=[10,20,30,40,50,60,70,80]
print(no[-6])
--------output-------------
30
8. Slicing
Slicing and indexing are two inter-related operations in lists. Slicing is an
operation in which you can slice a particular range from a sequence.
Syntax:
listname [start : stop : step]
Where, start is the starting point
Stop is the stopping point
Step is the step size—also known as stride, and is optional. Its default value is 1
9. Slicing
Now let Us take one Example:
mylist=[100,”Suraj”,50.45,”pratap”,450,890.25]
0 1 2 3 4 5
100 Suraj 50.45 pratap 450 890.25
-6 -5 -4 -3 -2 -1
print ( mylist [ : ] ) 100,”Suraj”,50.45,”pratap”,450,890.25
All the values
print ( mylist [ 1 : 4 ] ) ['Suraj', 50.45, 'pratap']
Items from 1 to 3
10. Slicing
Now let Us take one Example:
mylist=[100,”Suraj”,50.45,”pratap”,450,890.25]
0 1 2 3 4 5
100 Suraj 50.45 pratap 450 890.25
-6 -5 -4 -3 -2 -1
print(mylist[1:6:2]) ['Suraj', 'pratap', 890.25]
Items 1,3,5
print(mylist[-3:]) ['pratap', 450, 890.25]
Last 3 items
Steps: By default 1 step jump , bur if we make that 2 it means jump of two
11. Concatenation
It is a process in which lists can be combine together with the help of ‘+’
operator.
Example: list1=[10,20,30]
list2=[1,2,3]
In this list1 we add list2 and original list1 overwrite
list1=list1+list2
------------output--------------
[10, 20, 30, 1, 2, 3]
12. Repetition
Multiply ( * asterisk) operator replicates the list for specified number of
times.
Example: list1=[10,20,30]
print(list1*2)
------------output--------------
[10, 20, 30, 10, 20, 30]
13. It check whether a particular element or item is a member of that sequence or
list or not.
There are two operator:
1. in operator 2. not in operator
Membership Testing:
in operator:
It returns true if element appears
in the list, otherwise returns false.
Example:
list1=[10,20,30,40]
print(10 in list1)
----------output-----------
True
14. Note: it will give True if value not
exists inside the list
not in operator: It returns true if element not appears in the list, otherwise
returns false.
Example:
list1=[10,20,30,40]
print(3 not in list1)
----------output-----------
True
15. How to use arithmetic operators in list
Plus: (+) minus: (-) multiply: (*)
divide: (/)
16. 0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
Now if we need to modify the value of 40 at index no. 3 by adding 15 to it.
no=[10,20,30,40,50,60,70,80]
no=no+15no[3]=15
print(no)
[10,20,30,15,50,60,70,80]
17. 0 1 2 3 4 5 6 7
10 20 30 40 50 60 70 80
Now if we need to modify the value of 40 at index no. 3 by adding 15 to it.
no=[10,20,30,40,50,60,70,80]
no[3]=no[3]+15
print(no)
[10,20,30,55,50,60,70,80]