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

Python Lab Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
29 views

Python Lab Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Objective: To write a python program that takes in command line arguments as input and print the number of arguments. Code: import sys # Get the command line arguments argunents = sys.arev[1:] # Print the nunber of arguments run_argunents = len(argunents) print(*Nunber of argunents:", nun_argunents) Output: ‘Name: Veervijay Kushwaha Roll no, 2100970130124 Result: The program is executed successfully and showing the required result. EXPERIMENT — 07 Objective: To write a python program to find the most frequent words in a text file. Code: from collections import Counter Inport re def find_most_frequent_words(file_path, num_words): with open("command_line_arg.txt", 'r’) as file: text = file.read() # Remove punctuation and convert to lowercase text = re-sub(r'[*\w\s]’, ‘', text-lower()) words = text.split() word_counts = Counter(words) most_conmon = word_counts.most_common(nun_words) print("Most frequent words: for word, count in most_conmon: print(word, *:", count) file_path = ‘path/to/your/text/file.txt’ nun_words = 5 print("veervijay kushwaha") Find_most_frequent_words(file_path, num_words) Output: Name: Veervijay Kushwaha Roll no. 2100970130124 Result: The program is executed successfully and showing the required result. EXPERIM! Objective: To write a python program to find the maximum of a list of numbers. Code: def find_naximun(nunbers): if not numbers: return None max_nunber = nunbers[@] for nunber in numbers: if number > max_nunber: max_number = nunber return max_number print("Veervijay kushwaha") numbers list = [10, 5, 8, 26, 3, 15. maximun = find_naximun(nunbers_list) print(*Maximum nunber:", maximum) Output: Result: The program is executed successfully and showing the required result. Objective: To write a python program for linear search, Code: def Linear_search(arr, target): for § in range(len(arr)) if arr{i] == target: return i return -1 arr = [4, 2, 9, 1, 7, 5] print("Veervijay Kushwaha*) target = 7 result = linear_search(arr, target) if result l= -1 print("Elenent", target, “found at index”, result) else: print("Elenent™, target, *not found") Output: Result: The program is executed successfully and showing the required result. Obje ‘To write a python program for Binary search Code: def binary_search(arr, target): low = @ high = len(arr) - 4 while low <= high: mid = (ow + high) // 2 if arr(mid] return mid elif arr[mid] < target low = mid +1 arget else: high = mid - 1 return =1 arr = [1, 2, 3, 4, 5, 7, 9] target = 7 print(“Veervijay Kushwaha") result = binary_search(arr, target) if result |= -1 print("Elenent", target, “found at index”, result) else: print(“Element", target, “not found") Output: Result: The program is executed successfully and showing the required result. EXPERIMENT — 11 Objective: Code: ‘To write a python program for selection sort and insertion sort. a) Code for selection sort: def selection_sort (arr): for i in range(len(arr)): min_idx = i for j in range(itt, len(arr)): Af are(j] < arr(min Ade]: min_idx = j arr[i], arr[min_idx] = are[min_idx], arr[i] return arr print(“Veervijay Kushwaha") arr = [5, 3, 8, 2, 2, 7] sorted_arr = selection_sort(arr) print ("Sorted array:", sorted_arr) b) Code for Insertion sort: def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] jei-a while j >= @ and arr[j] > key: arr[j+1] = arr[3] j--1 arr[J+1] = key return arr print("Veervijay Kushwaha") arr = (5, 3, 8) 2, 4, 7] sorted_arr = insertion_sort(arr) print(*Sorted array:", sorted_arr) Output 1(selection sort): Output 2(Insertion sort): Result: The program is executed successfully and showing the required result.

You might also like