Loading...
I used two slashes by mistake and got a different answer from a single slash. Now I am not sure which division to use in my programs. What is the difference?
Python has two division operators. A single slash, /, is true division and always gives a float result, so 10 / 3 gives 3.3333333. A double slash, //, is floor division and gives the largest whole number less than or equal to the result, dropping the fractional part, so 10 // 3 gives 3. Note that // rounds down, not toward zero, so -10 // 3 gives -4, not -3. There is also the modulo operator %, which gives the remainder: 10 % 3 gives 1. Use / when you want exact decimal results, and // when you need a whole number, for example to find how many full groups fit or to get digits of a number. Pairing // and % together is common for splitting numbers.
Sign in as a tutor to answer this doubt.