9-Conditionals
9-Conditionals
• Multiple Else-If
• Conditionals Recap
• If-Then Conditionals in Python
Objectives
• The term “conditional” comes from the idea that sometimes we want to
run some code conditionally; in other words, we only want to run it if
something is true.
If-Then
• Like an else, an else-if only runs if the original if-then did not.
• Unlike an else, however, an else-if has its own conditions to check; if the
conditions aren’t met, it doesn’t run either.
Multiple Else-Ifs
• We must start with if, and we can have at most one else, but we can have
any number of else-ifs in between.
• Each else-if and else will only execute if no previous condition has executed.
Multiple Else-Ifs
• Take note of the colon symbol line 6. A colon (:) is Python’s sign that an
indented code block is beginning.
• The indented line of codes means that it is “under” or “controlled by” the
conditionals statement on line 6.
• The indented code is the “then” code; it’s the code that runs if the
conditional is true.
• Anything indented directly under that conditional statement will be
controlled by that statement.
If-Then Conditionals in Python
• Line of codes that are not indented are not controlled by the conditional.
• Lines that are not indented will be run regardless of the result of the
conditional.
• This is our fundamental if statement: the word if, some logical statement
that resolves to True or False, a colon, and some indented code.
If-Then-Else Conditionals in Python
• Logically, this makes sense: the else block code runs if the if block code did
not run; if else was part of the if block code (that is, indented under it), it
wouldn’t run either!
• else must also be followed by a colon, Python’s sign that an indented code
block is beginning.
If-Then-Else Conditionals in Python
• The only necessity for an elif statement is that it must come after an if and
before any else at that level of indentation.
• We can have more than one elif.
If-Then-Else-If-Else Conditionals in Python
If-Then-Else-If-Else Conditionals in Python
• The else-if will not run if the first if runs. Even if the condition in the elif
statement is true, it will not run if he first if ran. The else-if only runs as an
alternative to the preceding conditionals.
• All the unindented if statement’s conditional will be run because it is not an
elif for the other statements.
• Only use else-if if you want the conditional to be skipped if a previous part
of the structure was true.
Conditionals and Operators
• Functions can return Booleans as well, which means we can use functions in
conditionals.
• Boolean operators allow us to take other operators and functions and
combine them into far more complex conditionals.
Boolean Functions and Operators
• You might remember that one of the things that makes Python unique is
easy access to functions that check if something is a member of another set.
• We could instead create lists, and easily check if the condition can be found
on the list.
• Python’s syntax is accessible enough that you might understand this just
based on the natural meaning of the word “in”
Boolean Functions
• Note the line 18 and 19. It ends in a slash, the next line is double-indented,
and the colon isn’t until after line 19.
• In Python, this is how we tell the computer, “Interpret these two lines as one
line.” Breaking the code between two lines makes it more readable for us as
humans, but the computer needs to see it as all one line.
• The slash says, “Copy the next line, and put it where this slash is.”
Boolean Operators
• Ifs Within Ifs. Note that while one major benefit of nested conditionals is
that we can take care of more combinations of conditions, another benefit is
that in many ways, the code is more readable.
• With these nested conditionals, we no longer have to break one line of code
between two lines just for readability.
• Instead, we have three short, simple conditional statements, one under the
other.
Nested Conditionals in Python
• Each is indented under the previous one, meaning each only runs if the
previous one also ran.
• That means the purchase is only approved if the first conditional and the
second conditional and the third conditional are all True, which makes it
functionally equivalent to our original statement.
• However, with this structure, each individual conditional can have its own
dedicated else block, meaning we can print exactly why the purchase failed.
Ifs Within Elses
• In Python, the scope of a variable starts when it is created, and ends when
one of a number of terminations happen.
• For now, the only termination you need to know is the program ending:
when the program ends and closes, the computer forgets the variables that
were created while it was running.
Accessing Variables within Conditionals
• Recall that the code above creates the variable result outside the
conditional.
• The scope of result is from line 4 until the program stops running.
• The scope of a variable is from the point at which it is created until the end
of the program.
Creating Variables within Conditionals
• If you create a variable within an if statement’s code block, but that code
block does not run. That means the variable was never created, and so if you
try to access the variable outside the code block, your code will crash.
Creating Variables within Conditionals