DOC-20240425-WA0029
DOC-20240425-WA0029
DOC-20240425-WA0029
12. Write a function called addDict(dict1, dict2) which computes the union of two
dictionaries. It should return a new dictionary, with all the items in both its
arguments (assumed to be dictionaries). If the same key appears in both
arguments, feel free to pick a value from either.
Code:-
Sample Output:-
13. Write a program using a function to find the factorial of a given number.
Code:-
def factorial(n):
i=0
fact=1
while(i<n):
i=i+1
fact=fact*i
return fact
Sample Output:-
Code:-
Sample Output:-
15. Write a program that generates a series using a function which takes first and
last values of the series and then generates four terms that are equidistant e.g., if
two numbers passed are 1 and 7 then function returns 1 3 5 7.
Code:-
print(func(1, 9))
print(func(4, 8))
print(func(4, 10))
print(func(1, 7))
Sample Output:-
16. Write a function that takes a number n and then returns a randomly generated
number having exactly n digits (not starting with zero) e.g., if n is 2 then function
can randomly return a number 10-99 but 07, 02 etc. are not valid two digit
numbers.
Code:-
def ran(n):
x = int('9'*(n-1))+1 # first n-digit number, string is
# converted back to int
y = int('9'*n) # last n-digit number
return randint(x, y)
Sample Output:-
17. Write a Program in Python to find the GCD & LCM of two given numbers using
function.
Code:-
def gcd(x,y):
if(y==0):
return x
else:
r=x%y
return(gcd(y,r))
def lcm(x,y):
p=x*y
l=p/gcd(x,y)
return l
x=int(input("Enter the larger No.: "))
y=int(input("Enter the smaller No.: "))
print("GCD is",gcd(x,y))
print("LCM is",lcm(x,y))
Sample Output:-
18. Write a Program in Python to find the sum of the series using Function.
S=1 + 2 + 3 + 4 + 5 + . . .
Code:-
def series(n):
s=0
i=1
while i<n+1:
if i<n:
print(i,end="+")
if i==n:
print(i,end="")
s=s+i
i+=1
print(" = ",s)
n=int(input("enter the value of n: "))
series(n)
Sample Output:-
19. Write a Program in Python to find the sum of the series using Function.
S=1 + 1/4 + 1/9 + 1/16 + 1/25 . . .
Code:-
def series(n):
s=0
f=1
for i in range (1,n+1,1):
if i<n:
print("1/",i**2,end=" + ")
if i==n:
print("1/",i**2,end=" ")
s=s+(1/(i**2))
print("= ",s)
n=int(input("enter the value of n: "))
series(n)
Sample Output:-
20. Write a Menu driven Program in Python to calculate the area of a triangle,
rectangle or a circle using function depending on the choice entered by the user.
1. Triangle
2. Rectangle
3. Circle
4. Exit
Code:-
def triangle():
b=int(input("Enter the base: "))
h=int(input("Enter the height: "))
a=0.5*b*h
print("The area of triangle is :",a)
def rectangle():
b=int(input("Enter the breadth: "))
l=int(input("Enter the length: "))
a=b*l
print("The area of rectangle is :",a)
def circle(r):
a=3.14*r*r
print("The area of circle is :",a)
print("1.triangle 2.rectangle 3.circle 4.exit")
opt=int(input("Enter your option: "))
if opt==1:
triangle()
elif opt==2:
rectangle()
elif opt==3:
r=int(input("Enter the radius: "))
circle(r)
elif opt==4:
exit(1)
Sample Output:-
Code:-
def add():
b=int(input("Enter the first number: "))
h=int(input("Enter the second number: "))
a=b+h
print("The sum is :",a)
def subs():
b=int(input("Enter the larger no. : "))
l=int(input("Enter the smaller no.: "))
a=b-l
print("The result is :",a)
def mul():
r1=int(input("Enter the 1st no.: "))
r2=int(input("Enter the 2nd no.: "))
a=r1*r2
print("The product is :",a)
def dev():
r1=int(input("Enter the divident: "))
r2=int(input("Enter the divisor: "))
a=r1/r2
print("The quotient is :",a)
def mod():
r1=int(input("Enter the divident: "))
r2=int(input("Enter the divisor: "))
a=r1%r2
print("The remainder is :",a)
Sample Output:-
22. Write a Program in Python to Count the no. of words starts and
ends with a specified character using a function.
Code:-
def count_words(s):
ch=(input("enter the starting character "))
ch2=(input("enter the ending character "))
c=0
l=s.split()
for i in l:
if(i.endswith(ch) and i.startswith(ch2)):
c=c+1
print("The no of words: ", c)
Sample Output:-