+ 1

Can someone explain how ((3**2)//2) output is 4 ?

23rd Nov 2021, 2:38 AM
Kavinda Pethiyagoda
6 Réponses
+ 7
John Jeets ** is exponential operator x**y here y is the power of x so 3**2 = 3 * 3 = 9 // is known as floor division which returns whole integer (without fractional part) value not float So 9 // 2 = 4 / is known as normal division which returns float value So 9 / 2 = 4.5
23rd Nov 2021, 2:53 AM
A͢J
A͢J - avatar
+ 5
3**2 = 3 * 3 = 9 9 / 2 = 4.5 9 // 2 = 4
23rd Nov 2021, 2:52 AM
zexu knub
zexu knub - avatar
23rd Nov 2021, 3:02 AM
Kavinda Pethiyagoda
+ 2
3**2 is equivalent to saying 3^2 as exponential so that gives 9 and 9//2 is floor division which ignores the decimal part say when you divide 9 by 2 your quotient is 4 and remainder is 1 but actually it's 4.5 so // here ignores. . 5
24th Nov 2021, 6:32 PM
Ankit Kumar Singh
Ankit Kumar Singh - avatar
+ 1
// is floor division
23rd Nov 2021, 3:25 PM
Shadoff
Shadoff - avatar
+ 1
Python has things very similar to real life, which are, for example, the parentheses, now the ** sign is the expose; * is multiply; / is to divide, but, the result is a decimal expression; // is to divide, but, the result is a natural number; % is the remainder of the division; + is adding and - is subtracting. Some examples: print (2 ** 3) Outcome: 8 print (2 * 3) Outcome: 6 print (2/3) Outcome: 0.6 ... print (2 // 3) Outcome: 0 print (2% 3) Outcome: 2 print (2 + 3) Outcome: 5 And subtracting would be -1. Therefore, 3 ** 2 is 9 and 9 // 2 is 4; 4 by the // sign. I hope it is of use to you, any questions, ask me. Greetings.
24th Nov 2021, 8:02 PM
CGO!
CGO! - avatar