Conditional statements are adaptations to the default control flow. They allow your program to make decisions.
Main Conditionals in this Section
You'll get to know the following keywords and their usage in this section:
ifelifelse
All three of them are used to make decisions in your program. The most important one of the three is the if keyword, which is used in the aptly named if statements. You can use if without the other two keywords, while you can use those only in combination with an if statement.
What is the If Statement
The if statement allows your program to make decisions. There's not a lot of freedom in what these decisions are---you're still miles away from real Artificial Intelligence. But conditional statements give your programs much more flexibility.
How Does the If Statment Work
The idea of an if statement goes as follows:
ifsomething isTrue, then do what follows in the indented part below.
Example of an If Statment
To clarify this concept, you'll look at a code implementation of a basic if statement:
if True:
print("yay!")
If the condition following the if keyword evaluates to True, then the indented code gets executed. Otherwise, Python just skips it.
Info: Indentation has meaning in Python! You need to indent the code after the conditional statement exactly four spaces.
In the above example, the code will always get executed because True is always True! So, in this case, your code doesn't have to think much before knowing what to do. However, your conditional statement that follows the if keyword can be any code expression that evaluates to a Boolean.
Conditionals and Python Operators
In the previous section, you got to know a lot of Python expressions through operators that evaluate to either True or False. You can use any of them to create a more interesting conditional statement. You can use any expression that Python can ultimately turn into a truth value.
Conditional Flags
One common way that decisions are made in your programs is through the use of flags:
flag = True
if flag == True:
print("yay!")
This code snippet will currently always print yay! because you never change the value of the flag variable. However, you could make that switcheroo happen!
Fun with Flags
- Add a second conditional statement to the code snippet above that executes only if the
flagvariable isFalse. - Run the code and confirm that the second
print()statement doesn't get executed. - Now add a line to your first conditional statement that changes the value of
flagtoFalsein the indented code block. - Run the code again and confirm that both
print()statements now get executed.
What happened here? You used the changing state of your flag variable to gently guide your code to do what you wanted it to do.
Info: Keep in mind that flag is just a variable name. You could name this variable anything you wanted to. The concept of using the state of a Boolean value to decide what to do in a conditional statement is sometimes called a flag, which is why you named the variable like that for this general example.
Additional Resources
- Control Flow: The if Statement
In a future lesson, you will use more complex conditions, and you will add the else statement to your control flow.
Summary: Python If Statement
- The
ifstatement contains code that gets executed only under a certain condition - When the if condition is not met, Python will ignore the code inside the if block
- A conditional flag uses the state of a Boolean value to decide what to do in a conditional statement
- The main conditions in this section of the course are
if,elif, andelse