Reputation:
I am beginner in programming. I have just started to learn Python and have recently install 3.7 on my mac. After that I tried to simple code such as print(18 / 4) and answer should be like this 4.5 if I use // then would be 4. I assumed that might be an installation problem. but when I reinstall the editor it seems same. To make sure that I am right or wrong in this case, I did use online editor from official python website, it seems ok (4.5). It may be not a huge problem but I want to make myself clear from beginning. Just need a quick solution! Thanks.
Upvotes: 0
Views: 249
Reputation: 34
"/"
Divide left operand by the right one (always results into float)
5/2=2.5
"//"
Floor division - division that results into whole number adjusted to the left in the number line, divide left operand by the right one (always result into int)
5//2=2
Upvotes: 0
Reputation: 20490
To begin with, Python does not depend on what editor you are using. You can use any editor to begin with.
From https://docs.python.org/3.7/tutorial/introduction.html
Division (/) always returns a float.
To do floor division and get an integer result (discarding any fractional result)
you can use the // operator; to calculate the remainder you can use %
So 18/4 gives you 4.5, a float result. But 18//4 gives you 4, a integer result, since it gets rid of the fractional result.
Upvotes: 3