Numbers in Python
There are mainly two types of number in Python:
- Integer numbers which are whole numbers
- Floating point numbers with decimal points
Let’s see some examples:
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5.0*6) / 4
5.0
The return type of a division (/) operation depends on its operands. If both operands are of type int, floor division is performed and an int is returned.
If either operand is a float, classic division is performed and a float is returned. The // operator is also provided for doing floor division no matter what the operands are. The remainder can be calculated with the % operator. You can calculate power using ** operator.
>>> 17 / 3 # int / int -> int
5
>>> 17 / 3.0 # int / float -> float
5.666666666666667
>>> 17 // 3.0 # explicit floor division discards the fractional part
5.0
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 ** 2 # square of 5
25
What is the output of 3/2 in Python 3 ??
a) 1 b) 1.5
Answer is : option B.
Python 3 performs true division by default!
Does 0.1+0.2–0.3 equal 0.0 ??
If you say YES, you are wrong. Refer following url for better understanding:
In next chapter, we will look into Variable assignments.
Happy Coding!