Conditional Statements
So far, the Python programs you’ve written have all run step by step, from top to bottom, executing each line exactly once. This week, and the next week, you’ll learn to write programs where the flow of control is more complex.
Programs need to be able to do different things, depending on the circumstances. Conditional Statements will be your tool for writing code that can make decisions.
The kinds of decisions will vary, from "is this number even?" to "did the user input the correct password?", but they all boil down to a set of yes-or-no questions.
In Python, the data type for representing yes-or-no answers is the Boolean: True
or False
.
Topica Covered
This week, you'll learn:
if
statements let you write code that makes decisions- You can use comparison operators like
==
,<
,>
to compare values and getTrue
orFalse
elif
andelse
blocks let you handle more complicated branching situations- You can combine booleans expressions with
and
,or
, andnot