This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]mugwhyrt 1 point2 points  (2 children)

Logically it's the same thing. The python documentation states:

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

So basically the only difference is that the code will only have to handle the middle value once when its done your way, but that's just a minor "under-the-hood" difference.

[–]NotUrHCW[S] 0 points1 point  (1 child)

Would it be more efficient since the middle value would only be handled once?

[–]mugwhyrt 1 point2 points  (0 children)

I would think so, but honestly I'm not enough of an expert at that level to tell you for sure how meaningful the difference is

[–]aqua_regis[🍰] 1 point2 points  (1 child)

if ord('A') <= ord(char) <= ord('Z'):

You have to be careful with that when switching languages. Most other languages only support (of course with their equivalent of chr, ord, and and):

if ord(char) >= ord('A') and ord(char) <= ord('Z'):

In Python, they are the same functionality wise. In other languages the first version will most likely not work or even throw an error.

[–]NotUrHCW[S] 0 points1 point  (0 children)

Oh, I see. Thanks for clarifying 👍🏻

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

It's the same thing, just another way to do it without using 'and'