Simple decisions
Life is full of decisions.
- Do I stay at the party, or is it too awkward?
- Is the price too high?
- Should I study more for the exam?
Programs also need to make decisions. Depending on the circumstances, programs should behave differently.
- Is the entered password correct?
- Is the number even?
- Is the user's input valid?
- Is the dark mode enabled?
- Is the user logged in?
In this lesson, you’ll learn how to write programs that can make simple decisions using if
statements. if
(and related keywords else
and elif
) will let you write code that can handle different situations appropriately.
In the example above, you are going to stay at the party if you do not feel awkward. You can write that decision in a general if
statement:
if I don't feel awkward:
stay at the party
This example isn’t quite something Python could execute, but it’s surprisingly close!
If statement
The if
statement is the simplest tool for controlling the flow of the program in Python.
if x > 0 :
print('x is positive')
Up until now, Python has run every line of code in our programs, in order. With the if
statement, that’s no longer the case. The print
here only runs if x
really is more than 0
. Otherwise, the print
will never run at all.
A one-branch if
statement is the simplest conditional statement. Below is a flow control diagram for the code snippet.
The boolean expression after if
, in this case x > 0
, is called the condition. If it is true, then the indented statement gets executed. If not, nothing happens.
In the rest of this lesson, you’ll learn more about:
- boolean values and boolean expressions
- conditional statements
if
,elif
, andelse
keywords