New Pytho
New Pytho
Output :
Arguments passed :
5,7,3,2
Result : 17
Program 2
Python program to perform Matrix Multiplication.
# iterating by row of A
for i in range(len(A)):
# iterating by column by B
for j in range(len(B[0])):
# iterating by rows of B
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
for r in result:
print(r)
Output :
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Program 3
Python program to compute the GCD of two numbers.
def hcf(a, b):
if(b == 0):
return a
else:
return hcf(b, a % b)
# prints gcd
print("The gcd of is : ",
end="") print(hcf(a, b))
Output :
line_word = line.lower().replace(',',
'').replace('.', '').split(" ")
for w in line_word:
words.append(w)
count = 1
x = n
count = 0
while (1):
count += 1
x = root
return root
if __name__ == "__main__":
n = 327
l = 0.00001
print(squareRoot(n, l))
Output :
18.083141320025124
Program 6
Python program exponentiation (power of a number).
def calc_power(N, p):
if p == 0:
return 1
return N * calc_power(N, p-1)
Output :
Enter the number.3
Enter the exponential power.4
81
Program 7
Python program find the largest element of a list.
list1 = []
Output :
Enter number of elements in list: 5
Enter elements: 4
Enter elements: 7
Enter elements: 15
Enter elements: 21
Enter elements: 12
Largest element is: 21
Program 8
Python program for linear search.
def linearSearch(array, n, x):
array = [2, 4, 0, 1, 9]
x = 1
n = len(array)
result = linearSearch(array, n,
x) if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)
Output :
Element found at index 3.
Program 9
Python program Binary search.
def binary_search(arr, x):
low = 0
high = len(arr) - 1
mid = 0
if arr[mid] < x:
low = mid + 1
else:
return mid
return -1
result = binary_search(arr, x)
if result != -1:
print("Element is present at index",
str(result)) else:
print("Element is not present in array")
Output :
Element found at index 3.
Program 10
Python program selection sort.
def selectionSort(array, size):
Output :
The array after sorting in Ascending Order by Selection
Sort is :
[-202,-97,-9,-2,0,11,88,747]
Program 11
Python program insertion sort.
def insertionSort(arr):
if (n := len(arr)) <= 1:
return
for i in range(1, n):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
Output :
Sorted Array is :
[5,6,11,12,13]
Program 12
Python program merge sort.
def merge(arr, l, m,
r): n1 = m - l + 1
n2 = r - m
L = [0] * (n1)
R = [0] * (n2)
i = 0
j = 0
k = l
def mergeSort(arr, l,
r): if l < r:
m = l+(r-l)//2
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
arr = [12, 11, 13, 5, 6,
7] n = len(arr)
print("Given array is")
for i in range(n):
print("%d" % arr[i], end=" ")
mergeSort(arr, 0, n-1)
print("\n\nSorted array
is") for i in range(n):
print("%d" % arr[i], end="
") Output :
Given Array is :
12 11 13 5 6 7
Sorted Array is :
5 6 7 11 12 13
Program 13
Python program first n prime numbers.
def isPrime(n):
if (n == 1 or n == 0):
return False
return True
N = 100
for i in range(1, N+1):
if (isPrime(i)):
print(i, end=" ")
Output :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83
89 97
The array after sorting in Ascending Order by selection sort
Element is present at index 3
Element is present at index 3