Python Functions | Functions with arguments and returning value

Question:4
Python script to calculate and print square and cube of a number(Multiple functions)
Sol:

#function definition
def square(n):
    s=n*n
    return(s)

def cube(n):
    s=n*n*n
    return(s)


#function calling
a=int(input("Enter any no "))
b=square(a)
c=cube(a)
print("Square = ",b," Cube = ",c)

Output:

Enter any no 5
Square = 25 Cube = 125
>>>

Question:5
Python script to calculate and print sum , product difference and average of two numbers (Multple function)
Sol:

#function definition
def sum(n1,n2):
    s=n1+n2
    return(s)

def prod(n1,n2):
    p=n1*n2
    return(p)

def diff(n1,n2):
    if(n1>n2):
        return(n1-n2)
    else:
        return(n2-n1);

def average(n1,n2):
    av=(n1+n2)/2
    return(av)


#function calling
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
s=sum(a,b)
p=prod(a,b)
d=diff(a,b)
av=average(a,b)
print("Sum = ",s)
print("Product = ",p)
print("Difference = ",d)
print("average = ",av)

Output:

Enter 1st no 10
Enter 2nd no 20
Sum = 30
Product = 200
Difference = 10
average = 15.0
>>>

Visitor Counter

0051148