Simple arithmetic

Computers are good at math, that’s why they were created, and we can do all kinds of mathematical operations in python. We can add, subtract and multiply values.

sum = 2 + 2 # Addition
difference = 100 - 24.2 # Subtraction
product = 5 * 24 # Multiplication
quotient = 20 / 2 # Division

Here’s a list of all the arithmetic operators in python:

OperatorOperationDescription
+AdditionAdds two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivides two values
//Floor divisionDivides and rounds down to the nearest integer
%ModulusReturns the remainder after division
**ExponentiationRaise a number to a power

Assignment operators

When performing arithmetic on a variable you’ll need to reassign that variable to the new value.

count = 0
count = count + 1
count = count + 1
print(count)

Assignment operators are a shorthand, just add an equal sign after the operator to reassign the variable to the new value.

count = 0
count += 1
count += 2
print(count)