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:
https://docs.python.org/2/library/stdtypes.html#string-formatting-operations
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!
Exercises
See http://www.asmarterwaytolearn.com/python/12.html
See http://introtopython.org/if_statements.html
- Go to Stack Overflow:
https://stackoverflow.com - Do a Search. Look for how you can break a long line to multiple lines in Python.
- In your examples.py script, try each of the code blocks above.
- What do you need to add to make the last code block work? Be sure to add it.