Thursday 8 August 2013

python check if variable is empty

This is very easy to know in python that if any variable is empty or not

try to use :

if not ur_var:
     print "variable is empty 

get attention towards   not   keyword . This does all things .

This is equivalent to empty($ur_var)  in php

not keyword in python also checks for lists (array) , objects , strings , integrers , floats etc ..

example :

a= 0  or a = "" or a ={} or a= []
if not a :
    print "empty"

in all cases it will print empty .