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:
Operator | Operation | Description |
---|---|---|
+ | Addition | Adds two values |
- | Subtraction | Subtract one value from another |
* | Multiplication | Multiply two values |
/ | Division | Divides two values |
// | Floor division | Divides and rounds down to the nearest integer |
% | Modulus | Returns the remainder after division |
** | Exponentiation | Raise 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)