Welcome to lesson 5, which roughly covers pages 38 to 45 of
the "Automate the Boring Stuff with Python" textbook. Now that we know about
boolean values, comparison operators, and boolean operators we can start looking at flow control statements.
The simplest flow control statements are "if" and "else".
Open up a new file editor window by clicking on File > New File and enter the following code:
name = 'Alice' if name == 'Alice':
print('Hi Alice') ...and then go back to the original indentation and
type print('Done') I'm going to save this as if_example.py and press F5 to run it.
You can see that this code prints out 'Hi Alice'
and 'Done'. Let's take a look at the if statement first.
Here's a flow chart diagram for it.
The expression in the if statement is called a condition.
"Condition" is just a name for an expression in a flow control statement
But technically a condition and expression are the same thing.
If the condition in an if statement evaluates to True,
the execution enters the indented code that follows.
If the condition is False, the execution skips the indented code.
There's a great visualization tool at pythontutor.com
that can show us what's happening in this program. I'm just going to
but technically a condition and expression are the same thing
"Visualize Execution". We can click forward to step through the program
one line at a time. So first Alice is assigned to the name variable.
Then the condition is checked, and it does evaluate to True so the execution
enters this indented code and prints 'Hi Alice'.
And then finally it leaves the block of this indented line of code... and print... and executes print('Done')
Let's go back and change this slightly. We'll change this so that 'Bob'
is assigned to name. You can see that this condition
evaluates to False now, so the latest code after it is skipped and only 'Done'
then the condition is checked, and it does evaluate to True, so the execution
enters this indented code and prints out 'Hi Alice'.
The indentation is how Python tells what part is
inside the if statement's block, and what isn't. A block begins when the indentation
increases and ends when the indentation returns to its previous level.
Blank lines are ignored for the sake of looking at what is in or not in the block.
So this is how you can tell that print('Hi Alice')
is in a block because its indentation is increased compared to the previous line.
Here's a small game program that I wrote. You don't have to pay attention to the code,
just look at the indentation and you could tell where blocks begin and end.
So you can tell, here, the indentation is increased so this is starting a new block.
And then the indentation returns back to normal here, so that's the end of the block.
This is the complete block. Right here you can tell
a new block has started and then the indentation is increased again on this line.
So this is a block inside of this block.
And then the indentation returns to its previous level so this continues on
with the previous block. And then the indentation returns back to zero here.
That's the end of this block. This is all one block,
it just contains another block inside of itself. And then looking on you can see that
this is also a block, it just contains this blog- this
one-line block here and this one-line block here.
And then another block begins right here.
Blocks are also sometimes called clauses. But just like how "condition"
is just another name for an "expression", they're the same thing. There's a nice reminder:
New blocks begin only after statements that end with a colon, like in the if
statement in our previous program.
You can also check it out right here here's a colon and it begins a new block here.
Here's another call in this statement begins a new block. This colon and
so this is a block inside 0 this block
on the indentation returns to its previous level so this continues on
with the previous blog and then the indentation returns back to zero here
let's see and I'll this block this is all one block
it just contains another block inside itself been looking on in see that
this is also a block it just contains this blog this
one-line block here in this one-line block here
and then another block begins right here
blocks are also sometimes called clauses but just like hell condition
is just another name for an expression they're the same thing there's a nice
reminder
new books begin only after statements that end with the colon look in the if
statement in our previous program
you know so check it out right here here's a call and it begins a new blog
here
here's another call in this statement begins a new block this call and
shows that a new block is starting
in our name equals equals als example the condition was false nothing happened
but we can use in
ill statement so that could run specifically when a condition is false
screwed in new program and take in the following code
password equals swordfish if password
equals equals swordfish Colin
print access granted
and hit backspace to get rid their indentation
tight else Colin print
wrong password then save this
as def health example in plain English this reader's
if password is equal to swordfish print
axis created or else print wrong password
what's copy and paste this into the online play fun to turtle
if the condition is true than the if block is executed
and the else plot is skit
but if we set it so that the condition would be false
than the
if block is skipped in the else block is executed
one and only one of the blocks will be executed
the flow chart for this program would look something like this:
you can see that any path from start to end will go through one
and only one of the blocks
so with the Italian statements wanna two blocks is executed
but you might have a case we want one of many possible blocks to execute
the belet statement is in else if statement it lets you provide
as many additional conditions to check as you need open a new file editor
window
and enter the following code: in me
was bom H equals 3,000
if name equals equals Palace
the print hi alice bill if
pages less than 12 print
they were not Alice kiddo them
bill if the page is greater than 2000
print unlike you else's
not him on dead immortal vampire
they live
be discreet than a hundred print they were not Alice grainy
at save the says
yet Ellis example in let's run this program
we can copy and paste this into the online play fun to Turtle
to see what exactly is happening here
the string bob is assigned to that name the integer 3,000 as a side
aged it's an hour at the if statement this condition is checked
and it's false so it's gets that block then this
elliff statement has its condition checked it's also falls
we skip that in here this is the first true
condition that we found to the execution enters inside
this block prince that out and then it just gets
all the other conditions you can have as many lol statements following if
statement as you need
but the order of the old statements does matter the execution
enters the first block that has a true condition the rest to the conditions
won't even be checked
you can also add Intel statement to the end of the chain
Ellis statements the L statements block will execute if
all the previous conditions have been false
worst thing about flow control statements sometimes you might see code
like this
this condition is kinda weird the name variable is set to whatever the users
take did
but input will be returning a string value not a bully in true or false value
the reason this code works the conditions can use
truth he and full C values
for Strings the blank string is a full C value
condition evaluates the blank string it's considered to be the same as the
false boolean values
all the other non play extreme values are true the values
was for running this code
entry name again taken Ellis this is thank you for entering a name
sent a non-playing string like alice is truth the
then this was considered to be a true condition so it entered
this block of code but if I run this again and
entered nothing the blank string is considered a faulty value
so this condition is considered to be false and so
this block code was executed
will this code is works and it's on a shortcut in general it's better to be
more explicit you should change this line
to be something like give name does not equal the blank string
for integers the value 0
is the full see value and every other integer value is true fee
and the 0.0 floating point value is false EE and everything else
is true T you can use the ball function
to return adequately boo in value of water-filled
value pass at the screen like the string and into functions that we've seen
before
so if we pass 02 ball been see that's a full CW
ball returns falls if we pass any other injure
returns true you can do this with string values as well
hello is the truth the value where's the black string
is a full CW
to recap if statement can be used to conditionally execute code
depending on whether or not the if statement condition is true or false
in Ellis that is in else its statement can follow in if statement and its block
executes
if the condition is true in all the previous conditions
have been falls Intel statement comes at the end
its block is executed all the previous conditions have been false
and finally the values 0 0.0
and the empty string are considered to be full C values
when used in conditions they're considered to be the same as the falls
Windows he can only see which for yourself which values are true tearful
see
bypassing them to the pool function