Table of Contents
Python program to subtraction of two numbers| 4 different Methods
In this tutorial, we will discuss the Python program to subtraction of two numbers
In this post, we are going to learn how to find subtraction of two numbers via different 4 ways in Python language
Subtraction of two numbers
Subtraction of two numbers – standard method
Program 1
num1=432 #variable declaration num2=234 sub=num1-num2 #calculation print('Subtraction of {0} and {1} is: {2}'.format(num1,num2,sub)) #display the result
When the above code is executed, it produces the following result
Subtraction of 432 and 234 is: 198
Approach
- Integer variable num1 and num2 both are declared and initialized
- the num1 and num2 both are subtracted together and the value assigned to the integer variable sub
- finally the program is displayed the output using print() function
Subtraction of two numbers – using user input
Program 2
#Subtraction of two numbers - using user input num1=input("Enter first number ") num2=input("Enter second number ") #get input from the user sub=num1-num2 #calculate subtraction print('Subtraction of {0} and {1} is: {2}'.format(num1,num2,sub)) #display the result
When the above code is executed, it produces the following result
Enter first number 2345 Enter second number 543 Subtraction of 2345 and 543 is: 1802
Approach
- Integer variable num1 and num2 both are declared,
- The program takes input from the user
- Then the user enters the input value for num1, num2
- the program will read the input and store the variables in num1, num2.
- the num1 and num2 both are subtracted together and the value assigned to the integer variable sub
- finally the program is displayed the output using print()
Subtraction of two numbers – using user defined function
Program 3
#Python program to subtract two numbers using function def subtract(a,b): #function definition sub=a-b return sub num1=int(input("Please enter the first number "))#input number to num1 from user num2=int(input("Please enter the second number "))#input number to num2 from user result=subtract(num1,num2)#calling the function print('Subtraction of the given numbers',result)#display the output
When the above code is executed, it produces the following result
Please enter the first number 4321 Please enter the second number 1234 ('Subtraction of the given numbers 3087)
Approach
- Integer variable num1 and num2 both are declared,
- The program takes input from the user
- Then the user enters the input value for num1, num2
- the program will read the input store the variables in num1, num2.
- define the function for subtract two numbers
- When we call the function, the num1 and num2 both are subtracted together
- Then the value is assigned to the integer variable result
- finally the program is displayed the output using print() function
Suggested post
Similar post
C program to subtraction of two numbers | 6 different Methods
C++ program to subtraction of two numbers | 6 different Methods
Java program to subtraction of two numbers | 5 different Methods