Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Basics!
You have completed Python Basics!
Preview
Let's explore conditional branching
Learn more
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we can do some boolean reasoning,
0:00
we should look at how to use it
to our advantage in scripts.
0:02
We make choices all the time.
0:05
If it's raining outside,
I'm gonna get an umbrella.
0:07
If I'm hungry, I'm gonna eat some food.
0:10
If it's late and I'm tired, I go to bed.
0:12
Well, unless I'm stuck on a coding
problem that I just have to fix.
0:14
I should just go to bed at that point.
0:18
The answer usually comes in my sleep.
0:19
All those scenarios
start with the word if.
0:21
And that is the first way that we are
gonna control the flow of our application.
0:24
If you want to learn more,
keep watching this video.
0:28
In our script here, we ask for
a name, we say hello,
0:33
and then we say the person
is learning Python.
0:36
That's not always necessarily true, is it?
0:40
So let's do this to fix it.
0:43
If the name inputted is actually your
name, then we'll print that message.
0:45
So in order to do that,
we're going to need to check and
0:50
see if two values are actually equal.
0:54
You can compare two objects
using the double equal sign.
0:56
Let's take a look real quick in the shell.
0:59
So here's an example.
1:04
We'll say fruit = "apples".
1:05
And so we've assigned apples to
fruit with a single equal sign.
1:09
And then we'll check the quality
by using the double equal signs.
1:13
We'll say fruit does that equal apples?
1:17
True.
1:24
And then if we compare apples to oranges
1:24
which you're not supposed to do,
you get back False.
1:28
And that's because fruit is apples and
apples do not equal oranges.
1:32
So, note how the single
equal sign is used for
1:38
assignment and the double equal
sign is used for comparison.
1:41
Accidental assigning by using just
a single equals is a super common problem.
1:46
And that's probably because when we say,
1:52
fruit equals oranges in English,
not fruit equals equals oranges.
1:54
So when comparing make sure that
you double check for double equals.
1:59
So in our code we want to only show
the learning Python bit if it's us.
2:05
So I'm gonna add a new line here.
2:11
And start it with a new keyword that you
haven't seen yet and it's called if.
2:15
So if,
then what follows next is an expression.
2:20
So let's see.
2:24
We have first name already,
so if first name is equal to,
2:26
note the double equals there,
2:31
Craig, Double-check
the double equals always.
2:34
And then, we add a colon,
which is a new character for us.
2:40
What happens is the colon opens
up the body of our if statement.
2:44
Now, the body is what will run
if this expression is true.
2:49
The way that you group the code together
that will run when the expression is true
2:54
is by indenting each line.
2:59
Now, you can indent your code
with a tab or a series of spaces.
3:00
The only requirement is that all
indentation in your file is exactly
3:05
the same.
3:10
As you can imagine,
that might cause some problems.
3:10
So in order to standardize
that decision for you,
3:13
there is a very strong movement to use
four spaces to indent your Python code.
3:17
So as you can see down here in the editor,
3:22
this says we're gonna use
spaces instead of tabs.
3:24
So we're gonna use spaces and then this
2 here is saying the number of spaces.
3:27
And I just said that we wanna use four.
3:33
So I'm gonna change the default to be 4.
3:35
Four spaces is the default in
most text editors for Python.
3:40
And now we've just changed ours.
3:44
More in the teacher's notes.
3:46
So you'll notice that when I press
Enter after this if statement, you'll
3:48
see that the cursor is automatically
indented to where the code should go.
3:53
Here you'll see that we
are on line 4 which is true.
3:57
Line 4 and we're in column
5 which is 4 spaces, right?
4:01
There's 4 line spaces and
then we start writing, awesome.
4:04
But actually what we really
want is this line here, right?
4:09
We want this line here up there.
4:13
So what I'm gonna do is I'm gonna
move to the front of this and
4:15
I'm gonna press Backspace and
4:18
you'll see that it automatically
indented into our if block there.
4:19
You can finish this grouping or
block of code as we call it, block.
4:22
You can end that by going back to
the original indentation level.
4:28
So if we come to the end here and
we press Enter.
4:32
It's waiting for us there.
4:35
I can press Backspace back here.
4:36
So let's just add a new closing line so
that we can see it.
4:39
So we'll say print,
we'll say ("Have a great day.
4:41
Let's use our format string.
4:46
We'll use the name there one more time.
4:48
And we'll say format, and
we'll pass in first_name.
4:51
Do you see how the indentation
should stops right,
4:55
it shows that this is clearly
not part of the if statement.
4:58
So let's run it and see if it's working.
5:02
I'm gonna drop out of here,
I'm gonna make sure that this is saved,
5:04
there's no red mark up there,
we'll say clear here.
5:08
Let's go ahead and say python hello.py.
5:11
We've got an error.
5:17
Let's see what's happening.
5:18
Did you spot it?
5:20
Unexpected end of file while parsing.
5:22
So, here we go, look what I forgot.
5:24
I forgot the final paren there.
5:27
So there we go.
5:29
So it was just waiting for us to finish.
5:30
That's awesome,
that's exactly the error I showed.
5:32
See, it happens.
5:34
Here we go.
5:35
What is your first name.
5:37
My name is Craig.
5:40
And so there we see,
Craig is learning Python,
5:42
because it went in to the if block and
checked it.
5:45
And now let's test the other side of it.
5:48
Let's run it again without the name
Craig or without your name.
5:50
So how about my buddy Kenneth?
5:54
He's a great teacher and Pythonista and
5:57
you'll meet him in
a future course I'm sure.
5:59
Hello Kenneth.
6:01
Have a great day Kenneth.
6:02
So there we go.
We don't have the output Kenneth
6:04
is learning Python because
our if condition here, right?
6:06
This condition first name equals
Craig Kenneth does not equal Craig, so
6:08
that was false.
6:13
You know what though,
6:14
I'm kinda feeling like everybody should
probably start learning Python, don't you?
6:16
So how about everybody that isn't you
gets a message about learning Python.
6:20
And this way you could run it for
your family or friends, and
6:26
give them a little motivation,
a little wink wink, nudge nudge.
6:29
So you can very easily do this,
and the key word is Else.
6:33
So at the same indentation level as
your if, use the keyword else and
6:37
then a colon because
elses also have bodies.
6:43
So the next body of code and
see how this indented automatically there?
6:47
We'll say print("You should
6:51
totally learn Python, {}!.
6:57
Go ahead and close that string,
format, push in our first name and
7:03
then this time I'm gonna remember
to close my print statement.
7:07
There we go.
7:11
So what happens is, we come in here,
we check the first_name,
7:13
if this is true, this runs,
otherwise, this block runs.
7:17
Do you see how the indentation really
just makes the code kinda jut out?
7:24
It's kinda like branches of a tree,
this is why it's called branching.
7:28
Now, this spacing that you see is
also referred to as white space.
7:32
One thing to do before we verify that this
is working is to talk about our final
7:36
branching keyword.
7:41
It's one of the only Python keywords
that isn't actually a word in English,
7:42
it's called elif, and it's
a combination of the words else and if.
7:47
And what this allows you to do is chain
different conditions, so let's do this.
7:51
Now I told you before that you're
not alone in your journey.
7:57
Your fellow students are taking
this course right now.
8:00
So let's go to the community and
get some names.
8:02
So here I am on the Treehouse site,
8:05
it's most likely gonna look different
cuz we're constantly improving things.
8:07
But up here in the header,
there is a link to the Community link.
8:10
And this by default is all of the topics,
but we can narrow that.
8:15
So let's narrow that down here to Python.
8:20
Let's take a look at this first one here.
8:25
Is there a reason we are not setting
the condition in the while statement?
8:27
We're not quite there yet, but
we're going to see that here in a second.
8:30
So let's go here.
8:33
Let's take a look at this,
and this is Maximilliane.
8:35
Let's grab her name here.
8:38
Let's say Maximilliane Quel,
if I can, Maximiliane.
8:40
So I'm gonna copy that.
8:45
And you'll see here,
she's gone and she's asked and
8:46
she's put some questions in here.
8:50
She got an answer back.
8:52
And there's an answer
there from Steven Parker.
8:53
And she was told good job on
what it was that she was asking.
8:55
We'll get to this here just in a bit.
8:58
So I'm gonna pop back over and we're gonna
use Maximiliane in our example here.
9:00
So if it's me,
we wanna see this is learning Python.
9:06
But if it's specifically Maximiliane,
cuz we know she's in there,
9:11
we'll say if first_name =, and I hope I'm
saying her name right, "Maximiliane".
9:17
Then we'll come in here and
9:24
will say print (first_name,
9:27
"is learning with fellow
students in the Community!
9:31
Me too!").
9:39
And there we go, we added another branch.
9:42
So let's go ahead and
let's run it for Maximiliane.
9:45
It's gonna be, First
9:48
name is M-A-X-I-M-I, Maximiliane.
9:53
Maximiliane is learning with
fellow students in the community.
9:58
Me too.
10:00
Cool, that's awesome that
you're gonna hang out there.
10:02
Helping others is a wonderful way
to make sure your learning sticks.
10:04
I'm so proud of you.
10:07
So looking at the code,
we see that it checks this if statement,
10:09
is Maximiliane equal to Craig?
10:13
That is not true.
10:15
Elsif Maximiliane is equal
to Maximiliane print this.
10:16
And because that happened,
we're not gonna hit this otherwise block.
10:20
So let's run one that does
hit the otherwise block.
10:23
Let's do that real quick.
10:25
We'll say Python, and let's use one of
our JavaScript teachers here, Treasure.
10:26
And there it says, hello, Treasure,
you should totally learn Python, Treasure!
10:35
I think she's learning Python actually,
she's kind of a polyglot,
10:39
she talks a whole bunch of these
different programming languages.
10:42
So that equality comparison operative,
the double equals,
10:45
is just one of many ways that
we can compare our objects.
10:50
So if you would like to learn more
about other comparison operators
10:54
then watch the upcoming video.
10:59
Elsif you are feeling like you want to
check out the community forum then you
11:01
totally should, else you should take
a break and then come back refreshed and
11:05
ready to compare things.
11:10
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up