Conditional Statements
This tutorial was written for Python pre 3.0. A 3.0+ version of all my Python tutorials is planned.
If you haven’t read the previous tutorial located here, then do so! You may have some trouble following this section if you don’t. This tutorial is aimed at people who have never programmed before. If you have, but have never touched Python, then it may be worthwhile to see what this section is all about. Anyhoo, without further ado, let’s begin!
The programs we wrote earlier weren’t very interesting, were they? Of course not. No one in their right mind would think a program displaying “Hello, world!” is worthy of anything. So in this tutorial I will be teaching you about a very important and fundamental programming concept called the conditional. You could probably already guess what this is all about. Basically, we’re going to give the programs we write the ability to make choices. Conditionals are often referred to as if-statements because of this, and because the word if is found in the language syntax. So, lets take a look-see at exactly how to make a conditional statement in Python.
1 2 | if CONDITION:
STATEMENTS |
Seems pretty straight forward, but let’s analyse it for the hell of it. Please keep the indentation in mind, I’ll be talking about it later on. What we put for CONDITION determines what kind of choice (or choices) Python will be making. For example, instead of CONDITION we could write x == 2. So, when x == 2 runs in the Python interpreter, Python will ask, “so does the variable x hold the value 2? If so, run the STATEMENTS.” You may have noticed that we used a double equals sign in x == 2. This is how we compare variables. Using a single equals sign in an if-statement is a bad idea. So don’t do it. Moving along, we come to the STATEMENTS part. What we put under STATEMENTS determines what Python will do only if the variable x is equal to 2. If the variable x does not equal 2 then whatever we put instead of STATEMENTS will not run.
Let’s make a program that outputs “x equals 2″, if the variable x is 2.
1 2 3 4 | x = 3 if x == 2: print "The variable x equals 2!" |
Quiz time! Answer quickly! Does the above program display what we want!? What’s that? Yes? Ugh… I have obviously failed as a teacher. The above if-statement does not run! Take a look at the top line. The variable x currently holds the value 3. The if-statement asks, “does the variable x have the value 2? No? What? It holds a 3? Well, then I refuse to run my statements. I need a 2, not a 3. Hmph.” But, if we changed line 1 to read x = 2 instead, the if-statement will run it’s contents. It’s that simple. If this all seems elementary to you, then you were born to program, my friend!
If we wanted the code to do something after, we could always use the elif statement. elif stands for “else if”. You can see it in action below.
1 2 3 4 5 6 7 8 | x = 3 if x == 1: print "The variable x equals 1!" elif x == 2: print "The variable x equals 2!" elif x == 3: print "The variable x equals 3!" |
You can use as many elifs as you want. But this seems a little…inefficient. Don’t you think? What if we had to check against 100 numbers? We would be writing code all day! What we need is a way to make sure all cases are exhausted. We can do this by checking only for the important cases and then using the else statement. You can see an example of this below.
1 2 3 4 5 6 | x = 3 if x == 2: print "The variable x equals 2!" else: print "The variable x does not equal 2!" |
On another note…did you notice all the indentations in the code? You indent all blocks of code in Python, unless you want an error that is. How can you tell something is the beginning of a block of code in Python? It’ll usually end with a “:”. The unofficial standard for tabbing (indenting) your code is 4 spaces. It’s just one of those things programmers agree upon. Of course, your tabs don’t have to be 4 spaces, but then you’ll never fit in with the rest of the programmers. So don’t hang around me. I still want to be cool like everyone else. What’s that? Programmers were never cool to begin with? Don’t tell a programmer that. Contrary to popular belief, we programmers are kind and emotional people.
And that’s pretty much it for the conditional! Now that you understand the basics, here are a few more examples:
This is how to check if one variable is greater than or equal to another. To make it just “greater than” and not “greater than or equal to” like in the example below, remove the equals sign:
1 2 3 4 5 | x = 5 y = 2 if x >= y: print "x is greater than or equal to y." |
The code below will only run if both x and y are equal to 2:
1 2 3 4 5 | x = 2 y = 2 if x == 2 and y ==2: print "Both x and y are equal to 2." |
And finally, the code below will only run if x does not equal 2.
1 2 3 4 | x = 2 if x != 2: print "x is not equal to 2." |
Actually, I’ve got one more. You can even compare strings!
1 2 3 4 | x = "Aleks is awesome" if x == "Aleks is awesome": print "Aleks has an ego the size of Texas." |
You will learn a more “correct” way of comparing strings later. But hey, this works too.
Conclusion
And there you have it. The conditional. Such a simple and yet such a fundamental programming concept…and now you understand it. It’s also worth noting that you can literally compare almost anything. So keep that in mind. By now you should be able to create really simple programs…that have almost no functionality. But that’s okay because by the time you finish these tutorials you’ll be speaking Python, “HSSSS! HSSSS!” Sorry, that was lame.
I hope you enjoyed the tutorial! Don’t hesitate to comment below!
Comments
and elseif statement? Switch statements?
This tutorial was really meant as an introduction to the concept. You seem to know a lot more about the subject than a nonprogrammer does.
I’ve added an example with elif. I really don’t know how I missed that. As for “switch” statements. Please visit this link to see why they aren’t included in this tutorial.
I’ll just reiterate for any future commenters. These “Basics” tutorials are really meant for nonprogrammers. I realize that I’m leaving out a lot of stuff. I assure you, everything that was left out was a conscious decision made by me (leaving out elif was a mistake on my part! That shouldn’t have been left out.). I have my reasons, and more often than not that reason has to do with trying not to make the nonprogrammer’s life more complicated than it already is!
Cheers that was helpful for my number guessing game