0% found this document useful (0 votes)
2 views9 pages

Python

The document contains a series of Python programming experiments for a B.Tech course in Computer Science & Engineering. Each experiment includes a specific task such as finding the GCD of two numbers, calculating square roots using Newton's method, and implementing various search and sorting algorithms. The document provides code snippets and explanations for each experiment, demonstrating fundamental programming concepts.

Uploaded by

Dev Patil
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)
2 views9 pages

Python

The document contains a series of Python programming experiments for a B.Tech course in Computer Science & Engineering. Each experiment includes a specific task such as finding the GCD of two numbers, calculating square roots using Newton's method, and implementing various search and sorting algorithms. The document provides code snippets and explanations for each experiment, demonstrating fundamental programming concepts.

Uploaded by

Dev Patil
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
You are on page 1/ 9

Sanghvi Institute of

Management & Science


Session 2024 – 25

B.Tech
Computer Science & Engineering

Subject – Lab ( Python )


Semester – 5th Sem

Submitted by – Dev Patil


Roll No. – 0837CS211002
Experiment 1 : To write a Python program to find GCD of two numbers.

# Recursive function to return gcd of a and b


def gcd(a, b):

# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a

# base case
if (a == b):
return a

# a is greater
if (a > b):
return gcd(a-b, b)
return gcd(a, b-a)

# Driver program to test above function


a = 98
b = 56
if (gcd(a, b)):
print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
print('not found')
Experiment 2 : To write a Python Program to find the square root of a number by
Newton’s Method.

# Function to return the square root of


# a number using Newtons method
def squareRoot(n, l) :

# Assuming the sqrt of n as n only


x = n

# To count the number of iterations


count = 0

while (1) :
count += 1

# Calculate more closed x


root = 0.5 * (x + (n / x))

# Check for closeness


if (abs(root - x) < l) :
break

# Update root
x = root

return root

# Driver code
if __name__ == "__main__" :

n = 327
l = 0.00001

print(squareRoot(n, l))
Experiment 3 : To write a Python program to find the exponentiation of a
number.

def CalculatePower(N,X):

P=1

for i in range(1, X+1):

P=P*N

return P

N,X=2,3

print(CalculatePower(N,X))

N,X=3,4

print(CalculatePower(N,X))

Experiment 4 : To write a Python Program to find the maximum from a list of


numbers.

a = [10, 24, 76, 23, 12]

# Assuming first element is largest.

largest = a[0]

# Iterate through list and find largest

for val in a:

if val > largest:

# If current element is greater than largest

# update it

largest = val

print(largest)
Experiment 5 : To write a Python Program to perform Linear Search.

# Python program for Linear Search using iterative approach

def linear_search(arr, target):

# Traverse through all elements in the array

for index in range(len(arr)):

# If the element is found, return its index

if arr[index] == target:

return index

# If the element is not found, return -1

return -1

# Example usage:

arr = [10, 23, 45, 70, 11, 15]

target = 70

# Function call

result = linear_search(arr, target)

if result != -1:

print(f"Element found at index: {result}")

else:

print("Element not found in the array")


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

# It returns index of x in given array arr if present,

# else returns -1

def binary_search(arr, x):

low = 0

high = len(arr) - 1

mid = 0

while low <= high:

mid = (high + low) // 2

# If x is greater, ignore left half

if arr[mid] < x:

low = mid + 1

# If x is smaller, ignore right half

elif arr[mid] > x:

high = mid - 1

# means x is present at mid

else:

return mid

# If we reach here, then the element was not present

return -1

# Test array

arr = [ 2, 3, 4, 10, 40 ]

x = 10

# Function call

result = binary_search(arr, x)
if result != -1:

print("Element is present at index", str(result))

else:

print("Element is not present in array")

Experiment 7 : To write a Python Program to perform selection sort.

# Selection sort in Python

# time complexity O(n*n)

#sorting by finding min_index

def selectionSort(array, size):

for ind in range(size):

min_index = ind

for j in range(ind + 1, size):

# select the minimum element in every iteration

if array[j] < array[min_index]:

min_index = j

# swapping the elements to sort the array

(array[ind], array[min_index]) = (array[min_index], array[ind])

arr = [-2, 45, 0, 11, -9,88,-97,-202,747]

size = len(arr)

selectionSort(arr, size)

print('The array after sorting in Ascending Order by selection sort is:')

print(arr)
Experiment 8 : To write a Python Program to perform insertion sort.

def insertionSort(arr):

n = len(arr) # Get the length of the array

if n <= 1:
return # If the array has 0 or 1 element, it is already sorted, so
return

for i in range(1, n): # Iterate over the array starting from the second
element

key = arr[i] # Store the current element as the key to be inserted in


the right position

j = i-1

while j >= 0 and key < arr[j]: # Move elements greater than key one
position ahead

arr[j+1] = arr[j] # Shift elements to the right

j -= 1

arr[j+1] = key # Insert the key in the correct position

# Sorting the array [12, 11, 13, 5, 6] using insertionSort

arr = [12, 11, 13, 5, 6]

insertionSort(arr)

print(arr)
Experiment 9 : To write a Python program to find first n prime numbers.

#function to check if a given number is prime


def isPrime(n):
#since 0 and 1 is not prime return false.
if(n==1 or n==0): return False

#Run a loop from 2 to n-1


for i in range(2,n):
#if the number is divisible by i, then n is not a prime number.
if(n%i==0):
return False

#otherwise, n is prime number.


return True

# Driver code
N = 100;
#check for every number from 1 to N
for i in range(1,N+1):
#check if current number is prime
if(isPrime(i)):
print(i,end=" ")

You might also like