Strange python syntax, or in print statement -


i can't find this, i'm forced ask here. i'm sure it's easy question knows python well.

python 2:

print raw_input() == 0 or hash(tuple(map(int, raw_input().split(' ')))) 

python 3:

print(input()==0 or hash(tuple(map(int,input().strip().split())))) 

i trying understand why 'or' in print statement. code in question has boolean operator inside print statement, comparing boolean , int. need explained me. specific python. code print in case input()==0 returns true? how can compare boolean , hash, , again, doing making boolean comparisons inside print statement?

in python, comparisons or or and make use of 2 features:

  1. truthy , falsey values, and
  2. short-circuiting.

so, when have this:

print(input()==0 or hash(tuple(map(int,input().strip().split())))) 

it'll follow order of operations, checking whether input() returns 0. since it's or next term, if it's true next term has no impact on result , won't evaluated. if happens, it'll print true, since that's what's returned input()==0.

if that's false, it'll evaluate next part, getting input, mapping integer, turning tuple, , hashing it. it'll return hash whether or not it's truthy value (a number other 0, sequence or set contents, etc.).


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -