Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (20.3k points)

I tried running this piece of code:

path = '/bla/bla/bla'

if path is True:

    print "True"

else:

    print "False"

And it prints False. I thought Python treats anything with value as True. Why is this happening?

2 Answers

0 votes
by (40.7k points)

Here, "is" compares identity. A string will never be identical to a not-string. And "==" is equality. But a string will never be equal to either True or False.

You want neither.

path = '/bla/bla/bla'

if path:

    print "True"

else:

    print "False"

0 votes
by (106k points)

True and False are boolean operations, and these are also used when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers including strings, tuples, lists, dictionaries, sets and frozen sets. All other values are interpreted as true.

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...