How To Subtract Two Numbers In Python? {subtraction in python } [python code]

Example:1

# Subtract code
number1 = 50
number2 = 10
number = number1 - number2
print(number)

Output
40

Example:2

# example code
def sub(num1,num2):
    return(num1-num2)
print(sub(50,25))

Output
25

Example:3

a = int(input("Enter 1st number:"))
b = int(input("Enter 2nd number:"))
c = a-b
print("The result:",c)

Output
Enter 1st number:
Enter 2nd number:
The result:

Example:4

first_operand = int(input("Enter the first Number: "))
second_operand = int(input("Enter the second Number: "))
third_operand = int(input("Enter the third number: "))

output = third_operand - second_operand - first_operand

print(output)

Output
Enter the first Number:
Enter the second Number:
Enter the third number:

Example:5

first_operand = float(input("Enter the first Number: "))
second_operand = float(input("Enter the second Number: "))
third_operand = float(input("Enter the third Number: "))

output = third_operand - second_operand - first_operand

print(output)

Output
Enter the first Number:
Enter the second Number:
Enter the third number:

Example:6 (subtract the complex numbers in Python)

first_operand = 11 + 2j
second_operand = 22 + 3j
third_operand = 44 + 4j

output = third_operand - second_operand - first_operand

print(output)

Output
(11 – 1j)

Example:7

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# subtract two numbers
sub = num1 - num2

# print the subtraction result

print('The subtraction of numbers = %0.2f' %sub)

Output
Enter first number: 10
Enter second number: 2
The subtraction of numbers = 8.00

Example:8

#importing numpy.subtract() function
import numpy

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# subtract two numbers
sub = numpy.subtract(num1, num2)

# print the subtraction result

print('The subtraction of numbers = %0.2f' %sub)

Output
Enter first number: 20
Enter second number: 5
The subtraction of numbers = 15.00

Example:9

# Python program to subtract two numbers

def subtract_num(a,b):   #user-defined function
   if (b == 0):
      return a
   return subtract_num(a ^ b, (~a & b) << 1)

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calling function
subtract = subtract_num(num1, num2)

# print subtract of numbers
print('The subtract of numbers {0} and {1} is {2}'
                       .format(num1, num2, subtract))

Output
Enter first number: 120
Enter second number: 20
The subtract of numbers 120 and 20 is 100

Example:10

//Addition and Subtraction of two numbers
a = int(input("Enter first number: "))
b = int( input("Enter second number: "))
Sum = a+b; #Add two numbers
Difference = a-b; #Subtract two numbers
# To print the result
print("Addition of two numbers = ",Sum)
print("Subtraction of two numbers = ",Difference)

Output
Enter first number: 10
Enter second number: 5
Addition of two numbers =15
Subtraction of two numbers =5

Example:11

# Python program to perform Addition Subtraction Multiplication

num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))

print("Enter which operation would you like to perform?")
ch = input("Enter any of these char for specific operation +,-,*,/: ")

result = 0
if ch == '+':
    result = num1 + num2
elif ch == '-':
    result = num1 - num2
elif ch == '*':
    result = num1 * num2
elif ch == '/':
    result = num1 / num2
else:
    print("Input character is not recognized!")

print(num1, ch , num2, ":", result)

Output
Enter First Number: 10
Enter Second Number: 5
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: +
10 + 5 : 15

Keywords:

How To Subtract Two Numbers In Python? {subtraction in python } [python code]
python subtract two lists,
subtraction in python list,
python program to subtract two numbers using function,
how to subtract variables in python,
how to subtract multiple numbers in python,
python program to divide two numbers,
addition and subtraction of two numbers in python,
python program for multiplication of two numbers,
How do you subtract 2 numbers in Python?,
How do you subtract something in Python?,

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top