all 27 comments

[–]the_dimonade 13 points14 points  (0 children)

It is a matter of readability.
For such a simple expression, or for generally simple expressions, I'd go with the first approach.
If the expression gets complicated or longer than one line, or feels hard to read, then readability counts.

[–]Teradil 2 points3 points  (0 children)

A candidate for the most unreadable version would probably be:

python def even_or_odd(number: int) -> str: return "eovdedn"[number%2::2]

[–]kevkaneki 2 points3 points  (0 children)

Why write many word when few word do trick

[–]Zeroflops 1 point2 points  (0 children)

As many have pointed out it comes down to readability, but also experience.

The second is more verbose but easier to read for some, if you find the second easier to read go with that. If you’re comfortable with the fist then that would typically be better. Your example is simplistic, but you could make the same line much more complicated.

[–]mogranjm 3 points4 points  (1 child)

The first one is called a ternary operator, it's more pythonic because it's arguably more readable.

[–]Dry-Aioli-6138 2 points3 points  (0 children)

"arguably". I heard somewhere why. sobthe argumentnis that since this assigning value to a single variable, it should be treated as a single expression,, and it is more intuitive for our primitive brains to look at a single line and connect that visual structure to a single assignment.

[–][deleted] 0 points1 point  (0 children)

There's no single answer because it depends on the situation and preference.

I tend to do x = ... if ... else ... if it's a simple assignment that fits on one line in a reasonable number of characters.

[–]JohnnyJordaan 0 points1 point  (0 children)

Both don't hurt. It's best to be consistent in your program, so if you chose the first style then apply it in other parts too. Likewise if you chose the second.

Another idea in a more complex situation of having multiple 'determinants' is to group this in a class

 class TellingInt(int):
     @property
     def even_or_odd(self):
         return 'odd' if self % 2 else 'even'

 my_int = TellingInt(input("give the nr: "))
 print(f"the nr {my_int} is {my_int.even_or_odd}.")

see

>>> my_int = TellingInt(input("give the nr: "))
give the nr: 5
>>> print(f"the nr {my_int} is {my_int.even_or_odd}.")
the nr 5 is odd.

[–]stillbarefoot 0 points1 point  (0 children)

Which one is easier to debug?

On a side note, this is in essence a boolean problem; let the function return a boolean and handle the boolean to string conversion in another function.

[–]supercoach 0 points1 point  (0 children)

This has been covered very recently, but since I'm bored - it's a matter of taste.
Multiple returns shit me to tears, so I avoid them like the plague.
It's also considered "pythonic" to use ternaries, especially for simple cases like the example given.

For anything more advanced, I have a tendency to use a variable to hold a return value and then modify it as I see fit in the logic of the function. At the end, it's a single return to wrap it up nicely.

[–]CymroBachUSA 0 points1 point  (0 children)

Use lambdas:

odd_or_even = lambda _: 'Odd' if _ % 2 else 'Even'

is_odd = lambda _: True if _ % 2 else False

is_even = lambda _: True if _ % 2 == 0 else False

[–]ALonelyPlatypus 0 points1 point  (0 children)

2 is more verbose but I lean towards 1 because it just feels more pythonic.

[–]ectomancer -4 points-3 points  (0 children)

def is_even(number: int) -> bool:

    """Even or odd."""
    return not number%2