9

I erroneously wrote this code in Python:

name = input("what is your name?")
if name == "Kamran" or "Samaneh":
    print("That is a nice name")
else:
    print("You have a boring name ;)")

It always prints out "That is a nice name" even when the input is neither "Kamran" nor "Samaneh".

Am I correct in saying that it considers "Samaneh" as a true? Why?

By the way, I already noticed my mistake. The correct form is:

if name == "Kamran" or name == "Samaneh":
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kamran Bigdely
  • 7,946
  • 18
  • 66
  • 86
  • This should not have been linked as a duplicate for the "how do I write this `if` statement" canonical, because OP **already understood** how, and was asking a question about **why** the result of the wrong code is as it is. The question was specific about the details of how the code is evaluated, in a way that isn't directly addressed by that canonical. It is, however, addressed by a **different** canonical. (Fortunately, there aren't too many things that have been incorrectly closed as duplicates of **this** one....) – Karl Knechtel Feb 03 '23 at 12:13

5 Answers5

13

Any non empty string in Python (and most other languages) is true as are all non-zero numbers and non-empty lists, dictionaries, sets and tuples.1

A nicer way to do what you want is:

name = input("what is your name?")
if name in ("Kamran", "Samaneh"):
    print("That is a nice name")
else:
    print("You have a boring name ;)")

This creates a tuple containing the names that you want and performs a membership test.

1 As delnan points out in the comments, this applies to all well written collections. That is, if you implement a custom collection class, make sure that it is false when it's empty.

aaronasterling
  • 68,820
  • 20
  • 127
  • 125
  • Not only lists, dicts and tuples -- *all* empty collections (if well-written). –  Dec 25 '10 at 21:54
  • @delnan, I was trying to think of a good way to say that so I stuck to the built ins and forgot sets. – aaronasterling Dec 25 '10 at 22:09
  • Most objects, collection or otherwise, are truthy; only the ones wheich are explicitly define otherwise aren’t. If you just define a class with `class C(): pass`, instances will test as `True`. – Daniel H Jul 11 '17 at 19:21
8

Besides the empty string '', strings will all evaluate to True (see this page for a full list of values of all types that evaluate to False. This follows the logic of many other programming languages (except some which also evaluate strings like '0', 'false', etc. to False). The exact decision of what to do is somewhat arbitrary, but the choice made can be explained as allowing the cast to be used as a simple way to test for empty (default, or unpopulated) strings.

You can always force a cast of any type to bool using the bool() function.

>>> bool('')
False
>>> bool('non-empty string')
True
>>> bool('0')
True
>>> bool('False')
True
>>> bool('false')
True
moinudin
  • 134,091
  • 45
  • 190
  • 216
2

http://docs.python.org/library/stdtypes.html#truth-value-testing

"....All other values are considered true — so objects of many types are always true."

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
2

In Python an empty string is considered False, True otherwise.

You could use the in operator:

if name in ("Kamran","Samaneh"):
    print("That is a nice name")
else:
    print("You have a boring name ;)")
systempuntoout
  • 71,966
  • 47
  • 171
  • 241
0

A non-empty string is True, yes. An empty one is False. This is super-handy.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251