I can't understand the output of this code.
print(1//.2)
print(10//2)
The output of 1st is 4.0 and output of 2nd is 5
I can't understand the output of this code.
print(1//.2)
print(10//2)
The output of 1st is 4.0 and output of 2nd is 5
To clarify this, the // operator will give you the integer part of the result. So:
10 // 2 = 5
11 // 2 = 5
12 // 2 = 6
To get the rest you can use the modulo operator %:
10 % 2 = 0
11 % 2 = 1
12 % 2 = 0
Now, when you specify 0.2, this is taken as a floating point value. These are not 100% accurate in the way the system stores them, meaning the exact value of this is likely something like 0.2000000000000000001.
This means that when you do:
1 // 0.2
What you are really doing is:
1 // 0.2000000000000001
Which if course evaluates to 4. If you then try the modulo operator, you will see you get a value less than 0.2. When I ran this I saw:
>>> 1 // 0.2
4.0
>>> 1 % 0.2
0.19999999999999996