- 4

Python

When we operate (4+8)/2 we get 6 only.....but you are given the output as 6.0

29th Oct 2017, 7:15 AM
Aravind m.s
Aravind m.s - avatar
5 Answers
+ 11
Python display numbers by visual explicit format difference between floating point and integer number by appending '.0' to an integer value internally stored as a float one: in Python 3 slash ('/') is the operator for float division and always return a float value, while ('//') is integer division and return an integer... and int() cast the argument to integer value (stored as integer)
29th Oct 2017, 7:40 AM
visph
visph - avatar
+ 9
(4+8)/2 gives 6.0 (4+8)//2 gives 6 (5+8)/2 gives 6.5 (5+8)//2 gives 6 // is used to get quotient only / is used to divide number without leaving any remainder i.e. the value will be in decimal. So, it gives 6.0 instead 6
29th Oct 2017, 7:19 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 8
/ is just normal division, the return value is a float. To get rid of decimal places, use int() or //. Both works the same way
29th Oct 2017, 7:58 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
Try int((4+8)/2)
29th Oct 2017, 7:18 AM
Αητοιπe
Αητοιπe - avatar
+ 5
@Kartikey, in the last example, it seems you mistyped // with /, the one that gives quotation 6, please fix that so not to confuse the TS. Thanks :)
29th Oct 2017, 7:50 AM
Ipang