Conditionals and Control Flow

Learn how to use conditionals and control flow to create programs that generate different outcomes.

StartPython Illustration
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 [...] something,...

  2. 2

    Let's start with the simplest aspect of control flow: comparators. There are six: Equal to ( [...] ) [...] Not equal to ( [...] ) [...] Less than ( [...] ) [...] **Less...

  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 . [...] Let's switch it up: we'll give the boolean, and you'll write the expression, just l...

  5. 5

    Boolean operators compare statements and result in boolean values. There are three boolean operators: 1. [...] , which checks if both the statements are [...] ; 2. [...] , which checks if a...

  6. 6

    The boolean operator [...] returns [...] when the expressions on both sides of [...] are true. For instance: [...] is [...] ; [...] is [...] .

  7. 7

    The boolean operator [...] returns [...] when at least one expression on either side of [...] is true. For example: [...] is [...] ; [...] is [...] .

  8. 8

    The boolean operator [...] returns [...] for false statements and [...] for true statements. For example: * [...] will evaluate to [...] , while [...] will return [...] .

  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. [...] is evaluated first; 2. [...] is ...

  10. 10

    Great work! We're almost done with boolean operators. [...]

  11. 11

    [...] is a conditional statement that executes some specified code after checking if its expression is [...] . Here's an example of [...] statement syntax: [...] In this example, [...] ...

  12. 12

    Let's get some practice with [...] statements. Remember, the syntax looks like this: [...] Looking at the example above, in the event that [...] returns [...] , then the indented block of ...

  13. 13

    The [...] statement complements the [...] statement. An [...] / [...] pair says: "If this expression is true, run this indented code block; otherwise, run this code after the else statement."...

  14. 14

    [...] is short for "else if." It means exactly what it sounds like: "otherwise, if the following expression is true, do this!" [...] In the example above, the [...] statement is only checke...

  15. 15

    Really great work! Here's what you've learned in this unit: Comparators [...] Boolean operators [...] Conditional statements [...] Let's get to the grand finale.

  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. [...] In the above example, [...] accepts a string, prints it, and then waits for the user to type something and press Enter (or Return). In the inte...

  4. 4

    Next we need to ensure that the user actually typed something. [...] We can check that the user's string actually has characters!

  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: [...] In the first line, we create a strin...

  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 [...] function does not modify the string itself, it simply returns a lowercase-version. In the example above, we ...

  9. 9

    Now that we have the first letter stored, we need to add both the letter and the string stored in [...] to the end of the original string. Remember how to concatenate (i.e. add) strings together...

  10. 10

    Well done! However, now we have the first letter showing up both at the beginning and near the end. [...] 1. First we create a variable [...] and give it the string [...] 2. Next we access ...

  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 [...] statements you were usin...

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

Conditionals and Control Flow

StartPython Illustration