FREELessons: 48Length: 7.6 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.1 Conditional Statements

Now it is time to add some more flair to our applications with conditional logic, letting us manage the flow of our application. In this lesson, I'll show you how to use the if, elif and else blocks.

2 lessons, 11:32



2.Python Building Blocks
6 lessons, 1:08:07

Introduction to the Interpreter




Standard Input and Formatting

Building a Tip Calculator

3.Controlling the Flow
7 lessons, 1:20:10

Conditional Statements

Looping With For

The Range Function

Looping With While

Creating Functions: Part 1

Creating Functions: Part 2

Building an Average Calculator

4.Common Data Structures
4 lessons, 46:49

Lists, Stacks, and Queues, Oh My!


Iterating Data Structures

Building a Sentence Analyzer

5.Application Structure
7 lessons, 1:15:12






A Special Calculator: Part 1

A Special Calculator: Part 2

7 lessons, 46:55

What Are Comprehensions?

List Comprehensions

Dictionary Comprehensions





7.File I/O
6 lessons, 48:51

File Basics

Reading Entire Files

Navigating a File

Writing to Files

Reading and Writing to Files

Reading and Writing Complex Objects

5 lessons, 43:48

Introducing the Socket

Getting a Remote IP Address

Handling Socket Errors

Create a Socket Server

Create a Socket Client

9.Connecting to Network Services
3 lessons, 34:27

Getting the Current Time With NTP

Getting Websites With HTTP

Downloading Files With FTP

1 lesson, 02:08


3.1 Conditional Statements

In this lesson, and actually, in this section, we're gonna start talking about managing the flow, or controlling the flow, of your application. We're gonna do that in a number of different ways. We're gonna do that in the form of conditional statements and looping statements and those are the foundational ways to really control the flow of the data and the logic of your application. And also we are gonna start talking about the structure of your Python code as well. So that may sound like a lot but we're gonna break it up into several pieces. So in this lesson, we're gonna begin talking about controlling the flow of your application through if statements. So an if statement is really just a way to put some logic into your application to say if something is true or if something is not true, then follow this path. And if something else is true, then follow this path, and so on and so forth. So this is really going to open up the options you have within your application. So what we talk about when we have an if statement is we want to specify some sort of condition, some sort of statement that's gonna be evaluated. And if that evaluation winds up being either true or false, depending on how you structure it, then it's going to go ahead and follow that particular path. So let's start off with an example. So let's say we have an application where we ask the user to enter in a number and that number can be whatever they want. But within our application, we ultimately want to check the value of that number to see if it's maybe within a certain range or less than a certain threshold or greater than a certain threshold, and then do something based on that. So let's see how we're gonna structure that. So the first thing we'll do is we'll create a variable and we're gonna call this number. And we're gonna set it equal to, and we're going to use our little int function again to parse the inputs that we're going to present to the user. And it's simply going to say enter a number, like that, and then we're going to get a result and we're going to parse that result into an integer. So then at this point of our application flow, number is going to be an int. So we've got that nailed down. So now we want to add in some logic to be able to maybe output something based on the value of that number, or maybe within the range that that number falls in. So let's start off with something very basic, the if statement. So we're gonna start off with the word if and then we're gonna leave a space, and then we're gonna put the condition that we want to check. So the first thing we're gonna wanna do is we're gonna wanna check to say arbitrarily if the number is say, greater than ten, then we wanna do something. Now in a number of other languages, you may be used to using curly brackets to denote scope of a certain statement. So you might see something like this, maybe a curly brace here, and then the close curly brace there or maybe on separate lines or something like that. In a lot of languages that is very common but within Python, that is not. And that's not the syntax that we're going to use. So what we're going to use instead to denote the fact that we're going to start a block of code, is going to be a colon. So the colon is going to say, all right, there's going to be a block of code after this that's going to be associated with this if statement or whatever other control statement that we're going to be using. And we'll get into some other ones in the upcoming lessons. So we'll drop down to the next line. Now, we're going to want to put in here whatever sort of logic we want to happen, if the number is greater than ten. And so we're gonna wanna do a print statement so we're gonna wanna say print. And we'll just print something out. We'll say the number is greater than ten. Something like that, okay? So that's a very basic simple application, but this is going to be incorrect, but I'm gonna show you why in just a second. So if that happens, then we wanna print that out. So let's go ahead and save this file. And we'll call this number check. And we'll make sure to save this as a Python file. So we'll save that. Now we can come back into our shell and we are going to make sure we are in the proper directory, like that. So now we can see we have our numb check file. So let's go ahead and run it. We'll say Python numb check and as you can see here we're already experiencing a problem and its saying all right, on the line that says print the numbers greater then ten, expected an indented block. So that's our first tip as to what's going on with the scope of this if statement. The problem that we've run into here is that Python is very white space specific. So what's happening here is, when it sees this colon, it's expecting that there's gonna be some sort of block after it. And that block is going to have to be indented, so in this case I'm going to indent it with a tab. So now I'm gonna save that. So as you can see here, there's no open curly braces and closing curly braces to denote scope here. Instead we have a colon and then we have the next line or lines that we want to be associated with this logic to come after the statement, all have to be indented. So let's go ahead and save this. So now if we come back and we clear this out and we run our application again, now we see that we're getting that enter a number. So I'm gonna enter in ten and we would expect nothing to happen and that's truly what happened. So now we run it again and we'll say 11, and it's gonna say the number is greater than ten, which is true. All right so there we go. Now, once again, if we wanted to do more logic underneath this, then we would need to, of course, continue to indent it. So if I duplicate this line, then I want both of these to run, but let me just show you a little trick. If I bring this back so that it's flush up with the if statement here, then this line will be considered out of scope of this block of indented code. So just to show you, I'll save that. And I'll go ahead and run my application. Again, I'm gonna enter in ten this time, which is not greater than ten. And it's going to, of course, say the number is greater than ten, which means this block, this line of code right here, was executed because it is outside the scope of this if statement because this second print line is not indented. So we would need to indent this, save it again, and if I were to rerun my application, you'll see if I run ten, we get nothing. And if I run 11, I get that print statement twice. So just remember that scope is denoted by the white space, the indenting of the lines within your application. Okay, so now we've got the if statement down. I can check conditionally to see if some condition is true. So this could be greater than, it could be less than, it could be equal to, it could be all sorts of different comparisons. Now we're gonna leave this as greater than ten. Now what if I wanted to do something if the number is greater than ten, but I also wanted to check if the number was maybe less than ten and I wanted to output something in that case. Well, then I would drop down, and we would introduce the concept of an elseif. So I want to do something if the number is greater than ten, elseif the number is less than ten, then I wanna output something else. So how do I do that? Well in Python, we don't have the concept of elseif, that's not understood by Python. The elseif is in the form of elif, so it's a little bit of an abbreviation, so it can be a little bit confusing as you're getting started but the more and more you start to work with it, you'll start to get more familiar and more comfortable with it. So now we wanna check to see if elseif the number is say, less than ten. And once again, we're gonna use our colon and we're going to use some indenting here, and then we're gonna say print, and we'll say the number is less than ten. So there, we have that now. We'll go ahead and save it. So now this is going to the flow of our application will come down into these if blocks here and it will check the first one to see is the number greater then ten? If it is, it will print this. Elseif the number is less then ten, it will run this. So we'll make sure that's saved and we'll come back in here and we'll do numb check and this time we're gonna run one and we see, the number is less then ten. And then if we enter in 12, we see that is greater than ten, but if we enter in ten, we get nothing. And that's not something that we may want, we might wanna have some logic in there again. So now we have a couple different options here. We could do this in the form of another elseif. We could say elseif number is equal to ten, then we want to print the number is ten. Just like this, save that. Now this is definitely an option. So let's go ahead and run this just to verify. So we can say, all right, enter a number one is less than ten, 11 is greater than ten and then ultimately ten, the number is ten. Okay, so this has definitely allowed us to add in some functionality and some conditional statements here. But if you think about this logically, we see that if the number is greater than ten, we print something out. If the number is less than ten, then we print something out. But then what is the other option? Sure it can be equal to ten but what other options are there? There's only three options when we're dealing with comparisons like this. It's either greater than ten, it's less than ten, and if neither one of those are true, then the number has to be ten and that's just kinda the way these conditional checks go. So maybe you might think to yourself, well, then at this point, checking to see if this number is equal to ten is a little bit excessive because if it gets this far into the flow, then we know its gonna be ten. So we don't have to necessarily use the elseif. We can use the final conditional statement here which is then else, so we can say else the numbers equal to ten, and that's just logic. That's just saying it's not greater than ten, it's not less than ten, then it must be ten. So I can save that, come back and run our application again. And I can say one, the number is less than ten, 11, it's greater than ten, and then finally ten, the number is ten. So as you can see, using just a few simple keywords if, elseif or elif, and else, we can very easily start to control the flow of our application to insert logic to handle certain scenarios within our application. We've also learned that when we want to denote scope or blocks of code within our application, depending on these conditional statements, we need to introduce the colon at the end of the line. And then on the next line, we need to indent. That indentation needs to be there, otherwise, we're going to get some problems. So one other thing I want to show you very quickly before we start to move on to the next lesson of conditional statements, is if what if I didn't tab this? What if I took it back to the front of the line and indented it with just a single space? Let's go ahead and save that. If I'll come back in here, we'll clear all this out and we'll run our application again. It's going to say enter a number, and I can at this point, I will say 11, and it still works. So this is just something to bear in mind. Like depending on how you are structuring your application, there is really no specification on how far these things need to get indented. But you could probably, at this point start, to understand that if I were to write my application where I had all these indentions all over the place like this, it's probably gonna be very hard for somebody reading this to understand what's happening. That's why if you're going to indent in a certain way, then I advise you to always be consistent. So the habit that I have gotten into is that when I start to write my applications, if I start to create this type of scope, I typically always tab or set up my text editor to always do a certain number of spaces, maybe four spaces or five spaces or whatever it is you are comfortable with. But all I'm saying is be consistent, cuz that way you're going to create much more readable code and much more maintainable code. So now that we have these if statements in our tool belt, let's go ahead move on to the next sort of flow statements where we start to talk about ranges and loops.

Back to the top