# How to Subtract in Python?

How to Subtract in Python? is a guide that will teach you the different ways to subtract in Python. You will learn how to subtract integers, floats, and decimals.

Checkout this video:

## Introduction

Python is a programming language with many features, one of which is the ability to perform arithmetic operations. In this article, we will walk you through how to subtract numbers in Python.

Subtracting numbers in Python is a simple process. To subtract two numbers, you use the minus sign (-). For example, if you wanted to subtract 4 from 10, you would write: 10-4. The answer, of course, would be 6.

If you want to subtract more than two numbers, you can chain the subtraction operations together. For example, if you wanted to subtract 4 from 10 and then 2 from 6, you could write: 10-4-2. The answer to this operation would be 0.

It is important to note that Python follows the order of operations when performing arithmetic operations. This means that if you have an operation like 10-4*2, Python will first subtract 4 from 10 and then multiply 2 by that difference. The answer to this operation would be 2.

While in most cases it is not necessary to use parentheses to force Python to follow the order of operations that you want, there may be some cases where using parentheses can make your code easier to read or understand. For example, the following two lines of code are equivalent:
10-4*2 # answer is 2
(10-4)*2 # answer is 8
The first line of code follows the standard order of operations while the second line uses parentheses to force Python to multiply 2 by the difference of 10 and 4 first and then return that result.

## Basic Syntax

The basic syntax for subtracting two numbers in Python is:

result = operand1 – operand2
For example, if you wanted to calculate the result of 3 – 2, it would look like this:

result = 3 – 2
Of course, you can also use variables in your calculations. For example:

x = 3
y = 2
result = x – y

## Operators

Python considers the minus (-) operator to be a subtraction operator. This is demonstrated in the following example:
“`
>>> 2 – 3
-1
“`

## Variables

In programming, a variable is a value that can change, depending on conditions or on information passed to the program.

You can use variables to store data, like numbers or strings. You can also use variables to hold the result of a calculation.

## Loops

Python has a few different ways to repeat code, but the most common and straightforward is the for loop. A for loop lets you do something again and again until a condition is met. Here’s how they look:

for i in range(10):
print(i)
This loop will print out the numbers 0 through 9. The range() function generates a list of numbers, which in this case is 0 through 9. The for loop takes each number in that list and assigns it to the variable i. Then it runs the indented code block beneath it once for each value of i. In this case, that means it will print out the current value of i on each iteration.

You can use any variable name you want in a for loop, but i is conventionally used because it stands for “index” — meaning it keeps track of which iteration of the loop we’re currently on.

## Functions

Functions are defined blocks of code that take one or more input values, perform some calculation or action, and return a single output value. In Python, functions are defined using the keyword def. For example, the following function takes two input values, x and y, and returns the value of x – y:

def subtract(x, y):
return x – y

To call this function from within your program, you would use the following syntax:

result = subtract(10, 5)
print(result) # Output: 5

## Modules

In Python, the modulo operator is a percentage sign (%). It yields the remainder of the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. If the left argument is an uncaught exception, it is reraised and therefore not caught by an except clause.

The arguments may be floating point numbers, e.g., 3.14%0.7, but this is not handled specially and traditional integer division takes place, so if both arguments are integers then an integer division occurs. (The floor() Instance Method is available in most languages and can be used to achieve this.)

## I/O

Subtraction is one of the basic arithmetic operations in Python. It is represented by the minus (-) sign. When we subtract one number from another, we find the difference between them. For example, if we subtract 5 from 10, we will get 5 as the answer (10 – 5 = 5).

## Exceptions

When subtracting in Python, there are a few exceptions to be aware of. First, if both operands are of type int, then the result will also be an int. If either operand is a float, then the result will be a float. Second, if either operand is complex, then the result will be complex. Finally, if one of the operands is infinity or negative infinity, then the result will be infinity or negative infinity respectively.