Python Lab
Python Lab
ALGORITHM:
Step 1: Start.
Program:
rem =n1%n2
while rem!=0:
n1 = n2
n2 = rem
rem = n1% n2
Enter a number:54
Enter a number:54
Result:
Thus the Python program to compute GCD of two numbers is executed successfully and the output is
verified.
EX.NO.3 Exponentiation of a number
AIM:
ALGORITHM:
Step 1 : Start
. Step 8 : Stop.
Program:
n=int(input('enter number:'))
e=int(input('enter exponent:'))
r=n
for i in range(1,e):
r=n*r
print('exponentiation is:',r)
OUTPUT:
Exponentiation is:9
Exponentiation is:32
list1=[]
n=int (input())
a=int (input(""))
list1.append(a)
maxno = list1[0]
minno = list1[0]
maxno = list1[i]
minno = list1[i]
OUTPUT:
AIM:
ALGORITHM:
Step 1: Start
Step 5: Search the element with using for loop until length of list
PROGRAM/SOURCE CODE :
found = False
for i in range(len(list)):
if(list[i] == x):
found = True
break
else:
ALGORITHM:
Exit
Go to Step:1
Go to Step:2
Else
{element == arr[mid] }
exit
def Binary_search(arr,start,last,element):
mid =(int)(start+last)/2
if(start>last):
elif (element>arr[mid]):
start = mid+1
Binary_search(arr,start,last,element)
elif (element<arr[mid]):
last = mid-1
Binary_search(arr,start,last,element)
else:
arr = [2,14,19,21,99,210,512,1028,4443,5110]
element = 4443
start = 0
last = len(arr)-1
Binary_search(arr,start,last,element)
Ex.No.6a SELECTION SORT
array = [1,45,10,35,100,13,147,500,80]
size = len(array)
for i in range(0,size):
for j in range(i+1,size):
min = array[j];
array[j] = array[i];
array[i] = min;
print(array)
(or)
i = 0
while i<len(a):
#smallest element in the sublist
smallest = min(a[i:])
#index of smallest element
index_of_smallest = a.index(smallest)
#swapping
a[i],a[index_of_smallest] = a[index_of_smallest],a[i]
i=i+1
print (a)
(OR)
1. def selectionSort(X):
2. for i in range(len(X)):
3. min_index = i
6. min_index = j
8. return X
11. print(sorted_X)
def insertion_sort(a):
j=i
j = j-1
if __name__ == '__main__':
insertion_sort(a)
print(a)
OR
1. def insertionSort(array):
2.
3. for step in range(1, len(array)):
4. key = array[step]
5. j = step - 1
6. while j >= 0 and key < array[j]:
7.
8. # For descending order, change key<array[j] to key>array[j].
9.
10. array[j + 1] = array[j]
11. j = j - 1
12. array[j + 1] = key
13.
14.
15. data = [9, 5, 1, 4, 3]
16. insertionSort(data)
17. print('Sorted Array in Ascending Order:')
18. print(data)
EX.NO.7 MERGE SORT
def mergesort(alist):
print("splitting",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergesort(lefthalf)
mergesort(righthalf)
i=0
j=0
k=0
alist[k] = lefthalf[i]
i=i+1
k=k+1
else:
alist[k] = righthalf[j]
j = j+1
k = k+1
while i <len(lefthalf):
alist[k] = lefthalf[i]
i = i+1
k = k+1
while j <len(righthalf):
alist[k] = righthalf[j]
j = j+1
k = k+1
print("merging",alist)
alist=[13,2,17,76,82,12,7]
mergesort (alist)
print (alist)
OUTPUT:
splitting [13, 2, 17, 76, 82, 12, 7]
splitting [13]
merging [13]
splitting [2]
merging [2]
splitting [17]
merging [17]
splitting [76]
merging [76]
splitting [82]
merging [82]
splitting [12, 7]
splitting [12]
merging [12]
splitting [7]
merging [7]
for i in range(2,num):
if num%i == 0:
j=num/i
break
else:
OUTPUT:
1 is a prime number
2 is a prime number
3 is a prime number
4 equals 2*2
5 is a prime number
6 equals 2*3
TO FIND THE FIRST n PRIME NUMBERS:
n=int(input(“enter a number r:”))
if n>1:
print(i,”presents time”,n//i,’is’,n)
break
else:
else:
OUTPUT:
Enter a no :5
5 is a prime number
Enter a number :8
2times 4 to 8