Revision 3 - Python Class 9
Revision 3 - Python Class 9
output
21
output
P
# Write a program to create a list to print second last element of list
list1=[21,30,45,60]
print(list1[-2])
output
45
output
P
Y
T
H
O
N
output
10
20
30
40
50
append(): It appends a single element with the given value at the end of
the list. We can use this function in a loop for adding multiple values.
With single append the length of the list increases by 1.
# Write a program to create a list to add the new element at the end of
the list
list1=[10,20,30,40,50]
list1.append(78)
print(list1)
output
10,20,30,40,50,78
list1=["Red","Yellow"]
list1.append("Green")
print(list1)
output
['Red', 'Yellow', 'Green']
list1=['R','E']
list1.append('D')
print(list1)
output
['R', 'E', 'D']
Output
[10, 20, 30, 40, 50, 78, 90, 100]
Output
[10, 78, 20, 30, 40, 50]
Output
['abc', 10, 20, 30, 40, 50]
# Write a program to create a list to insert elements using variable “a”
as xyz at third position in the list
a="xyz"
list1=[10,20,30,40,50]
list1.insert(3,a)
print(list1)
Output
[10, 20, 30, 'xyz', 40, 50]
# Write a program to modify the third element of list with value 150
list1=[10,20,30,40,50]
list1[3]=150
print(list1)
Output
[10, 20, 30, 150, 50]
Only one value can be removed at a time even if there are duplicate
values in the list and to remove the multiple values it is used in
loop
If we try to remove the element which does not exist in the then it
gives an IndexError
Output
[10, 20, 40, 50]
pop(): It removes the element from the list based on the index number
specified in the function and returns the deleted value.
Output
[10, 20, 40, 50]
clear(): This function removes all the elements of the list in one go.
It empties the list but the empty list still exists which can be used
later to fill the values.
Output
[]
Output
length of list 5
Output
[50, 40, 30, 20, 10]
Output
[50, 40, 20, 10, 5, 3]
index(): It returns the index number of the value given in the function
If the value not found in the list, then it returns ValueError
exception
Output
3
Output
4
+ operator with list is used to concatenate the list with another list.
Both the operands on either side of the + operator have to be a list
only otherwise it gives an error.
list1=[1,2,3]
list2=[4,5,6]
list3=list1+list2
print(list3)
Output
[1, 2, 3, 4, 5, 6]
* operator with list is used to replicate a list specific number of
times. With * operator one operand has to be a list and other should be
an integer, otherwise it will an error.
list1=[1,2,3]
list2=list1*3
print(list2)
Output
[1, 2, 3, 1, 2, 3, 1, 2, 3]
list2=list1*4
print(list2)
Output
['Amit', 'Amit', 'Amit', 'Amit']
list1=[1,2,3]
list2=[1,2,3]
print(list1==list2)
Output
True
list1=[1,2,30]
list2=[1,2,3]
print(list1<list2)
Output
False
1 – Bracket ()
2 – Exponential **
3 – Multiplication *, Division /, Modulus %, Floor Division //
4 – Addition +, Subtraction –
Operators on the same level number will be calculated from left to right
Example 1
a=10
b=20
c=a+b-2
print(c)
Result – 28
Explanation
c=10+20-2
c=30-2
c=28
Example 2
a=10
b=20
c=a*b/2
print(c)
Result – 100.0
Explanation
c=10*20/2
c=200/2
c=100.0
Example 3
a=10
b=20
c=a*b//2+5-3
print(c)
Result – 102
Explanation
c=10*20//2+5-3
c=200//2+5-3
c=100+5-3
c=105-3
c=102
Example 4
a=10
b=20
c=(a*b) +5
print(c)
Result – 205
Explanation
c=(10*20)+5
c=200+5
c=205