In [1]: import math In [2]: x = lambda num: int(f[1]) if (f := math.modf(num))[0] == 0 else f[0] + f[1] In [3]: x(4.0) Out[3]: 4 In [4]: x(4.1) Out[4]: 4.1Just noticed you are talking about decimal.Decimal numbers.
It works with those too. Just tested
In [5]: import decimal In [6]: num = decimal.Decimal('37') In [7]: x(4.1) Out[7]: 4.1 In [8]: x(4.0) Out[8]: 4 In [9]: x(num) Out[9]: 37 In [10]: num = decimal.Decimal('37.0') In [11]: x(num) Out[11]: 37 In [12]: num = decimal.Decimal('37.1') In [13]: x(num) Out[13]: 37.1Ignore in/out 7 and 8. I repeated them by mistake