Numbers
Numbers
The first basic data type we’ll cover are numbers
There are a two different “types” of numbers in python:
- Integers - positive or negative whole numbers.
- Float - real numbers, but also can contain decimal points
Here are some Examples
Integers
1
57
2020
Floats
6.5
2.7e10 # Scientific Notation
3.14159265
These are the 3 most basic number data types in Python. We can also perform different mathematical operations with these numbers too!
Number Operators
Like in math, Python allows us to do various operations with these numbers
Here’s a list of the operators:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Exponentiation (**)
- Modulo (%)
Again, here are some examples
Addition
# + Adds numbers
2 + 2 # equals 4
56 + 44 # equals 100
243.132 + 584 # equals 827.132
Subtraction
# - Subtracts numbers
10 - 7 # equals 3
0.5 - 0.3 # equals 0.2
345 - 543 # equals -198
Multiplication
# * Multiplies numbers
2 * 2 # equals 4
3.1415 * 2 # equals 6.283
-3 * 6 #equals -18
Division
# / Divides numbers
8 / 4 # equals 2
10 / 2 # equals 5
3 / 4 # equals 0.75
Division can also used with 2 slashes to perform a floor division
# // Divides then Floors numbers
3 // 4 # equals 0
9 // 2 # equals 4
100 // 3 # equals 33
Exponentiation
# ** Exponentiates numbers
4 ** 4 # equals 256
5 ** 2 # equals 25
2.5 ** 3 # equals 15.625
Modulo
# % returns the Remainder of the numbers when divided
5 % 2 # equals 1
35 % 5 # equals 0
100 % 7 # equals 2
Final Note
Python also supports typical order of operations, PEMDAS. Python will read from left to right and follow the following convention:
- Parenthesis
- Exponents
- Multiplication and/or Division
- Addition and/or Subtraction