Python: Testing Multiple Conditions

  1. Introduction to Python
  2. Python: Choosing a Text Editor or IDE
  3. Python: Hello World
  4. Python: Variables, Strings and Numbers
  5. Python: Variable Naming
  6. Python: Math, Familiar
  7. Python: Math, Less Familiar
  8. Python: Mathematical Order of Operations
  9. Python: Introducing PEP 8
  10. Python: Text Concatenation
  11. Python: if Statements and Comparison Operators
  12. Python: else and elif statements
  13. Python: Testing Multiple Conditions
  14. Python: Testing Sets of Conditions
  15. Python: Nested if Statements
  16. Python: Lists
  17. Python: Adding To and Changing Lists
  18. Python: Lists: Take a Slice, Delete Elements, Popping Elements
  19. Python: Tuples
  20. Python: for Loops
  21. Python: Nested for Loops
  22. Python: Capturing and Formatting User Input
  23. Python: Dictionaries
  24. Python: Functions
  25. Python: While Loops
  26. Python: Creating and Using Classes
  27. Python: Data Files
  28. Python: Modules
  29. Python: CSV Files
  30. Python: JSON Files
  31. Python: Errors and Exception Handling
  32. Python: Using Pexpect
  33. Python : Using Pexpect :
  34. Python : Using Pexpect:
  35. Python: DCL Conversion to Python

Go to Chapter 12 in A Smarter Way.

Okay, let’s think more deeply about how we do testing. Consider a list of friends: if anyone on the list is present, we might want to greet each one who is at our party:

friends_at_party = ['bill', 'joel', 'jeff', 'jim']

if 'bill' in friends_at_party:
    print("Hello, Bill!")
if 'joel' in friends_at_party:
    print("Hello, Joel!")
if 'jeff' in friends_at_party:
    print("Hello, Jeff!")
if 'jim' in friends_at_party:
    print("Hello, Jim!")

If we are snotty, however, we might list our friends in the order of “best-ness,” and only greet the “most best” friend, and snub everyone else:

friends_at_party = ['bill', 'joel', 'jeff', 'jim']

if 'bill' in friends_at_party:
    print("Hello, Bill!")
elif 'joel' in friends_at_party:
    print("Hello, Joel!")
elif 'jeff' in friends_at_party:
    print("Hello, Jeff!")
elif 'jim' in friends_at_party:
    print("Hello, Jim!")

Only the first match is going to get a hello. The rest of the code is skipped.

But what if things are more complicated? Notice that we only have one list to consult. What if there are two conditions?

friends = ['bill', 'joel', 'jeff', 'jim', 'scott', 'todd']
people_at_party = ['joel', 'jim']

# Go through all the people who are present, 
# and greet our friends.
for person in people_at_party:
    if person in friends:
        print("Hello, %s!" % person.title())

Take a good look at the indentations. There’s a condition inside a condition, so there are two levels of indent. Don’t forget that technically an indent equals 4 spaces in Python.

Also note that %s in the last line. Technically this is a string formatting operator, and it makes it a little easier to cast a variable into a string for printing. %s means convert the variable to a string, %d to convert to a decimal, etc. Check out the official documentation:

What’s cool about these operators is that you can stack them up in a print statement, and then list the items that will fill them in the same order after a % character. But the % character is not the only way to do this:

Fname = "Fred"
Lname = "Farkle"
# Use % and a tuple:
print('Hi, your first name is %s and your last name is %s' 
      % (Fname, Lname))

# Formally recommended: use str.format(Fname, Lname).
print("Hi, your first name is {0} and your last name 
     is {1}".format(Fname, Lname))

# Using brackets without numbers will also work,
# as long as you're using the variables in order:
print('Hi, your first name is {} and your last name 
      is {}'.format(Fname, Lname))

Also take note of the cool implicit line continuation inside parens, braces or brackets in Python. It is critical that you indent to the beginning paren/brace/bracket!




  1. Go to Stack Overflow:
  2. Do a Search. Look for how you can break a long line to multiple lines in Python.
  3. In your script, try each of the code blocks above.
  4. What do you need to add to make the last code block work? Be sure to add it.