ADDS Assignment Python Programs
By: Pragadeesh S K
BFT-2
[Link] Program:
def factorial(a):
factorial = 1
if a < 0:
print(" Factorial does not exist for negative numbers")
elif a == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,a + 1):
factorial = factorial*i
print(factorial)
02. Prime Number:
def prime(a):
n=0
for i in range(a):
if i==0:
print("")
elif a%i==0:
n=n+1
if n>=2:
print("it is not a prime number")
else:
1
print("it is a prime number")
03. Basic Calculator:
def add(a,b):
result=a+b
print(result)
def sub(a,b):
result=a-b
print(result)
def mul(a,b):
result=a*b
print(result)
def div(a,b):
result=a/b
print(result)
a=int(input("Enter the number 1: "))
b=int(input("Enter the number 2: "))
add(a,b)
sub(a,b)
mul(a,b)
div(a,b)
04. Matrix Addition:
def matrixadd():
l1=[]
l2=[]
for i in range(2):
2
line1=input("enter the number in first row:")
line2=input("enter the number in second row:")
line11=int(line1)
line22=int(line2)
[Link](line11)
[Link](line22)
print("the first Matrix is:")
print(l1)
print(l2)
q1=[]
q2=[]
for i in range(2):
lin1=input("enter the number in first row:")
lin2=input("enter the number in second row:")
line111=int(lin1)
line222=int(lin2)
[Link](line111)
[Link](line222)
print("the second Matrix is:")
print(q1)
print(q2)
final_11=l1[0]+q1[0]
final_12=l1[1]+q1[1]
final_21=l2[0]+q2[0]
final_22=l2[1]+q2[1]
print("the final matrix after addition is:")
print("[",final_11,final_12,"]")
3
print("[",final_21,final_22,"]")
05. Matrix Multiplication:
def matrixmul():
l1=[]
l2=[]
for i in range(2):
line1=input("enter the number in first row:")
line2=input("enter the number in second row:")
line11=int(line1)
line22=int(line2)
[Link](line11)
[Link](line22)
print("the first Matrix is:")
print(l1)
print(l2)
q1=[]
q2=[]
for i in range(2):
lin1=input("enter the number in first row:")
lin2=input("enter the number in second row:")
line111=int(lin1)
line222=int(lin2)
[Link](line111)
[Link](line222)
print("the second Matrix is:")
print(q1)
4
print(q2)
f11=(l1[0]*q1[0])+(l1[1]*q2[0])
f12=(l1[0]*q1[1])+(l1[1]*q2[1])
f21=(l2[0]*q1[0])+(l2[1]*q2[0])
f22=(l2[0]*q1[1])+(l2[1]*q2[1])
print("The final matrix after multipilication is:")
print("[",f11,f12,"]")
print("[",f21,f22,"]")
06. Random Password Generator:
import random
import string
fname=input("Enter the first name:")
lname=input("Enter the last name:")
reaction="Y"
while reaction=="Y":
length = 1
characters = string.ascii_uppercase
letter1 = ''.join([Link](characters, k=length))
spl_chr=("!","@","#","$","%","^","&")
letter2 = [Link](spl_chr)
length = 1
characters = string.ascii_uppercase
l3 = ''.join([Link](characters, k=length))
letter3 = [Link]()
letter4=str([Link](0,9))
letter5=str([Link](0,9))
5
letter6 = [Link](string.ascii_letters)
letter7 = [Link](string.ascii_letters)
CHOICE=(str(fname[0]),str(lname[-1]))
letter8=[Link](CHOICE)
reaction="Y"
print(letter1+letter2+letter3+letter4+letter5+letter6+letter7+letter8)
reaction=input("do you want to get one more password?(Y/N)")
07. Check the number of count of a number in list:
import random as r
import math
result="Y"
while result=="Y":
L1=[]
for i in range(10):
n=[Link](1,20)
[Link](n)
for a in L1:
c=[Link](a)
if c>=2:
[Link](a)
else:
continue
print("The original List is:",L1)
L2=[]
while len(L2)<=2:
num=input("Enter the number you want:")
6
n=int(num)
for i in L1:
if i==n:
print("The number entered by you is in the list")
fact=[Link](i)
[Link](fact)
print("")
print("The factorials are:",L2)
print("")
L3=[]
for i in L2:
[Link](str(i)[::-1])
print("Reversed Number: " , L3[::-1])
result=input("Do you want to do the program again(Y/N):")
08. Palindrome Checker:
def is_palindrome(word):
return word == word[::-1]
word = input("Enter a word: ")
if is_palindrome(word):
print("Yes, it's a palindrome!")
else:
print("No, it's not a palindrome.")
09. Addition of List:
import random as r
L1=[]
7
L2=[]
for i1 in range(10):
n1=[Link](1,50)
[Link](n1)
print("The original List 1 is:",L1)
for i2 in range(10):
n2=[Link](1,50)
[Link](n2)
print("The original List 2 is:",L2)
L3=L1+L2
print("The List1 + List 2 is:",L2)
for a in L3:
c=[Link](a)
if c>1:
[Link](a)
[Link]()
print("The combined list is:",L3)
10. Pythagorean Theorem:
def hyp_of_right_tri(adj,opp):
import math
hyp=[Link]((adj*adj)+(opp*opp))
print(hyp)
def adj_of_right_tri(hyp,opp):
import math
adj=[Link]((hyp*hyp)-(opp*opp))
print(adj)