Numbers in Python
Numbers in Python#
Let’s do some basic aritmethic operations with numbers:
10+5
15
Both 10 and 5 are integers and the result is an integer too!
Next, we can do some division to see what happens our integers:
10/4
2.5
It is a float! Now, what can we do to get an integer? - floor division (//)
10//4
2
With floor division we will get a truncated result. Now, try what happens if one of the numbers is negative:
-10//4
-3
What if one the numbers is float?
10.0//4
2.0
No matter you are doing a floor division or not, if you have a float, you will get a float as a result.
Common Operators for integers and floats in Python
Operator |
Description |
Integer |
Result |
Floating Point |
Result |
|---|---|---|---|---|---|
- |
Subtraction |
9-2 |
? |
9-2.0 |
? |
+ |
Addition |
9+2 |
? |
9+2.0 |
7 |
/ |
Division |
9/2 |
4.5 |
9/2.0 |
4.5 |
// |
Floor Division |
9//2 |
4 |
9//2.0 |
4.0 |
% |
Modulus |
9%2 |
? |
9%2.0 |
? |
* |
Multiplication |
9 * 2 |
? |
9* 2.0 |
? |
** |
Exponent |
9 ** 2 |
? |
9** 2.0 |
? |
9%2.0
1.0
Now, go ahead and try the all the operators to see what results you will get: