round() in Python

  • A+

I'm currently learning python with pycharm as IDE. While using the round() function I noticed that I get two different results depending on wether I don't explicitly choose the number of decimal places to include or choosing the number to be 0.

x = 4.1 print(round(x)) print(round(x, 0)) 

It prints the following:

4 4.0 

What is the difference?


When you specify the number of decimals, even if that number is 0, you are calling the version of the method that returns a float. So it is normal that you get that result.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: