Python Lab Programs 1. To Write A Python Program To Find GCD of Two Numbers
Python Lab Programs 1. To Write A Python Program To Find GCD of Two Numbers
Python Lab Programs 1. To Write A Python Program To Find GCD of Two Numbers
i=1
gcd = i
i=i+1
a = float(number)
for i in range(number_iters):
number = 0.5 * (number + a / number)
return number
exponent = 9
print "Exponential Value is: ", base ** exponent
4. To write a Python Program to find the maximum from a list of
numbers.
list1 = [9,1,32,76,2,65,12]
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = ['p','u','s,'o','l','i','n','m']
x = 'a'
start = 0
end = len(alist)
start = mid + 1
else:
return mid
return -1
alist = alist.split()
if index < 0:
else:
for i in range(len(A)):
min_idx = i
for i in range(len(A)):
print("%d" %A[i]),
key = arr[i]
j = i-1
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
insertionSort(arr)
for i in range(len(arr)):
n1 = m - l + 1
n2 = r- m
L = [0] * (n1)
R = [0] * (n2)
L[i] = arr[l + i]
R[j] = arr[m + 1 + j]
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
arr[k] = L[i]
i += 1
k += 1
arr[k] = R[j]
j += 1
k += 1
def mergeSort(arr,l,r):
if l < r:
m = (l+(r-1))//2
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
n = len(arr)
for i in range(n):
mergeSort(arr,0,n-1)
for i in range(n):
i, j, flag = 0, 0, 0;
if (i == 1 or i == 0):
continue;
flag = 1;
if (i % j == 0):
flag = 0;
break;
if (flag == 1):
print(i);
N = 100;
print_primes_till_N(N);
[4, 5, 6],
[7, 8, 9]]
B = [[5, 8, 1],
[6, 7, 3],
[4, 5, 9]]
[0, 0, 0],
[0, 0, 0]]
# iterating by row of A
for i in range(len(A)):
# iterating by coloum by B
for j in range(len(B[0])):
# iterating by rows of B
for k in range(len(B)):
for r in result:
print(r)
# python3 filename.py 1 2 3 4
import sys
# total arguments
n = len(sys.argv)
print("Total arguments passed:", n)
# Arguments passed
print("\nName of Python script:", sys.argv[0])
print("\nArguments passed:", end = " ")
for i in range(1, n):
print(sys.argv[i], end = " ")
Sum = 0
for i in range(1, n):
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)
# Result=10
13. To write a Python program to find the most frequent words in a text
read from a file.
fname=input("enter file name: ")
count=0 #count of a specific word
maxcount=0 #maximum among the count of each words
l=[] #list to store the words with maximum count
with open(fname,'r') as f:
contents=f.read()
words=contents.split()
for i in range(len(words)):
for j in range(i,len(words)):
if(words[i]==words[j]): #finding count of each word
count+=1
if(count==maxcount): #comparing with maximum count
l.append(words[i])
elif(count>maxcount): #if count greater than maxcount
l.clear()
l.append(words[i])
maxcount=count
else:
l=l
count=0
print(l)
14. Reshaping 5*10 array into a 2*10 array
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5],[7,6,5,8],[2,4,6,1,],[7,3,9,6]])
print(a)
new_a=a.reshape(2,10)
print("new array:-")
print(new_a)
a=np.array([[1,2,3],[2,3,4],[7,6,5],[2,4,6,],[7,3,9]])
print(a)
new_a=a.flatten()
print("new array:-")
print(new_a)
a=np.array([[1,2,3,4,5,6,7,8],[2,3,4,4,5,6,7,8],[7,6,5,4,5,6,7,8],[2,4,6,4,5,6,7,8],[7,3,9,4,5,6,7,8],[
7,3,9,4,5,6,7,8]])
print(a)
new_a=np.split(a,2)
print("new array:-")
print(new_a)
17. Sorting dataframe in ascending
import pandas as pd
df = pd.DataFrame({
})
print(df)
df.sort_values(by=['col1'])
print(df)
df = pd.DataFrame({
})
print(df.max())
19. Finding min value in a dataframe
import pandas as pd
df = pd.DataFrame({
})
print(df.min())