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

Revision 3 - Python Class 9

Uploaded by

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

Revision 3 - Python Class 9

Uploaded by

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

Revision 3

Artificial Intelligence (Subject Code: 417)


Python Programs – Class IX

 List is a collection of heterogeneous data arranged in a sequence.


The values can be of any data type like string, integer, float,
object or even a list.
 Each value is called an element and its position is referred by an
index number.
 All the elements in a list are separated by comma and enclosed in
square brackets.

# Write a program to create an empty list


list1=[]
print(list1)

# Write a program to create a list with one element


list1=[56]
print(list1)

# Write a program to create a list of marks


list1=[56,67,78,90]
print(list1)

# Write a program to create a list of friends


list1=["anita","arshia","rohit"]
print(list1)

# Write a program to create a list of students with roll no and


percentage
list1=[21,"anita",56.5,22,"arshia",78]
print(list1)

# Write a program to create a list to print element at zero position


list1=[21,30,45,60]
print(list1[0])

output
21

# Write a program to create a list to print element at zero position


list1=['P','Y','T','H','O','N']
print(list1[0])

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

Traversing: Visiting each element and processing it as required by a


program. We use loop to traverse a list.

# Write a program to create a list to traverse the element of the list


list1=['P','Y','T','H','O','N']
for i in list1:
print(i)

output
P
Y
T
H
O
N

# Write a program to create a list to traverse the element of the list


list1=[10,20,30,40,50]
for i in list1:
print(i)

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']

extend(): It append multiple values at a time in a list.

# Write a program to create a list to add the elements (78,90,100) at


the end of the list
list1=[10,20,30,40,50]
list2=[78,90,100]
list1.extend(list2)
print(list1)

Output
[10, 20, 30, 40, 50, 78, 90, 100]

insert(): It is used to add a single value at a specific position in an


existing list.

# Write a program to create a list to insert elements (78) at first


position in the list
list1=[10,20,30,40,50]
list1.insert(1,78)
print(list1)

Output
[10, 78, 20, 30, 40, 50]

# Write a program to create a list to insert elements (“abc”) at zero


position in the list
list1=[10,20,30,40,50]
list1.insert(0,"abc")
print(list1)

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]

Modifying Existing values: List is mutable so data can be easily


modified by overwriting a new value to an existing value in a given list
by using an assignment operator (=).

# 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]

remove(): It removes the first occurrence of the element with the


specified value in a list.

 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

# Write a program to remove the element (30) from the list


list1=[10,20,30,40,50]
list1.remove(30)
print(list1)

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.

 If no index number is given, then by default it removes the last


element from the list
 If we try to remove the index number which does not exist in the
then it gives an IndexError
# Write a program to remove the element from the second position in the
list
list1=[10,20,30,40,50]
list1.pop(2)
print(list1)

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.

# Write a program to clear the list


list1=["Delhi", "Bombay", "Chennai"]
list1.clear()
print(list1)

Output
[]

len(): It returns the length of a list.

# Write a program to check the length of the list


list1=[10,20,30,40,50]
print("length of list ",len(list1))

Output
length of list 5

reverse(): It reverses the content of the list in place.

# Write a program to reverse the elements of the list


list1=[10,20,30,40,50]
list1.reverse()
print(list1)

Output
[50, 40, 30, 20, 10]

sort(): It sorts the list in ascending or descending order. It works for


the list with the values of the same data type.

# Write a program to sort the elements of the list in ascending order


list1=[10,5,20,3,40,50]
list1.sort()
print(list1)
Output
[3, 5, 10, 20, 40, 50]

# Write a program to sort the elements of the list in descending order


list1=[10,5,20,3,40,50]
list1.sort(reverse=True)
print(list1)

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

# Write a program to find the index of 40 in the list


list1=[10,20,30,40,50]
print(list1.index(40))

Output
3

Count(): This function counts the number of occurrences of the specified


value in the given list.
 If the value doesn’t exist, then the function will return 0.

# Write a program to find the index of 40 in the list


list1=[10,20,30,40,50,40,78,40,56,40]
print(list1.count(40))

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']

Comparing the list

It compares the list in lexicographical order. For example, if


corresponding elements are same, it goes to the next element and so on
until it finds elements that differs. It uses relational or comparison
operators like >, <, >=, = = and !=

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

Operator Precedence Rule

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

You might also like