Built-in and user defined functions in Python

# Built-in functions in Python [importing math package]
import math
x=int(input(“Enter any number:”))
print(“Square root of {0} is {1}”.format(x,math.sqrt(x)))
print(“Square of {0} is {1}”.format(x, x*x))
print(“Cube of {0} is {1}”.format(x, x*x*x))

OutPut:

Enter any number:5
Square root of 5 is 2.23606797749979
Square of 5 is 25
Cube of 5 is 125

 

User-defined functions 

def my_function():
print(“Hello from a function”)

def disp():
print(“this is disp function”)

my_function()
disp()

OUTPUT:

Hello from a function
this is disp function

# using Functions in Python
def fun_disp():
print(“Inside the user defined function”)

fun_disp()

OUTPUT:

Inside the user-defined function

 

# Functions in Python
def per_det():
name=input(“Enter Name:”)
age=int(input(“Enter age:”))
print(“Name=”,name)
print(“Age=”,age)

def aca_det():
edu=input(“Enter educational qualification:”)
exp=input(“Experience, if any:”)
print(“Educational Qualification=”, edu)
print(“Experience=”, exp)

per_det()
aca_det()

OUTPUT:

Enter Name:Ganesh
Enter age:21
Name= Ganesh
Age= 21
Enter educational qualification:PhD
Experience, if any:1
Educational Qualification= PhD
Experience= 1

# Program to check number is even or odd using functions in python
def eve_odd(x):
if (x%2==0):
print(“Number is even!”)
else:
print(“Number is odd!”)
eve_odd(2)

OUTPUT:

Number is even!

# Program using functions in python
def swap():
a=int(input(“Enter Num1:”))
b=int(input(“Enter Num2:”))
c=a ;
a=b;
b=c;
print(a)
print(b)

# Call the function here
swap()

OUTPUT:

Enter Num1:4
Enter Num2:5
5
4

Share
Share
Scroll to Top