These AI Class 9 Notes Chapter 9 Lists in Python Class 9 AI Notes simplify complex AI concepts for easy understanding.
Class 9 AI Lists in Python Notes
Introduction Class 9 Notes
As we studied in the fundamental chapter, list is a type of container, which is used to store multiple data at the same time. List contains a sequence of heterogeneous elements which makes it powerful tool in Python. It can store integer, string as well as object in a single list. Lists are mutable which means they can be changed after creation. Each element of a list is assigned a number its position or index. The first index is zero, the second index is one, the third index is two and so on. List is one of the most frequently used and very versatile data type used in Python. A number of operations can be performed on the lists, which we will study as we go forward.
Creating a List Class 9 Notes
In Python, lists can be created to put the elements in square brackets [ ]. The elements in the list are separated by the comma (,).
For example,
Creating List From an Existing Sequence
In Python, list ( ) method is used to create list from an existing sequence.
Syntax:
new_list_name = list (sequence/string)
Here, sequence includes tuples, lists etc.
For example,
Output
[‘P’, ‘Y’, ‘T, ‘H’, ‘O’, ‘N’]
A = list (“PYTHON”)
print(A)
Output
[‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’]
l = (‘P’, ‘Y’, ‘T’, ‘H’, ‘0’, ‘N’)
l1 = list(l)
print (l1)
Output
[‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’]
Or
list() method is also used to create list of characters and integers through keyboard.
For example,
a = list(input(“Enter the elements ;”))
print(a)
Output
Enter the elements : 23456
[‘2’, ‘3’, ‘4’, ‘5’, ‘6’]
b = list(input(“Enter string: “))
print(b)
Output
Enter string:ARIHANT
[‘A’, ‘R’, ‘I’, ‘H’, ‘A’, ‘N’, ‘T’]
We can create different types of list in python as follows
(i) Empty List
Empty list can be created in Python using [ ]. It takes the truth value as false.
Here is the two ways to create empty list as
a = []
print (a)
Output
[]
or a = 1 ist()
print(a)
Output
[]
(ii) Mixed Data Type List
It can be created to place different data types such as integers, strings, double etc., into one list.
For example,
a = [‘Neha’ . ‘Sharma’.45, 34, 4]
print(a)
Output
[‘Neha’, ‘Sharma’, 45, 34, 4]
(iii) Nested List
Nested lists are list objects where the elements in the lists can be lists themselves
For example,
A = [‘Neha’ , 4,2,[5,23,4], 65]
print(A)
Output
[‘Neha’, 4, 2, [5, 23, 4], 65]
List A contains 5 elements while inner list contains 3 elements ([5,23,4]). List A is considered [5,23,4] as one element.
Accessing Lists
To access the list’s elements, index number is used. Use the index operator [ ] to access the elements of a list. The index should be an integer. Index of 0 refers to first element, 1 refers to second element and so on. While the index of -1 refers to the last element, -2 refers to the second last element and so on.
For example,
l1 = [5,7,3,4,5,6,9,0,8]
Output
34
76
Python
12
11
ERROR!
Traceback (most recent call last)
File “<main. py>”. line 7, in <module>
IndexError: list index out of range
Statement print (11[8]) will give Error as IndexError: list index out of range. Because it has 8 elements for which index are 0 to 7.
Traversing a List
Traversing a list is a technique to access an individual element of that list. It is also called iterate over a list. There are multiple ways to iterate over a list in Python.
These are as follows
(i) Using for loop
The most common and easy way to traverse a list is with for loop. for loop is used when you want to traverse each element of a list.
Syntax
for variable in list_name:
For example,
Output
P
R
O
G
A
M
(ii) Using for loop with range ( )
There is another method to traverse a list using for loop with range( ). This is also used len( ) function with range. This method is used when you want to traverse particular characters in a list.
Syntax
for variable in range (len(list_name)):
For example,
Output
P
R
O
G
A
M
Program to display the elements of list
[‘P’, ‘ Y ‘, ‘T”, ‘H’, ‘O’, ‘N’] in separate line with their index number.
For example,
Output
Element : P at index number 0
Element: R at index number 1
Element: O at index number 2
Element: G at index number 3
Element: A at index number 4
Element : M at index number 5
List Operations Class 9 Notes
We can perform various operations on list in Python, some of them are describe below.
(i) Concatenate Lists The most conventional method to perform on the list concatenaton, the use of (+) operator can easily add the whole of one list to other list and hence perform the concatenation.
Syntax
list = list1 + list2
For example,
Output
[43, 56, 34, 22, 34, 98]
The (+) operator cannot add list with other type as number or string because this operator is used only with list types.
For example,
Output
ERROR!
Traceback (most recent call last):
File “<main.py>”, line 2, in <module>
TypeError: can only concatenate list (not “int”) to list
(ii) Replicating List You can repeat the elements of the list using (*) operator. This operator is used to replicate the list.
Syntax
list = listl * digit
For example,
Output
[2,5,7,2,5,7]
(iii) Slicing of a List In Python list, there are multiple ways to print the whole list with all the elements, but to print a specific range of elements from the list, we use slice operation. Slice operation is performed on lists with the use of colon” (:).
Syntax
s-list_name [Start: End]
For example,
Output
[7, 6, 4]
To print elements from beginning to a range use [: Index], to print elements from end use [: -Index] and to print elements from specific index till the end use [Index:].
For example,
Output
[4,3,7,6,4]
[ 4,3,7,6 ]
[6, 4, 9, 5, 0, 3, 2]
You can also print all elements of list in reverse order using [: : -1 ].
For example,
Output
[2,3,0,5,9,4,6,7,3,4]
Lists are also provide slice steps which used to extract elements from list that are not consecutive.
Syntax
S = list_name [Start: Stop: Step]
It takes three parameters which are as follows
Start starting integer where the slicing of the object starts.
Stop integer until which the slicing takes place. The slicing stops at index stop -1.
Step integer value which determines the increment between each index for slicing.
For example,
Output
[3, 4, 0]
[7, 4, 5, 3]
[4, 4, 3]
[4, 3]
[6,4,9,5,0,3,2]
List Modification using Slicing Class 9 Notes
List can be modified after it created using slicing.
For example,
Output
[34, ‘Hello’, 4, ‘Try’, 54, ‘Again’]
[34, ‘Hello’, 4, ‘Try’, [‘World’], ‘Again’]
[34, ‘Hello’, ‘Hiiii’, ‘Try’, [‘World’], ‘Again’]
Built-in Functions Class 9 Notes
Python has large number of built-in functions and methods that make programming easier. Some of them are as follows
(i) append ()
This method is used for appending and adding elements to a list. It is used to add elements to the last position of a list.
Syntax
list_name-append (element)
For example,
Output
[34, 65, 23, 98, 76]
(ii) insert ()
This method is used to insert an element at specified position in the list. This method takes two arguments : one for index number and second for element value.
Syntax
list_name insert (index, element)
For example,
Output
[34, 65, 23, ‘New’, 98]
(iii) extend ()
This method is used to add contents of list 2 to the end of list 1.
Syntax
listnamel, extend (list_name2)
For example,
Output
[43, ‘Hello’, 56, ‘World’, ‘Try’, 65, 77]
(iv) sum ()
This method is used to calculate the sum of all the elements in the list.
Syntax
sum (list_name)
For example,
Output
169
sum () method is used for only numeric values otherwise it gives an error.
Output
Trackback (most recent call last) :
File “<pyshell# 17> “, line l , in <module>
sum(l)
TypeError: unsupported operand type (s) for + : ‘int’ and ‘str’
(v) count ()
This method is used to calculate total occurrence of given element of list.
Syntax
list_name count (element)
For example,
Output
4
(vi) len ( )
This method is used to calculate the total length of list.
Syntax
len (list_name)
For example,
Output
11
(vii) index ()
It returns the index of first occurrence. Start and end index are not necessary parameters.
Syntax
list_name.index (element[. start [. end]])
For example,
Output
4
(viii) reverse ()
Using the reverse () method, we can reverse the contents of the list object in-place, i.e. we don’t need to create a new list instead we just copy the existing elements to the original list in reverse order.
Syntax
list_name. reverse ( )
For example,
Output
[65, 54, 33, 89, 76, 34]
[‘Namesty’, ‘Hey’, ‘Hello’, ‘Hii’]
(ix) pop ()
This function is used to remove the element and return last value from the list or the given index value.
Syntax
list_name.pop (index)
For example,
If you do not give any index value, then it will remove last value from the list.
Output
[34, 65, 22, 87, 61]
[34, 65, 22, 87]
(x) remove ()
This method searches for the given element in the list and removes the first matching element. It takes a single element as an argument and remove it from the list.
Syntax
list_name. remove (element)
For example,
Output
[34, 76, 98, 26, 20]
Output
[‘Maths’, ‘English’, ‘Hindi’, ‘Science’]
If the element (argument) passed to the remove () method does not exist, ValueError exception is thrown.
l1. remove ()
Trackback (most recent call last) :
File “<pyshell# l1>”, line 1, in <module>
l1.remove ()
TypeError : remove () takes exactly one argument ( 0 given)
(xi) clear ()
This function is used to remove all the items of a list. This method will empty the entire list.
Syntax
list_name. clear ( )
For example,
Output
[]
(xii) sort ()
This function is used to sort the given list in ascending order.
Syntax
list _name. sort ( )
For example,
Output
[12, 23, 23,65,77,90, 99]
Output
[‘abc’, ‘abc’, ‘gdr’,’nki’, ‘uyt’]
The sort function has an argument called
reverse = True. This allows us to sort the list elements in descending order.
Syntax
list _ name. sort (reverse – True)
For example,
Output
[99,90,77,65,23,23,12]
Programs Class 9 Notes
1. Program to calculate the sum and mean of the elements which are entered by user.
Output
Enter the size of the list : 6
Enter the number :
34
87
12
98
54
77
SUM =362
MEAN =60.333333333333336
2. Program to enter the elements of a list and reverse these elements.
Output
Enter the size of the list : 6
Enter the number :
34
76
23
98
67
23
Entered list : [34,76,23,98,67,23]
Reversed list : [23, 67, 98, 23, 76, 34]
Glossary
- List It is a type of container, which is used to store multiple data at the same time.
- append() This method is used for appending and adding elements to a list. It is used to add elements to the last position of a list.
- insert() This method is used to insert an element at specified position in the list.
- extend() This method is used to add contents of list 2 to the end of list 1.
- sum() This method is used to calculate the sum of all the elements in the list.
- len() This method is used to calculate the total length of list.
- pop() This function is used to remove the element and return last value from the list or the given index value.
- remove() This method searches for the given element in the list and removes the first matching element.
The post Lists in Python Class 9 AI Notes appeared first on Learn CBSE.