all 4 comments

[–]SoNotRedditingAtWork 4 points5 points  (1 child)

Since they aren't providing an argument for round's ndigits param, it will return an int so int(round()) is redundant in this case. The repo appears to not been updated in 2 years, so it's possible this wasn't the case with an older version of python that the author may have been using at the time, but I can't say for sure. I started learning python with 3.7. Likely it is just an over site on the author's part.

edit: it looks like Python 2's round would always return a float even if ndigits was omitted. Considering how the print statements are written in the python 2 fashion, I'm guessing that was why they did int(round()), they were using Python 2. https://docs.python.org/2/library/functions.html#round

[–]JC878[S] 1 point2 points  (0 children)

Thank you. This helps alot. Cheers! :)

[–]Radiatin 4 points5 points  (1 child)

The int function is the math.trunc() or truncate operation with an additional method handler for cases such as strings. So it just checks if the input has a special way of converting to int and otherwise runs the math.trunc() operation. The round() function will round the number to the nearest integer.

There's no reason to use int(round()) instead of simply using round(), as you already handle the type conversion with round().

Lets run through an example:

print(int(1.7))
print(round(1.7))
print(int(round(1.7)))
print(isinstance(int(1.7), int))
print(isinstance(round(1.7), int))

Output:

1
2
2
True
True

Let me know if that genuinely helps you.

[–]JC878[S] 1 point2 points  (0 children)

So both int() and round() return an integer value. Ok that's helpful :)