Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Introduction
In chapter 2, we have learnt about Basics of Python and simple data types like integer, strings etc. Now, we’ll explore two important data structures in Python — Lists and Tuples.
These are collections of data used in Artificial Intelligence projects to store and manage multiple values efficiently.
Do You Know?
Python Lists are one of the most used data structures in the world!
Even NASA uses Python for space data analysis
Introduction to Lists in Python
A List is a sequence of values of different data types like — numbers, text, or even other lists. Values in the list are called elements / items. Lists are enclosed within square brackets [ ], and their elements are separated by commas (,).
Examples of List :
Example 1 : L1 is the list of first five odd numbers
>>>L1 = [1, 3, 5, 7, 9]
>>>print(L1)
>>>[1, 3, 5, 7, 9]
Example 2 : L2 is the list of vowels
>>>L2 = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
>>>print(L2)
>>>[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
Example 3 : L3 is the list of first five multiples of 7
>>>L3 = [7, 14, 21, 28, 35]
>>>print(L3)
>>>[7, 14, 21, 28, 35]
Example 4 : L4 is the list of mixed data types
>>>L4 = [7, ‘school’, 23.5, 17.5, ‘college’]
>>>print(L4)
>>>[7, ‘school’, 23.5, 17.5, ‘college’]
Example 5 : L5 is the list of lists called nested list
>>>L5 = [[“Aman”, 34], [“Sumit”, 30], [“Ravi”, 29]]
>>>print(L4)
>>>[[“Aman”, 34], [“Sumit”, 30], [“Ravi”, 29]]
NOTE: List is one of the most frequently used and very versatile data type used in Python.
Features of List
- Lists can contain different types of data like numbers, strings, or even another list.
- Lists are mutable, meaning we can change their content after creation.
- Lists are very flexible and are used almost everywhere in Python programming.
- All the elements of List are enclosed in square brackets.
How to create a list ?
In Python programming, a list can be created by placing all the items (elements) inside a square bracket [ ], separated by commas. It can have any number of items of different types (integer, float, string etc.).
L1 = [ ] # Empty list
age = [15, 12, 18] # List of integers
data = ["Ansh", 5.7, 60] # List with mixed data types
student_marks = ["Aditya", "10-A", ["English", 75]] # Nested list (a list inside another list)
Do You Know?
A list inside another list is called a nested list.
L1 = [23, 45.5, [3, 4, 5], 56] #L1 is a nested list as it contains another list
How to access elements of a list ?
There are two ways to access an individual element of a list:
1. Positive indexing
Index in the list starts from 0, so if a list has 5 elements the index will start from 0 and go on till 4.
2. Negative indexing
Python also allows negative indexing, which is helpful for accessing elements from the end of the list. The index -1 refers to the last item and the index -2 refers to the second last item, and so on.
NOTE: Each element in a list has a position, known as an index. Indexes start from 0 for the first element.
The elements of a list are accessed using index number. for example
L1 = [23, 43, 12, 54, 34, 65]
| Positive Indexing | 0 | 1 | 2 | 3 | 4 | 5 |
| 23 | 43 | 12 | 54 | 34 | 65 | |
| Negative Indexing | -6 | -5 | -4 | -3 | -2 | -1 |
>>>L[2] return 12
>>>L[0] return 23
>>>L[5] return 65
>>>L[-3] return 54
>>>L[1+3] returns 34
>>>L[9] returns error as index is out of range
Access elements in Nested List
>>>L2 = [23, 45, 56, [34, 32]]
>>>L2[-1] return [34, 32]
>>>L2[3][0] return 34
>>>L2[3][1] return 32
>>>L2[3.1] return error as only Integer can be used for indexing.
Do You Know?
Python Lists can even store other lists or tuples, making them super flexible for AI data handling!
Quick Exercise: Accessing a Nested List
For the list n_list = ["Happy", [2, 0, 1, 5], 42, 66, 55]]:
- How would you access the letter ‘a’ from “Happy”?
- How would you access the number ‘5’ from the inner list?
- How would you access the letter ‘H’ from “Happy”?
- How many elements are in the above list?
- How would you access the number ’66’ from the above list?
How to add elements in List?
There are three common methods which allow us to add an element in a list.
1. append( ):
This function add a single element at the end of the list. for example
>>>L = [34, 45, 56, 23, 12]
>>>L.append(77)
>>>print(L)
OUTPUT : [34, 45, 56, 23, 12, 77]
L.append(28,9) will return type error as append function can take only one argument
This function can add a list at the end of another list. for example
>>>L.append([28, 29])
OUTPUT : [34, 45, 56, 23, 12, 77, [28, 29] ]
——————————————————————————————————————————
>>>List = [1, 2, 4]
>>>List.append([‘Good’, ‘Morning’])
>>>print(List)
OUTPUT : [1, 2, 4, [‘Good’, ‘Morning’]]
——————————————————————————————————————————
L2 = [ ]
print("Initial blank List: ")
print(List)
#Addition of Elements in the List
L2.append(12)
L2.append(25)
L2.append(41)
print("\nList after Addition : ")
print(List)
OUTPUT
Initial blank List:
[ ]
List after Addition:
[12, 25, 41]
2. extend( )
This function adds all the elements of one list at the end of the other list. for example :
L1 = [1, 6, 3, 9]
L2 = [12, 13, 18, 19]
L1.extend(L2)
print(L1)
OUTPUT : [1, 6, 3, 9, 12, 13, 18, 19]
All the elements of list L2 are added at the end of the list L1
Q1. Write the output of the following two coding:
L1 = [1, 2, 3]
L2 = [7, 8 ,9]
L1.append(L2)
print(L1)
OUTPUT : [1, 2, 3, [7, 8, 9]]
L1 = [1, 2, 3]
L2 = [7, 8 ,9]
L1.extend(L2)
print(L1)
OUTPUT : [1, 2, 3, 7, 8, 9]
Q2. Differentiate between append( ) and extend( ) function.
| append() | extend() |
| Add single element at the end of the list. | Add multiple elements at the end of the list. |
| Length of List is increased by 1 | Length of List is increased by number of elements added. |
3. insert( )
This function help us to add an element at a specific index value. for example
L1 = [23, 12, 45, 32]
L1.insert(3, 77) # This statement will add element 77 at index value 3 ie position number 4
print(L1)
OUTPUT : [23, 12, 45, 77, 32]
L1.insert(1, 91) # This statement will add element 91 at index value 1.
print(L1)
OUTPUT : [23, 91, 12, 45, 77, 32]
NOTE: insert( ) method takes two arguments.
How to remove elements from List?
Elements from a list can removed using two methods
1. remove( ):
This function delete the first occurrence of specified element. This function is used when we know the element to be deleted but not the index value. for example:
L1 = [90, 56, 87, 98, 23, 6, 78, 98]
L1.remove(98) #will remove element 98 from the list which is at index value 3
print(L1)
OUTPUT : [90, 56, 87, 23, 6, 78, 98]
L2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘c’]
L2.remove(‘b’) #will remove element ‘b’ from the list which is at index value 1
print(L2)
OUTPUT : [‘a’, ‘c’, ‘d’, ‘e’, ‘c’]
L3 = [1, 4, 6, 7, 3, 2]
L3.remove(9)
OUTPUT :
ValueError: list.remove(x): x not in list
2. pop( ):
This function delete/remove the element from the specified index. This function also return the deleted element. for example
>>>L1 = [90, 56, 87, 98, 23, 6, 78]
>>>print(L1.pop(3 )) #Output of this statement shows that it return the element.
>>>98
>>>print(L1)
>>>[90, 56, 87, 23, 6, 78]
NOTE : If we don’t give any index value in pop( ) function then it will delete the last element.
>>>L1 = [90, 56, 87, 98, 23, 6, 78]
>>>print(L1.pop( )) #Output of this statement shows that it return the element.
>>>78
>>>print(L1)
>>>[90, 56, 87, 98, 23, 6]
>>>L2 = [“Amit”, “Sumit”, “Naman” , “Manan”, “Kapil”]
>>>print(L2.pop(-2)) # pop( ) function work with negative index value also.
>>>Manan
>>>print(L2)
>>>[‘Amit’, ‘Sumit’, ‘Naman’, ‘Kapil’]
Quick Exercise:
Q1. Create a list with numbers 1 to 5 and print the second element.
Q2. Add two new elements (6 and 7) using append() and print the new list.
Q3. Remove one element ie 4 using pop() and print the updated list.
Q4. Slice the list to display only the first three elements.
Q5. Create a list of 5 subjects and print only the second and fourth subjects using indexing.
Some of the other functions which can be used with lists are mentioned below:
1. clear(): This method is used to remove all elements from the list. In other words we can say that it makes the list empty. for example
>>>L1 = [45, 67, 32, 89, 94]
>>>L1.clear( )
>>>L1
[ ]
2. index( ): It returns the index of the first occurrence of matched element.
>>>L1 = [45, 67, 32, 89, 94, 67]
>>>L1.index(67)
>>>1
3. count( ): It returns the frequency of the element passed as argument.
>>>L1 = [45, 67, 32, 89, 94, 67]
>>>L1.count(67)
>>>2
4. sort( ): It arranges all the elements in the list in ascending order.
>>>L1 = [45, 67, 32, 89, 94, 67]
>>>L1.sort()
>>>L1
>>>[32, 45, 67, 67, 89, 94]
5. reverse( ): This function simply reverse the arrangement of elements in the list.
>>>L1 = [45, 67, 32, 89, 94, 67]
>>>L1.reverse()
>>>L1
>>>[67, 94, 89, 32, 67, 45]
6. copy( ): It returns the copy of the list.
>>>L1 = [45, 67, 32, 89, 94, 67]
>>>L2=L1.copy()
>>>L2
>>>[45, 67, 32, 89, 94, 67]
What is String Slicing in Python?
String slicing in Python is to obtain a substring from the main string. Slice of string means part (substring) of string.
Syntax of String Slicing in Python
<String Name>[Start : Stop : Step]
Start : A position from where we have to start the cutting of string.
Stop : A position before what we have to cut the string.
Step : Number of characters to be skipped during string slicing in python.
NOTE : Position of stop is not included in the slicing
By default Start is 0
By default Step is 1
Quick Exercise:
Q1. Create a list of 5 favorite colours
Q2. Add one new colour using append().
Q3. Remove any one colour using remove().
Q4. Try printing only the first three using slicing.
Python List Slicing Examples
Q1. Write the Answers of the following in reference to given String:
str = “string slicing”
| str | s | t | r | i | n | g | s | l | i | c | i | n | g | |
| +ve Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| -ve Index | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
a) print(str[3 : 6])
Here Slicing is starting from index 3 (from alphabet ‘i’) and ending position is before 6 (till alphabet ‘g’)
Output : ing
b) print(str[4 : 10])
Here Slicing is starting from index 4 (from alphabet ‘n’) and ending position is before 10 (till alphabet ‘i’)
Output : ng sli
c) print(str[ : ])
Output : string slicing
d) print (str[7 : ])
Output : slicing
e) print(str[-7 : ]
Output : slicing
f) print(str[-9 : -3]
Output : g slic
g) print(str[2 : 10 : 2])
Output : rn l
h) print(str[-1 : -1 : -1])
Output : Nothing will print
h) print(str[-11 : -4])
Output : ing sli
i) print(str[-14 : : -3])
Output : si in
j) print( str[ : : -3])
Output : gcsnt
——————————————————————————————————————————
Introduction to Tuples
A tuple is an ordered sequence of elements of different data types, such as integer, float, string or list. Elements of a tuple are enclosed in parenthesis(round brackets) and are separated by commas. for example :
>>> a = (1, ‘a’, 7, 6.5) # a is the tuple of mixed data type
>>> b = (2, 4, 6, 8, 10) # b is a tuple of only integers
>>> c = (“English”, “Hindi”, “Math”, “Science”) # c is a tuple of only string
>>> d = (2, 4, 7, [4, 5, 6]) # d is a tuple with list as an element
| Incorrect way of assigning a single element | Correct way of assigning a single element |
| >>> a = (2) Verification : >>> type(a) <class ‘int’> | >>> a = (2,) Verification : >>> type(a) <class ‘tuple’> |
Do You Know?
Tuples are faster than lists and are often used to store fixed data
Accessing elements of Tuples
Elements of a tuple can be accessed in the same way as a list or string using indexing and slicing. for example
>>> a = (‘C’ , ‘ O’ , ‘M’ , ‘P’ , ‘U’ , ‘T’ , ‘E’ , ‘R’)
| ELEMENTS | C | O | M | P | U | T | E | R |
| POSITIVE INDEX VALUE | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| NEGATIVE INDEX VALUE | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
>>> a[4] # Output is U (fifth element of tuple)
>>> a[7] # Output is R (Eighth element of tuple)
>>> a[-1] # Output is R (last element of tuple or first element from right)
>>> a[9] # Output is IndexError: tuple index out of range
Deleting a Tuple
We can not delete a particular element/elements from tuple as it is immutable. Entire tuple can be deleted by using del statement.
>>> T = [3, 4, 5, 6]
>>> T
[3, 4, 5, 6]
>>> del T
>>> T
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
T
NameError: name 'T' is not defined
Difference between List and Tuple :
| LIST | TUPLE |
| It is mutable data type. | It is an immutable data type. |
| Elements are enclosed in Square brackets ie [ ] | Elements are enclosed in Parenthesis. ie ( ) |
| Iterating through a list is slower as compared to a tuple | Iterating through a tuple is faster as compared to a list |
Disclaimer : I tried to give you the correct Notes of Ch 4 More about List and Tuple in python Class 9 Notes (AI-417), but if you feel that there is/are mistakes in the Notes Ch 4 More about List and Tuple in python Class 9 Notes given above, you can directly contact me at csiplearninghub@gmail.com. Also Share your valuable feedback about the above Ch 4 More about List and Tuple in python Class 9 Notes or any other suggestion so that I can give better content to you. The Notes of Ch 4 More about List and Tuple in python Class 9 Notes are from NCERT BOOK available on CBSE website. The content is not copied from any other site.
Important Links
Class IX A.I. Book
Class IX AI Curriculum 2025-26
Class X AI Book
Class X AI Curriculum 2025-26
Python Manual
—————————————————————————————————————————–
Chapter 1: Algorithms and Flowcharts NOTES
Chapter 2: Introduction to Python NOTES
Chapter 3 – Introduction to tools for AI NOTES
Chapter 4 – More about List and Tuples
——————————————————————————————————————————
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 Notes Important Points
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 MCQ
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 Question Answers
—————————————————————————————————————————–
Unit 2 – Data Literacy NOTES Important Points
Unit 2 – Data Literacy MCQ
Unit 2 – Data Literacy Question Answers
——————————————————————————————————————————
Unit 3 – Math in AI NOTES Important Points
Unit 3 – Math in AI MCQ
Unit 3 – Math in AI Question Answers
——————————————————————————————————————————
Unit 4 – Generative AI NOTES Important Points
Unit 4 – Generative AI MCQ
Unit 4 – Generative AI Question Answers
——————————————————————————————————————————
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
Ch 4 More about List and Tuple in python Class 9 Notes Important Points
v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v