Phython
Phython
Phython
Sol : a = 2
b=4
maximum = max(a, b)
print(maximum)
if(sorted(s1)==sorted(s2)):
else:
def binary_to_gray(n):
n ^= (n >> 1)
return bin(n)[2:]
b = binary_to_gray(g)
print('Gray codeword:', b)
def bisection(a,b):
return
c=a
c = (a+b)/2
if (func(c) == 0.0):
break
else:
a=c
# Driver code
a =-200
b = 300
bisection(a, b)
if select == 1:
print(number_1, "+", number_2, "=",
add(number_1, number_2))
elif select == 2:
print(number_1, "-", number_2, "=",
subtract(number_1, number_2))
elif select == 3:
print(number_1, "*", number_2, "=",
multiply(number_1, number_2))
elif select == 4:
print(number_1, "/", number_2, "=",
divide(number_1, number_2))
else:
print("Invalid input")
print("D is : ", D)
print("D1 is : ", D1)
print("D2 is : ", D2)
print("D3 is : ", D3)
# Case 1
if (D != 0):
print("Value of x is : ", x)
print("Value of y is : ", y)
print("Value of z is : ", z)
# Case 2
else:
if (D1 == 0 and D2 == 0 and
D3 == 0):
print("Infinite solutions")
elif (D1 != 0 or D2 != 0 or
D3 != 0):
print("No solutions")
# Driver Code
if __name__ == "__main__":
findSolution(coeff)
# initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# printing result
print("The constructed dictionary list : " + str(res))
for x in range(1,n+1):
d[x]=x*x
print(d)
10. Write program to read 2X2 matrix and find its covariance matrix. Eigen
values and Eigen vectors of covariance matrix. Discuss what do you mean by
eigen values and covariance matrix
import numpy as np
m = np.mat("3 -2;1 0")
print("Original matrix:")
print("a\n", m)
w, v = np.linalg.eig(m)
print( "Eigenvalues of the said matrix",w)
print( "Eigenvectors of the said matrix",v)
11.Write a Python Program to Compute the Value of Euler's Number e. Use the
Formula:
e = 1 + 1/1! + 1/2! + …… 1/n!
import math
n=int(input("Enter the number of terms: "))
sum1=1
for i in range(1,n+1):
sum1=sum1+(1/math.factorial(i))
print("The sum of series is",round(sum1,2))
test_string=input("Enter string:")
l=[]
l=test_string.split()
wordfreq=[l.count(p) for p in l]
print(dict(zip(l,wordfreq)))
12.Write a python program to find the H.C.F of two input number using loops
def is_empty(self):
return self.q.is_empty()
def pop(self):
for _ in range(self.q.get_size() - 1):
dequeued = self.q.dequeue()
self.q.enqueue(dequeued)
return self.q.dequeue()
class Queue:
def __init__(self):
self.items = []
self.size = 0
def is_empty(self):
return self.items == []
def dequeue(self):
self.size -= 1
return self.items.pop(0)
def get_size(self):
return self.size
s = Stack()
print('Menu')
print('push <value>')
print('pop')
print('quit')
while True:
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
s.push(int(do[1]))
elif operation == 'pop':
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elif operation == 'quit':
break
def CheckLeap(Year):
# Checking if the given year is leap year
if((Year % 400 == 0) or
(Year % 100 != 0) and
(Year % 4 == 0)):
print("Given Year is a leap Year");
# Else it is not a leap year
else:
print ("Given Year is not a leap Year")
# Taking an input year from user
Year = int(input("Enter the number: "))
# Printing result
CheckLeap(Year)
17.Write a Python Program to Read a List of Words and Return the Length of
the Longest One
a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=input("Enter element" + str(x+1) + ":")
a.append(element)
max1=len(a[0])
temp=a[0]
for i in a:
if(len(i)>max1):
max1=len(i)
temp=i
print("The word with the longest length is:")
print(temp)
import numpy as np
matrix1 = []
print("Enter the entries of first matrix rowwise:")
matrix2 = []
arr2 = np.array(matrix2)
print('----------First Matrix---------------')
else:
reduce = True
if reduce:
# self.Display(Matrix, self.R,self.C)
return (rank)
# Driver Code
Matrix = matrix1
RankMatrix = rankMatrix(Matrix)
print ("Rank of the Matrix is:",
(RankMatrix.rankOfMatrix(Matrix)))
Matrix = matrix2
RankMatrix = rankMatrix(Matrix)
print ("Rank of the Matrix is:",
(RankMatrix.rankOfMatrix(Matrix)))
a=2
b=4
maximum = max(a, b)
print(maximum)
20. Write a python program for Matrix Multiplication Using Nested List
Comprehension and using Using Nested Loop
def Multiply(X,Y):
result=[ [0,0,0],[0,0,0],[0,0,0] ]
#list comprehension
result= [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*Y)] for
X_row in X]
for k in result:
print(k)
return 0
print("Result: ")
Multiply(A,B)
21. Write a Python Program that Reads a Text File and Counts the Number of
Times a Certain Letter Appears in the Text File
num_words = 0
def is_empty(self):
return self.items == []
def pop(self):
return self.items.pop()
s = Stack()
text = input('Please enter the string: ')
reversed_text = ''
while not s.is_empty():
reversed_text = reversed_text + s.pop()
if text == reversed_text:
print('The string is a palindrome.')
else:
print('The string is not a palindrome.')
num =10
# Iterate from 2 to n / 2
for i in range(2, int(num/2)+1):
def speed_check(speed):
if speed <= 60:
return "OK"
else:
speed1 = (speed-60)/5
else:
return ("License suspended")
def even_odd_recognize(limit):
for i in range(0,limit+1):
if(i%2==0):
print(i,' EVEN')
else:
print(i,' ODD')
28. Write a function that returns the sum of multiples of 3 and 5 between 0
and limit (parameter). For example, if limit is 20, it should return the sum of 3,
5, 6, 9, 10, 12, 15, 18, 20.
total_sum = 0
for i in range(21):
if (i%3 == 0 or i%5 == 0):
total_sum = total_sum+i
print (total_sum)
29. Write a Python Program to Read a Number n And Print the Series "1+2+
…..+n= "
n = int (n)
sum = 0
sum = sum+num
average = sum / n