Q:

python if condition "and" keyword issue

I simply trying calling this code
uname=""
passwd="jhj"
if uname =="":print "uname"
if passwd=="":print "passwd"
if not uname and passwd:
    print "either"

and the output is:
uname
either

shouldn't It be "uname"?how can I fix this?

A:

Simple fix would be
if not uname and not passwd:

since it evaluates as
if (not uname) and (not passwd):

or
if not (uname or passwd):

but this would not work as awaited if neither uname nor password are provided (it would print all three).
You could change the logic to this construct where only one print is executed:
uname = ""
passwd = "jhj"

if not (uname or passwd):
    print "either"
elif not uname:
    print "uname"
elif not passwd:
    print "passwd"

A:

What you are doing here is this:
if (not uname) and passwd:

If you want to check if they are both false, you need to do:
if not uname and not passwd:

Note that:
if not (uname and passwd):

Is not the same thing, as if one item is True, the whole thing will evaluate false. If you want to do it with one not, the correct logic is:
if not (uname or passwd):

