0% found this document useful (0 votes)
4 views2 pages

Python Programs for Common Algorithms

The document contains a series of Python programming exercises including finding the GCD of two numbers, calculating square roots using Newton's method, performing linear and binary searches, and sorting algorithms like selection and insertion sort. It also includes a program to find the first n prime numbers and demonstrates how to handle command line arguments. Each exercise is accompanied by sample code and output statements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python Programs for Common Algorithms

The document contains a series of Python programming exercises including finding the GCD of two numbers, calculating square roots using Newton's method, performing linear and binary searches, and sorting algorithms like selection and insertion sort. It also includes a program to find the first n prime numbers and demonstrates how to handle command line arguments. Each exercise is accompanied by sample code and output statements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Lab File

1. To write a Python program to find GCD of two numbers.


import math
num1 = 60
num2 = 48
print("The GCD is:", [Link](num1, num2))

2. To write a Python Program to find the square root of a number by Newton's Method.
def newton_sqrt(n):
root = n / 2 # Initial guess
for _ in range(10):
root = 0.5 * (root + n / root)
return root
print("Square Root:", newton_sqrt(25))

3. To write a Python program to find the exponentiation of a number.


base = 2
exp = 5
print(f"{base} to the power of {exp} is:", base ** exp)

4. To write a Python Program to find the maximum from a list of numbers.


numbers = [10, 50, 25, 80, 5]
print("Maximum number is:", max(numbers))

5. To write a Python Program to perform Linear Search.


def linear_search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = [2, 3, 4, 10, 40]
print("Element found at index:", linear_search(arr, 10))

6. To write a Python Program to perform binary search.


def binary_search(arr, x):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x: return mid
elif arr[mid] < x: low = mid + 1
else: high = mid - 1
return -1
arr = [2, 3, 4, 10, 40]
print("Element found at index:", binary_search(arr, 10))
7. To write a Python Program to perform selection sort.
arr = [64, 25, 12, 22, 11]
for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
print("Sorted array:", arr)

8. To write a Python Program to perform insertion sort


arr = [12, 11, 13, 5, 6]
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
print("Sorted array:", arr)

9. To write a Python program to find first n prime numbers


n = 10
primes = []
num = 2
while len(primes) < n:
for i in range(2, int(num**0.5) + 1):
if num % i == 0: break
else:
[Link](num)
num += 1
print(f"First {n} primes:", primes)

10. To write a Python program for command line arguments


import sys
print("Script name:", [Link][0])
print("Arguments:", [Link][1:])

You might also like