Catch-up! It might feel like we’re “Behind”. Don’t worry: I give you the chance to go over things again and again. And I repeat things. That’s critical for learning and so that’s why we do it!
Outline of today’s aspiration.
- Textbook check-in: How’s it been for you?
-
Variables
- Debugging
- Logical turtles review
-
Conditionals
- Approx 2:10pm: Functions Preview
- Flow of Control
- Refactoring: Functional Turtle
By around 2:10 I’d like to be onto the functions preview. and hopefully help you refactor some Turtle before class ends. Anything we don’t get to, go through as part of your homework.
Variables
This is a console:
It reads, evaluates and prints any expression you type in. In programming generally this is also called a read, evaluate, print loop, or REPL.
The >>>>
is what Python shows at its default prompt. So now you know, if you see this in examples online, they're describing what would happen if you were to enter this into a REPL. Aside: If you've ever used Python in a notebook this is also what happens: results are printed automatically. This leads to confusion about Flow of Control, which is why we won't use notebooks in this class.
Let's Code, Shall We?
Let's dive into an apparently simple problem:
OK let's debug some code. There are at least 3 things wrong with this program. Let me tell you what it should do, and let's identify the errors, categorize them as logical or semantic, and fix the code to do what I told you it would do.
Along the way let's figure out how to make debugging faster by commenting out the input statements.
Write code below to get at least 3 values from the user using the input
function and output a mad lib (which will use the input to tell a silly story).
Conditionals
Here's a series of examples that give you an initial framework to start with, and demonstrate how crafting and running slightly different examples can help you learn.
If
num = 2
if num > 0:
print("Greater than 0")
if num > 1:
print("Greater than 1")
if num > 2:
print("Greater than 2")
How many of these things print? Why?
Else If
num = 2
if num > 0:
print("Greater than 0")
elif num > 1:
print("Greater than 1")
elif num > 2:
print("Greater than 2")
How many of these things print? Why?
Else if again
num = -2
if num > 0:
print("Greater than 0")
elif num > 1:
print("Greater than 1")
elif num > 2:
print("Greater than 2")
How many of these things print? Why?
Else
num = -2
if num > 0:
print("Greater than 0")
elif num > 1:
print("Greater than 1")
elif num > 2:
print("Greater than 2")
else:
print("Not greater than 0, 1, or 2")
How many of these things print? Why?
These are the kinds of experiments you might set up for yourself when you're asking Python questions in Python code.
Flow of Control!
Conditionals are great, but can get complex fast when they're chained and nested. This is our first introduction to Flow of Control, which is a fancy way of saying "what's Python doing now? why?"
Much more about this, but pay attention to what Python's doing, and why, throughout this chapter. Conditionals are a core feature that help us modulate what python's doing.
Recommended Skill: Read First
I highly recommend practicing your code Reading skills on this chapter. Go line by line, trying to figure out what Python is doing there. In doing that, you're starting to get practice internalizing Flow of Control through understanding conditionals. You'll notice that order of conditionals can matter a lot, as will whether there's an else
statement ensuring that something will happen even if all the conditional statements return False
Conditionals, Booleans, and Flow of Control
Boolean values are very simple but very powerful. There are tons of useful ways to
construct expressions that evaluate to True
or False
in Python, and we use these
to change the behavior of our program.
Basic if
statements act as ‘gates’ to control whether blocks of code get executed.
elif
and else
statements enhance this control.
Some specific concepts to understand:
- Truithiness: Everything can be evaluated to either true or false. Most things are true.
try
andexcept
: expecting exceptions (also known as ‘errors’) in your code. This is super helpful for user input. Compare:
…with:
- Flow of Control: Python does things in a specific order. Sometimes it’ll not execute some code, and any runtime errors in that code won’t appear until or unless it does.
Flow of Control: What's Python Doing, and Why?
Some chapter concepts:
- Boolean Expressions. They evaluate to
True
orFalse
- Logical Operators. Specify how to turn data types of various kinds into
True
orFalse
. - Execution types:
- Conditional: only
if
- Alternative: this or that
- Chained: do one thing (
if
, followed by zero or moreelifs
, and anelse
), or do one or zero things (noelse
) - Nested: layers of conditions
- Conditional: only
- Exceptions
- One way to say to Python "I know an error might happen here. If so, do this instead of what you normally do"
- Understanding order of evaluation matters: 'short-circuiting'
Functions: Repeatable Units of Modifiable Code
Functions are awesome!
So awesome, in fact, that we'll touch on them throughout the rest of class, including the next several weeks.
So far, most of our code has been all the way to the left, unindented. if statements have indented blocks: that's the first instance we've seen of code being inside other code.
Conditionals maybe execute the code inside them. Functions use the code inside them in a different way:
- Function definitions tell Python to remember code for later. Nothing else is done when a function is defined.
- Function calls tell python to go execute the code in the function.
- Functions can be defined such that Python is expecting some extra information, arguments, when you call them. These arguments can change the behavior of the function when you call it different ways. Or, they can be defined to not need any kind of input.
- Functions can de defined to
return
something. If they do, nothing else will happen unless that return value is stored or computed upon.
Functions are a fundamental concept in programming, and a powerful tool for making programs do cool stuff.