Using = operatorswith LIST
= Creates a new list
>>> marks=[24, 'TARUN', 'COMP SC', 56,
25]
>>> print(marks)
Displays [24, 'TARUN', 'COMP SC’, 56,
25]
2.
Using = operatorswith LIST
= Assigns a value to an element of a list
>>> marks=[24, 'TARUN', 'COMP SC', 56,
25]
>>> marks[2], marks[3]=59, 28
>>> print(marks)
Displays [24, 'TARUN', 'COMP SC', 59, 28]
3.
Using = operatorswith LIST
= Creates a new list which an alias
(reference) of an existing list
>>> stu1= [12, 'RAHUL', 64]
>>> stu2=stu1
List stu2 is created as an alias (a reference)
of stu1. The two lists stu1 and stu2 share
same memory location. Any change in stu1
will update stu2 and vice-versa.
Using + operatorswith LIST
+ Joins to two or more lists
>>> list1=[24, 'TARUN', 56]
>>> list2=[25, 81, 'B2']
>>> list3=list1+list2
>>> print(list3)
Displays [24, 'TARUN', 56, 25, 81, 'B2']
6.
Using += operatorswith LIST
+=Updates an existing list by appending
another list
>>> stu=[24, 'TARUN', 56]
>>> stu+=[25, 81, 'B2']
>>> print(stu)
Displays [24, 'TARUN', 56, 25, 81, 'B2’]
7.
Using * operatorswith LIST
* Replicate a list
>>> list1=[24, 'TARUN', 56]
>>> list2=2*list1
New list list2 is created by duplicating
values stored in list list1.
>>> print(list2)
Displays [24, 'TARUN', 56, 24, 'TARUN',
56]
8.
Using *= operatorswith LIST
*= Updates an existing list by replicating
values stored in the list
>>> stu=[24, 'TARUN', 56]
>>> stu*=3
The list stu is updated triplicating values
stored in the list stu.
>>> print(stu)
Displays [24, 'TARUN', 56, 24, 'TARUN', 56,
24, 'TARUN', 56]
9.
Using in operatorswith LIST
in Checks whether an element is
present in a list
>>> alist=[10, 20, 30, 40, 50]
>>> print(30 in alist)
Displays True
10.
>>> print(60 inalist)
Displays False
>>> stu=[24, 'TARUN', 56]
>>> x, y='TARUN' in stu, 'ENGLISH' in stu
>>> print(x, y)
Displays True False
11.
Using del operatorswith LIST
del Deletes an element or elements from a list
>>> marks=[24, 'TARUN', 'COMP SC', 56, 25]
>>> del marks[2]
>>> print(marks)
Displays [24, 'TARUN', 56, 25]
Deletes the element whose index is 2
12.
>>> alist=[10,20,30,40]
>>> delalist[2], alist[2]
>>> print(alist)
Displays [10, 20]
>>> del alist[5]
Displays run-time error because index 5 is
out of range.
13.
Using del operatorswith LIST
del Deletes a list
>>> marks=[24, 'TARUN', 'COMP SC', 56,
25]
>>> del marks
>>> print(marks)
Displays run-time error because the list
has been deleted from the memory.
14.
Built-in functions forlist data type
print()
print() function displays the entire list on the
screen from left to right.
student=[24, 'TARUN', 'COMP SC', 56, 25]
print(student)
Displays [24, 'TARUN', 'COMP SC', 56, 25]
15.
len()
Function len() returnsnumber of values
present in a list. Function len() works with list
of any type.
a,b=[55,45,25,65] ,[2.5,6.7,9.3,5.7,1.2]
c,d=['DIPTI','AVEEK','SANDIP’],[10, ‘AAA’]
print(len(a),len(b),len(c), len(d) )
Displays 4 5 3 2
16.
max(), min()
Function max()returns the maximum value
(highest value) stored in the list. Function min()
returns the minimum value (lowest value) stored
in the list. Functions max() and min() can be used
with list containing either homogeneous data
type (either only int or only float or only str) or
containing numbers (int type values and float
type values).
17.
Using functions max()and min() with list containing int
(float) type values and str type values will trigger run-time
error.
a,b=[55,45,25,65] ,[2.5,6.7,9.3,5.7,1.2]
c,d=['DIPTI','AVEEK','SANDIP’],[10, ‘AAA’,11.5]
print(max(a),max(b),max(c),sep=‘,’)
Displays 65, 9.3, SANDIP
print(min(a),min(b),min(c) ,sep=‘,’)
Displays 25, 1.2, AVEEK
print(max(d)) #OR, print(min(d))
Displays run-time error since type str and type int(float)
cannot be compared.
18.
sum()
Function sum() returnsthe sum of the elements
stored in the list, provided list contains only
numbers (all int type or all float type or
collection of int type and float type). If a list
contains str type value, function sum() triggers a
run-time error.
a,b=[55,5,25,65,15],[2.5,6.7,9.3,5.7,1.2]
print(sum(a))
Displays 205 (55+5+25+65+15)
print(sum(b))
19.
>>> print(sum(d))
Displays 76.5()
>>> print(sum(c))
Displays run-time error since string type cannot
be added to obtain a sum.
>>> print(sum(e))
Displays run-time error since integer type and
string type cannot be added to obtain a sum.
20.
sorted()
Function sorted() returnsa list sorted in ascending order
by default but it does not sort the original list. Function
sorted() can be used with list containing either
homogeneous data type (either only int or only float or
only str) or containing int type values and float type
values.
a=[55,45,25,65,15]
b=[2.5,6.7,9.3,5.7,1.2]
c=['DIPTI','AVEEK','SANDIP','NEETA','BHUVAN']