0% found this document useful (0 votes)
6 views12 pages

Practical Program 2 - Functions-1

The document contains a series of Python programming tasks, each accompanied by code examples. The tasks include functions for dictionary union, factorial calculation, Fibonacci series generation, random number generation, GCD & LCM calculation, series summation, area calculation for geometric shapes, and a menu-driven calculator. Each task is designed to demonstrate specific programming concepts and techniques.

Uploaded by

scar40290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Practical Program 2 - Functions-1

The document contains a series of Python programming tasks, each accompanied by code examples. The tasks include functions for dictionary union, factorial calculation, Fibonacci series generation, random number generation, GCD & LCM calculation, series summation, area calculation for geometric shapes, and a menu-driven calculator. Each task is designed to demonstrate specific programming concepts and techniques.

Uploaded by

scar40290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

FUNCTIONS

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:-

def addDict(dic1, dic2):


new_dic = {}
for i in dic1.keys():
new_dic[i] = dic1[i]
for i in dic2.keys():
new_dic[i] = dic2[i]
return new_dic
dic1 = {'a':1, 'b':2, 'c':3}
dic2 = {'c':4, 'd':5, 'e':6}
print(addDict(dic1, dic2))

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

x = int(input("Enter the Number"))

print("Factorial of ",x, "is :", factorial(x))

Sample Output:-
14. Write a program using a function that creates a Fibonacci series.

Code:-

#Fibonacci Series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …


Number = int(input("\nPlease Enter the Range Number: "))
# Initializing First and Second Values of a Series
First_Value = 0
Second_Value = 1
print(First_Value, " ", Second_Value, end=" ")
# Find & Displaying Fibonacci series
for Num in range(0, Number):
if(Num <= 1):
Next = Num
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next, end=" ")

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:-

def func(x, y):


d = (y-x)/3 # divides the difference in 3 equal parts
return [x, x+d, x+2*d, y] # returns the series as a list

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:-

from random import randint

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)

print('Random number of digit 3 is', ran(3))


print('Random number of digit 5 is', ran(5))

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:-
21. Write a Program in Python to create a menu driven calculator.
The menu should look like: Menu Driven Calculator

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)

print("1.Addition 2.Substraction 3.Multiplication 4.Divison


5.Modulus 6.Exit")
opt=int(input("Enter your option: "))
if opt==1:
add()
elif opt==2:
subs()
elif opt==3:
mul()
elif opt==4:
dev()
elif opt==5:
mod()
elif opt==6:
exit(0)

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(ch2)and i.startswith(ch)):
c=c+1
print("The no of words: ", c)

s = input("Enter the string ")


count_words(s)

Sample Output:-

You might also like