Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Python program to subtraction of two numbers | 4 different Methods

Python program to subtraction of two numbers | 4 different Methods

Posted on August 9, 2020

Table of Contents

  • Python program to subtraction of two numbers| 4 different Methods
    • Subtraction of two numbers
      • Subtraction of two numbers – standard method
      • Subtraction of two numbers – using user input
      • Subtraction of two numbers – using user defined function

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

Function in Python language

Operator in Python language

Data type in Python

 

 

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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes