Exploring Python Lists in Class XI Module-4 by Mrs. Sujata Pradhan
Exploring list structures, characteristics, and operations in Python. Covering topics such as mutability, basic list operations, indexing, slicing, concatenation, repetition, traversal, updation, and comparison. Detailed explanations on list methods like append, extend, insert, index, sort, count, reverse, pop, remove, and clear. Methods for inserting elements, concatenating lists, and various ways of removing elements. Understanding linear search in Python lists with a step-by-step procedure. Creation of lists in Python and a sample program for linear search along with output demonstration.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
PYTHONLIST CLASS XI (MODULE-4) BY Mrs. SUJATAPRADHAN, PGT(SS), Computer Science, AECS,ANUPURAM
SUMMARY OF PYTHON LIST List structure and characteristics Mutability Basic List operations Positive & NegativeIndexing List slicing Concatenation &Repetition Membership Operation List traversal List updation List comparison
List methods append() extend() insert() index() sort() count() reverse() pop() remove() clear() Insert an element in a list: The list.insert(i,x) method The list.append(x) method Concatenate two lists: +operator list.extend(iterable)
removing elements from a list There are three ways of removing elements from a list: Using the del keyword delete one or multiple items of a list using the del keyword, Using the list.remove(x)method The list.remove(x) method deletes the first matching element from the list. Using the list.pop([x])method The list.pop([x]) method removes the item at the given index, and returns it. If the index is not provided, the list.pop() method removes and returns the last item of the list.
Python Linear search on list What is a Linear Search? A linear search is also known as a sequential search. It is a method of finding an element within a list by checking each element of the list sequentially until a match is found. Procedure: Start search from the leftmost element of list. compare searched element ,say X with each element of the list one by one. If X matches with any element of the list, search is successful. If it doesn't match with any of elements, it is an unsuccessfulsearch.
List Creation A list is an ordered and mutable Python container.It is one of the most common data structures in Python.To create a list, the elements are placed inside square brackets ([]) and separated by commas. Generally we create a new list using three steps. Create an empty list. Loop through the list. Append the elements to the new list.
Program for Linear Search L = [] found = False n = int(input("Enter size of list")) for i in range(n): num = int(input("Enter a number: ")) L.append(num) print( List: ,L) X = int(input( Enter a number to search:")) for i in range(len(L)): if L[i] ==X: found = True print(X , found at position , i+1) break else: print( Unsuccessfulsearch ) OUTPUT Enter size of list4 Enter a number: 10 Enter a number: 25 Enter a number: 5 Enter a numbers: 30 List: [10,25,5,30] Enter a number to search: 5 5 found at position 3
find the largest number in alist list1=[] n=int(input("Enter number of elements:")) for i in range(1,n+1): b=int(input("Enter element:")) list1.append(b) max=list1[0] for a in list1: if a > max: max =a print( Largest element is: , max) OUTPUT: Enter number of elements:5 Enter element:2 Enter element:5 Enter element:3 Enter element:7 Enter element:6 Largest element is:7 #Using sort() a=[] n=int(input("Enter number of elements:")) for i in range(1,n+1): b=int(input("Enter element:")) a.append(b) a.sort() print("Largest element is:",a[n-1])
working with multi dimensionallist List = [ Hello", Python", World"] print("Accessing element from the list") print(List[0]) print(List[2]) List = [[ Hello', Python'] ,[ World']] # accessing an element from a Multi-Dimensional List print("Accessing element from a Multi-Dimensionallist") print(List[0][1]) print(List[1][0]) Output: Accessing element from thelist Hello Python Accessing element from a Multi-Dimensionallist Python World
Program to add two matrices using nested loop X = [[12,7,3], [4,5,6], [7,8,9]] [[5,8,1], [6,7,3], [4,5,9]] Nested loops are used to iterate through each row and eachcolumn. Y = Output [17, 15,4] [10, 12,9] [11, 13, 18] result=[[0,0,0], [0,0,0], [0,0,0]] for i in range(len(X)): for j in range(len(X[0])): result[i][j] = X[i][j] + Y[i][j] for r in result: print(r)
program to remove duplicate elements List1 = [] n = int(input("Enter the number of elements in the list:")) for i in range(0,n): list1.append(input("Enter the element:")) print( List1= , list1) # Declare an empty list that will store unique values list2 = [] for i in list1: if i not in list2: list2.append(i) print( List2= ,list2) Output: Enter the number of elements in the list:4 Enter the element:25 Enter the element:46 Enter the element:25 List1=[25,46,25] List2=[25,46]