Short-circuiting with helper function in print()

Can someone please explain why the following code comes out as "geeks" in the console? def check(): return "geeks" print(0 or check() or 1) I’m assuming that Python recognizes the boolean operator, thus treating 0 == False? So does that mean every time boolean operators are used, the arguments are treated as True/False values? >Solution… Read More Short-circuiting with helper function in print()

Python all short-circuiting with None element

I read everywhere that Python all and any functions support short-circuiting. However: a = None all((a is not None, a + 1 > 2)) Throws the following error: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-4-6e28870e65c8>", line 1, in <module> all((a is not None, a + 1… Read More Python all short-circuiting with None element

Python 2 Syntax error when executing print in boolean expression

In order to demonstrate that python performs short-circuiting I tried to run the following code snipplet True or print(‘here’) and expected the code to execute, evaluate to True and not print "here". However, python 2.7 reports a syntax error: python2 -c "True or print(‘hier’)" File "<string>", line 1 True or print(‘hier’) ^ SyntaxError: invalid syntax… Read More Python 2 Syntax error when executing print in boolean expression