Functions in Python

Current Page :3

Page 1    Page 2    Page 3  

Functions with arguments and not returning any value

Example:1

Write a python script (using functions) to take for two numbers calculate and print their sum?

#function definition
 def sum(n1,n2):
     s=n1+n2
     print("Sum = ",s)
 

#function calling
 a=int(input("Enter 1st no "))
 b=int(input("Enter 2nd no "))
 sum(a,b)

Output:

Enter 1st no 25
Enter 2nd no 63
Sum = 88
>>>

Example:2

Write a python script (using functions) to take for three numbers calculate and print their product?

#function definition
 def prod(n1,n2,n3):
     p=n1n2n3
     print("Product = ",p)
 

#function calling
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
c=int(input("Enter 2nd no "))
prod(a,b,c)

Output:

Enter 1st no 2
Enter 2nd no 6
Enter 2nd no 3
Product = 36
>>>

Example:3

Write a python script (using functions) to take for a number calculate and print its square and cube?

#function definition
 def cal(n):
     s=nn     c=nn*n
     print("square = ",s)
     print("cube = ",c)
 #function calling
 a=int(input("Enter any no "))
 cal(a)

Output:

Enter any no 5
square = 25
cube = 125
>>>

Example:4 

 

function with arguments and returning value

Question:
Write a python script using function to calculate and print sum of two numbers?

#function definition
def sum(p,q):
       s=p+q
       return(s)

#function calling
n1=int(input(“Enter 1st no “))
n2=int(input(“Enter 2nd no “))
c=sum(n1,n2)
print(“sum = “,c)
#or
print(“sum = “,sum(n1,n2))

output:

Enter 1st no 25
Enter 2nd no 63
sum = 88
sum = 88
>>>

 

Question:
Write a python script using function to calculate and print square of a number?
#function definition
def square(n):
s=n*n
return(s)

#function calling
a=int(input(“Enter any no “))
b=square(a)
print(“Square = “,b)

output:

Enter any no 5
Square = 25
>>>

Current Page :3

Page 1    Page 2    Page 3