Chevron Left Icon
Conditionals & Control Flow
Lesson 1 of 2
Chevron Right Icon
  1. 1
    Just like in real life, sometimes we’d like our code to be able to make decisions. The Python programs we’ve written so far have had one-track minds: they can add two numbers or print something, b…
  2. 2
    Let’s start with the simplest aspect of control flow: comparators. There are six: Equal to (==) >>> 2 == 2 True >>> 2 == 5 False Not equal to (!=) >>> 2 != 5 True >>> 2 != 2 False …
  3. 3
    Excellent! It looks like you’re comfortable with basic expressions and comparators. But what about extreme expressions and comparators?
  4. 4
    Comparisons result in either True or False, which are booleans as we learned before in this exercise . # Make me true! bool_one = 3 < 5 Let’s switch it up: we’ll give the boolean, and you’ll wr…
  5. 5
    Boolean operators compare statements and result in boolean values. There are three boolean operators: 1. and, which checks if both the statements are True; 2. or, which checks if at least one …
  6. 6
    The boolean operator and returns True when the expressions on both sides of and are true. For instance: * 1 3 is False.
  7. 7
    The boolean operator or returns True when at least one expression on either side of or is true. For example: 1 3 is True; 1 > 2 or 2 > 3 is False.
  8. 8
    The boolean operator not returns True for false statements and False for true statements. For example: * not False will evaluate to True, while not 41 > 40 will return False.
  9. 9
    Boolean operators aren’t just evaluated from left to right. Just like with arithmetic operators, there’s an order of operations for boolean operators: 1. not is evaluated first; 2. and is evaluate…
  10. 10
    Great work! We’re almost done with boolean operators. # Make me false bool_one = (2 <= 2) and “Alpha” == “Bravo”
  11. 11
    if is a conditional statement that executes some specified code after checking if its expression is True. Here’s an example of if statement syntax: if 8 < 9: print “Eight is less than nine!” …
  12. 12
    Let’s get some practice with if statements. Remember, the syntax looks like this: if some_function(): # block line one # block line two # et cetera Looking at the example above, in the eve…
  13. 13
    The else statement complements the if statement. An if/else pair says: “If this expression is true, run this indented code block; otherwise, run this code after the else statement.” Unlike if, els…
  14. 14
    elif is short for “else if.” It means exactly what it sounds like: “otherwise, if the following expression is true, do this!” if 8 > 9: print “I don’t get printed!” elif 8 < 9: print “I get pr…
  15. 15
    Really great work! Here’s what you’ve learned in this unit: Comparators 3 = 5 10 == 10 12 != 13 Boolean operators True or False (3 = 5) this() and not that() **Conditional stateme…
  1. 1
    Now let’s take what we’ve learned so far and write a Pig Latin translator. Pig Latin is a language game, where you move the first letter of the word to the end and add “ay.” So “Python” becomes “y…
  2. 2
    Let’s warm up by printing a welcome message for our translator users.
  3. 3
    Next, we need to ask the user for input. name = raw_input(“What’s your name?”) print name In the above example, raw_input() accepts a string, prints it, and then waits for the user to type somet…
  4. 4
    Next we need to ensure that the user actually typed something. empty_string = “” if len(empty_string) > 0: # Run this block. # Maybe print something? else: # That string must have been empty…
  5. 5
    Now we know we have a non-empty string. Let’s be even more thorough and check that our string only contains letters. Consider the following code: x = “J123” x.isalpha() # This will return ‘F…
  6. 6
    When you finish one part of your program, it’s important to test it multiple times, using a variety of inputs.
  7. 7
    Now we can get ready to start translating to Pig Latin! Let’s review the rules for translation: You move the first letter of the word to the end and then append the suffix ‘ay’. Example: pytho…
  8. 8
    Let’s simplify things by making the letters in our word lowercase. the_string = “Hello” the_string = the_string.lower() The .lower() function does not modify the string itself, it simply returns…
  9. 9
    Now that we have the first letter stored, we need to add both the letter and the string stored in pyg to the end of the original string. Remember how to concatenate (i.e. add) strings together? gr…
  10. 10
    Well done! However, now we have the first letter showing up both at the beginning and near the end. s = “Charlie” print s[0] # will print “C” print s[1:4] # will print “har” 1. First we create…
  11. 11
    Yay! You should have a fully functioning Pig Latin translator. Test your code thorougly to be sure everything is working smoothly. You’ll also want to take out any print statements you were using …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo