Python "Or" Usage with "In" Statement [Duplicate]

I've this code:

url='

('h' or 'f') in url[0:4] #This is True
('f' or 'h') in url[0:4] #This is False

I'm just trying to understand why the 'or' operator seems to evaluate just the first condition.

3

This or returns the first element if it evaluates to true (as a bool) and the second one otherwise... it's is usually applied to set default values.

This way, 'h' or 'f' is simply 'h' and 'f' or 'h' is simply 'f'.

You can achieve what you want with something like:

any(x in url[:4] for x in ['h', 'f'])
3
Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest