1

In the code below:

def modify_note(self):
    id = input("Enter a note id: ")
    memo = input("Enter a memo: ")
    tags = input("Enter tags: ")
    if memo:
       self.notebook.modify_memo(id, memo)
    if tags:
       self.notebook.modify_tags(id, tags)

memo and tags are string type variables. How can you write them after if, does python regard them as boolean here?

4
  • Is this Python 3.x? input() has a different meaning in 2.x and 3.x. Commented May 13, 2011 at 13:51
  • 2
    Just a note: unless you're using python 3, careful with input()! It's not the same as raw_input, as it evaluates whatever input you type in, equivalent to eval(raw_input())! Commented May 13, 2011 at 13:53
  • yes it is python 3.x. what happened to raw_input in python 3? Commented May 13, 2011 at 14:08
  • 1
    "what happened to raw_input in python 3?" That's an unrelated question. First, search stack overflow. Then read the Python 3 notes in Python's web site. It's no secret. Please read all available information. Commented May 13, 2011 at 14:27

3 Answers 3

8

Every object in Python has a truth value. Strings are True if they are non-empty.

Sign up to request clarification or add additional context in comments.

Comments

6

The if memo and if tags statements are checking the truthiness of the memo and tags variables.

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False.

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

2 Comments

but in that code there is no way entering an empty string for tags and memo variables. which means they will always be true. right?
@alwbtc: If the user hits enter right away, you get an empty string, as the newline isn't included in the return value.
0

It all depends on Python's version of truthiness

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.