1.
Write a function calculation() such that it can accept two variables and
calculate the addition and subtraction of them. And also it must return both
addition and subtraction in a single return call
CODE:
def calculation(a,b):
c = int(a)+int(b)
d = int(a)-int(b)
return (c,d)
n1 = input("enter first number : ")
n2 = input("enter second number : ")
final = calculation(n1, n2)
print(final)
OUTPUT:
enter first number : 40
enter second number : 10
(50, 30)
2. Create a function showEmployee() in such a way that it should accept
employee name, and its salary and display both. If the salary is missing in the
function call assign default value 9000 to salary
CODE:
def showEmployee(nm,sl):
if len(sl)==0 :
print('name and salary : ',nm,9000)
else:
print('name and salary : ',nm,sl)
name = input("enter name : ")
salary = input("enter salary : ")
showEmployee(name,salary)
OUTPUT CASE 1:
enter name : Ben
enter salary : 9000
name and salary : Ben 9000
OUTPUT CASE 2:
enter name : Ben
enter salary :
name and salary : Ben 9000
3. Guess the output of the following code snippet -
def swap(x, y):
temp = x
x=y
y = temp
# Driver code
x=2
y=3
swap(x, y)
print(x)
print(y)
OUTPUT :
2
3
4. Write python function code - Area calculator
CODE:
def rectanglearea(a,b):
x = int(a)*int(b)
return (x)
def trianglearea(c,d):
y = (1/2)*int(c)*int(d)
return (y)
def squarearea(e):
z = int(e)*int(e)
return (z)
choice = input('enter 1 for rectangle, 2 for triangle, 3 for square : ')
if choice == '1':
n1 = input("enter length : ")
n2 = input("enter breath : ")
f1 = rectanglearea(n1, n2)
print('rectangle area : ',f1)
if choice == '2':
n3 = input("enter height : ")
n4 = input("enter base : ")
f2 = trianglearea(n3,n4)
print('triangle area : ',f2)
if choice == '3':
n5 = input("enter side : ")
f3 = squarearea(n5)
print('square area : ',f3)
OUTPUT CASE 1 :
enter 1 for rectangle, 2 for triangle, 3 for square : 1
enter length : 20
enter breath : 30
rectangle area : 600
OUTPUT CASE 2:
enter 1 for rectangle, 2 for triangle, 3 for square : 2
enter height : 50
enter base : 20
triangle area : 500.0
OUTPUT CASE 3:
enter 1 for rectangle, 2 for triangle, 3 for square : 3
enter side : 10
square area : 100
5. Write python function code - Palindrome
CODE:
def stringpaline(string):
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("Not a palindrome")
a = input ('Enter a string : ')
stringpaline(a)
OUTPUT CASE 1:
Enter a string or number : aka
The string is a palindrome
OUTPUT CASE 2:
Enter a string : cap
Not a palindrome
6. Python program for Towers of Hanoi
CODE :
def TowerOfHanoi(n , from_rod, to_rod, aux_rod):
if n == 1:
print("Move disk 1 from rod",from_rod,"to rod",to_rod)
return
TowerOfHanoi(n-1, from_rod, aux_rod, to_rod)
print("Move disk",n,"from rod",from_rod,"to rod",to_rod)
TowerOfHanoi(n-1, aux_rod, to_rod, from_rod)
n = input('enter the no. of disks : ')
n = int(n)
TowerOfHanoi(n, 'A', 'C', 'B')
OUTPUT :
enter the no. of disks : 4
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
7. Assign a different name to function and call it through the new name
CODE :
def display(name, age):
print(name, age)
display("Kishan", 21)
show = display
show("Kishan", 21)
OUTPUT :
Kishan 21
Kishan 21