Current Page :2
Page 1 Page 2 Page 3
Example of a user-defined function
Functions without arguments
Example: 1
Write a python script using a function to calculate and print square of a number
#function definition def square(): a=int(input("Enter any no ")) b=a*a print("square = ",b) #function calling square()
Output:
Enter any no 5
square = 25
>>>
Example: 2
Write a python script using a function to calculate and print cube of a number
#function definition def cube(): a=int(input("Enter any no ")) b=aaa print("cube = ",b) #function calling cube()
Output:
Enter any no 5
cube = 125
>>>
Example:3
Write a python script using a function to calculate and print sum of two numbers?
#function definition def sum(): a=int(input("Enter 1st no ")) b=int(input("Enter 2nd no ")) c=a+b print("Sum = ",c) #function calling sum()
Output:
Enter 1st no 10
Enter 2nd no 20
Sum = 30
>>>
Example:4
Write a python script using function to take input for three numbers check and print the largest number?
#function definition def largest(): a=int(input("Enter 1st no ")) b=int(input("Enter 2nd no ")) c=int(input("Enter 3rd no ")) if(a>b and a>c): m=a elif(b>c): m=b else: m=c print("Largest number = ",m) #function calling largest()
Output:
Enter 1st no 25
Enter 2nd no 96
Enter 3rd no 35
Largest number = 96
>>>
Example:5
Write a python script using function to take input for two numbers calculate and print their sum, product and difference?
#function definition def cal(): a=int(input("Enter 1st no ")) b=int(input("Enter 2nd no ")) s=a+b p=a*b if(a>b): d=a-b else: d=b-a print("Sum = ",s," Product = ",p," diff = ",d) #function calling cal()
Output:
Enter 1st no 10
Enter 2nd no 20
Sum = 30 Product = 200 diff = 10
>>>