Grokking Modern System Design Interview for Engineers & Managers
Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.
The division function in Python has two variations:
Float division: gives a decimal answer.
Integer division: gives the answer in whole numbers (the division result is rounded to the nearest whole number).
The float division operation /
is used when you want a more precise division result, as it does not round off the value to whole numbers.
It must be noted that for recurring decimal values, there is a limit to how many digits a float
variable can store, so it is not always possible to store the exact value.
The return value for the float division operator is always of type float
, even when the operands of the operator are not float
variables themselves.
This behavior of the float division operator began with the advent of Python3, as in Python2, the float division operator would return an integer value if both operands were integers.
The following code example shows that regardless of what numeric data type your variables are, your answer is always in float
type if you use the float division operator.
# initializing two floatsmy_float1 = 10.0my_float2 = 10.0print ("Result of float division of two floats: ", my_float1/my_float2)# initializing two integersmy_int1 = 10my_int2 = 10print ("Result of float division of two integers: ", my_int1/my_int2)
The integer division operation //
is used when you want your answer to be in whole numbers. Since it is very common for the result of dividing two integers to be a decimal value, in Python3 integers, division
The return value for the integer division operator is of type integer
if the operands are both integers. In contrast, it returns a float
value if either one of the operands is a float.
The integer division operator was introduced in Python3, so for systems running Python2, the user must import the
__future__
module to use it.
The following code example shows the division of two integers to produce an integer result.
# initializing two integersmy_int1 = 10my_int2 = 10print ("Result of integer division of two integers: ", my_int1//my_int2)
The following example demonstrates that if either one of the operands (or both) is a float
, then the answer is also a float
, but rounded to the nearest lower bound whole number.
# initializing two floatsmy_float1 = 4my_float2 = 3.0print ("Result of integer division of floats and integers: ", my_float1//my_float2)# initializing two floatsmy_float3 = 4.0my_float4 = 3print ("Result of integer division of floats and integers ", my_float3//my_float4)
Note how the result is a float
type value, but just as for the normal integer division we saw earlier, the result is still floored. This means that only the quotient part is kept from the original float answer, while everything coming after the decimal is discarded and represented with a 0.
RELATED TAGS
CONTRIBUTOR
Grokking Modern System Design Interview for Engineers & Managers
Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.