Functions
Practical 1: function without arguments:
def smsprint():
print("Welcome to python function")
print("++++++++++++++++++++++++++")
print("This is demo program")
print("end of Programs")
Practical 2: function with arguments:
def welcome(name):
print("Hello",name)
Practical 3: function without arguments:
def is_even():
x=int(input("Enter number"))
if (x%2==0):
print("Entered number is Even")
else:
print("Entered number is Odd")
Practical 4: function with arguments:
def is_even(num):
if (num%2==0):
print("Entered number is Even")
else:
print("Entered number is Odd")
Practical 5: function with arguments:
def calc(a,b):
add=a+b
sub=a-b
div=a/b
mul=a*b
print("Addition:",add)
print("subtraction:",sub)
print("division:",div)
print("Multiplication:",mul)
Practical 6: function with arguments with single return:
def sum(a,b):
c=a+b
return c
result=sum(10,50)
print("Sum:",result)
1
print("Sum:",sum(10,10))
Practical 7: function with arguments with multiple returns:
def calc(x,y):
a=x+y
b=x-y
c=x*y
d=x/y
return a,b,c,d
add,sub,mul,div=calc(20,10)
print("Sum:",add)
print("Subtraction:",sub)
print("multiplication:",mul)
print("Division:",div)
Practical 8: function with default arguments and with multiple returns:
def calc(x,y=20):
a=x+y
b=x-y
c=x*y
d=x/y
return a,b,c,d
add,sub,mul,div=calc(20)
print("Sum:",add)
print("Subtraction:",sub)
print("multiplication:",mul)
print("Division:",div)
Practical 8: inbuild date functions:
import datetime
dt=datetime.datetime.now()
print("Today's date:",dt)
print("Today's date:",dt.date())
days=["Mon","Tuesday","Wednesday"]
print("Today's weekday:",dt.weekday())
print("Today's weekday:",days[dt.weekday()])
print("Time:",dt.time())
Practical 9: inbuild math and random functions:
import random
import math
print(eval('45*6'))
2
print(eval('(45*6)-(12/3)'))
print(max(45,23,41,89,45,23,))
print(max('Apple','Bombay','Mumbai'))
print(min(45,23,41,89,45,23,))
print(min('Apple','Bombay','Mumbai'))
print(pow(2,3))
print(round(3.1416))
print(round(3.1456,2))
print(int('12')+int('12'))
print(round(random.random()*10))
print(random.choice([1,2,3,4,5]))
print(random.randrange(1,6,3))
print(math.ceil(12.5))
print(math.floor(12.5))
print(math.sqrt(36))