Back to community
Computer ScienceClass 11 (Maharashtra HSC)

Why does 10/3 give 3.33 but 10//3 gives 3 in Python?

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?

Asked by Imran Shaikh 22 45d ago
Need a Computer Science tutor? Browse verified Computer Science tutors near you.

1 answer

0

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.

K
Kavita Rao
380 pts· 45d ago

Sign in as a tutor to answer this doubt.