if statement - Tricky Simple Python -


can 1 please explain me why if condition working.

x = 0xa5  if x == 0xaa or 0x5a or 0xa0 or 0xab:     print "host address correct"    0xaa or 0x5a or 0xa0 or 0xab binary operation not equal 0xa5 either 

python isn't doing think - when say

if x == 0xaa or 0x5a or 0xa0 or 0xab: 

it's checking if x==0xaa or 0x5a "truthy" - wherein non-empty string, example, considered true - , on. error getting suggests 1 of 0x5a, 0xa0, 0xab "truthy". need is

if x == 0xaa or x == 0x5a or x == 0xa0 or x == 0xab: 

which can expressed more as

if x in [0xaa, 0x5a, 0xa0, 0xab]: 

though should noted if 0xaa etc strings, need written '0xaa' etc.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -