Python Numbers #
Integer #
A whole number.
num = 3
print(type(num))
# <class 'integer'>
Float #
A decimal.
num = 3.14
print(type(num))
# <class 'float'>
Operators #
Some of this seems obvious, but for the sake of being complete, let’s state the obvious nonetheless.
Operator | Example | Result |
---|---|---|
Addition | 3+2 |
5 |
Subtraction | 3-2 |
1 |
Multiplication | 3*2 |
6 |
Division | 3/2 |
1.5 |
Floor division (i.e., no remainder) | 3//2 |
1 |
Exponent | 3**2 |
9 |
Modulus (i.e., remainder) | 3%2 |
1 |
Regarding the modulus operator – it’s often used to check if a value is even or odd:
3 % 2 # 1, odd
4 % 2 # 0, even
Logic #
Condition | Example | Result |
---|---|---|
Equal | 3==2 |
False |
Not Equal | 3!=2 |
True |
Greater than | 3>2 |
True |
Less than | 3<2 |
False |
Greater or equal | 3>=2 |
True |
Less or equal | 3<=2 |
False |
Incrementing #
num = 1
num = num + 1
or
num = 1
num += 1
Something different:
num = 1
num *= 10
Absolute value #
abs(<some number>)
Round #
Round to the nearest integer.
round(<some number>, <number of digits after decimal, defaults to 0>)
Casting #
Convert viable string to number.
int('1')