0

If print(10/2) shows result as 5.0 , print(10//2) shows 5 then print(10*2) shows 20.0 but print(10**2) is showing 100.0 why?

12th May 2021, 6:03 AM
pranav
pranav - avatar
5 Réponses
+ 5
Kpranav K / is the Division Operator, will return a float // is the floor Division operator, which will round down to the next whole number (integer Division), will return an int * is multiplication operator. It will return an int, when both operands are int, else it will return a float. ** is power operator, like ^x, will return an int, when both operands are int, else it will return a float. Now, With the above, print(10*2) will indeed Show 20, not 20.0. Instead print(10.0*2) will show 20.0.. In case, you want to surely have an int, you could convert it accordingly. print(int(10.0*2)) will show 20. You could also use round, ceil, floor, depending on your needs.
12th May 2021, 6:38 AM
G B
G B - avatar
+ 3
Try to run 10 ** 2, and it will return an integer. Remember that the result or answer will be float if atleast one of the number is float, or the operation is division. https://code.sololearn.com/c4BOmqAKw3T8/?ref=app
12th May 2021, 6:27 AM
noteve
noteve - avatar
+ 1
Hi! Becouse 10**2 == 10^2 == 10² == 10 *10 == 100
12th May 2021, 6:09 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
print(10*2) outputs "20". If you want to print it as 20.0 you can do print(float(10*2)) print(10**2) outputs 100. Again you can convert to float to output 100.0
12th May 2021, 6:23 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
x**y is like x^y, so if you print(10**2), then it will be 10^2, 10*10, which means 100
12th May 2021, 8:42 AM
senz
senz - avatar