Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "variable or 0" mean in python?

What is the meaning of the following statement in python:

x = variable_1 or 0 

variable_1 is an object. What value does x have above? And what is the type of x?

like image 718
alwbtc Avatar asked Aug 31 '12 10:08

alwbtc


People also ask

What is meant by 0 in Python?

Python 0 is an indicator of the format method that you need to be replaced by the format's first (index zero) parameter. It is used to execute a string formatting operation. The formatted string argument contains a literal text or replacement fields delimited by braces { }.

Is 0 the same as 0 in Python?

Python returns the first character of a string when either of these are used as index values. 0 and -0 are the same.


1 Answers

If variable_1 evaluates to False , x is set to 0, otherwise to variable_1

Think of it as

if variable_1:   x = variable_1 else:   x = 0 
like image 188
sloth Avatar answered Oct 19 '22 06:10

sloth