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! (By the way, the first bit of that last sentence rhymed. God, I’m such a loser.)
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, “lol, so does the variable x hold the value 2? If so, run the STATEMENTS, lol.” You may have noticed that the Python interpreter lol’d. It does that. But on a more serious note, you may have also 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!
On another note…notice how we indented the line inside of the if-statement? 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 “:”, just like line 3 ended in “:”. The unofficial standard for tabbing (indenting) your code is 4 spaces. It’s just one of those things programmers agree upon. Of course, you don’t have to, 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. I mean, I friggen’ cried when Bambi’s mother died!
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”, 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 "I want to have his babies." |
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 email me if you’ve got any questions or comments.