How to Train Your Python: Part 5, Intro to Control Flow and Booleans
Well, first things firsts, it's been awhile! I'm sorry the series has been dead for a little bit, but I recently hit some personal relationship issues and needed to take some time to think. But, now that it's all said and done, the dust can settle and we can start training again!
So, when we're creating a script, we'll need to be able to control what goes in and out, this is known as control flow. We decide what the user can put in, and we decide what the user can get out, plain and simple. We'll be going deeper into control flow in the next article but this is just to get a feel for the concept.
So, we've already defined control flow as a way to govern what goes in and out of our script, but how would we do that? Now don't worry if you don't understand all of this as of right now, it'll all make much better sense after the next few articles, so until those are published, you can use this as a control point for gathering all your acquired information about control flow.
There are many ways to control what goes in and out, and one of the biggest ways of doing this is if statements. These let us test for a certain value and do what we want based on the result. This is where the term control flow comes from, we can control the flow of what is happening and make decisions based on what we want. Now, let's get into booleans and see their association to control flow.
Do you remember back in math class when you solved inequalities, the greater than, less than, or equal to's? We use these in control flow extremely often. It all boils down to something being True or False (In python, True and False are capitalized, so be careful). We can use booleans to test whether a statement is True or False. Let's list some of the basic booleans we use often and what they stand for...
- == - Stands for equal to.
- != - Stands for is NOT equal to.
- > - Stands for greater than.
- < - Stands for less than.
- >= - Stands for greater than or equal to.
- <= - Stands for less than or equal to.
These are all the basic booleans we'll be using very often. We use booleans for comparing two or more things against each other. When we do this, it will return either True or False, this allows us to do a multitude of things.
In order to get a feel for booleans, we'll use 3 of then here today, we'll start with "==", which stands for equal to, we'll use ">=", which stands for greater than or equal to, and we'll use "!=", which stands for is NOT equal to. So, let's start with "=="...
We can use this to compare both strings and numbers, but you should be careful. As we've already said, python is case sensitive, this can drastically effect the legitimacy of our scripts if left neglected. Let's see this boolean in action...
So, here we've set the test variable equal to the string "Null-Byte" make note of the capital letters as they effect the outcome. We then run a comparison of test and the string "null-byte" with lowercase letters, because python is case sensitive, these string aren't technically the same, so the answer is False. But, if we use the .lower() string method on test, we can make all of it's characters lowercase, so when we compare it to "null-byte", it returns True.
Now this boolean can also we used with numbers, let's have a quick demonstration...
There isn't really anything special here, just a demonstration that this boolean also works with numbers. Now let's move on to the not equal to boolean...
We've set the number variable equal to the integer of 1337, we then compared that number with 42, and since we're using "!=", and they aren't the same, the answer is True. We then compare the test variable from earlier with the string "null-byte", since they aren't technically the same string, it returns True.
For the final boolean I've chosen to cover greater than or equal to (>=). Just keep in mind that the number comparison booleans tend to work the same as the rest. So, let's quickly use this boolean to compare some numbers...
First, we compare our number variable to 42, and since our number is bigger, it returns True. We then compare our number to it's own value, and the answer is still True, this is due to it being a greater than or equal to boolean, the standard > and < booleans would've returned False in this situation, but our's returned True.
There we have it! Booleans. Now I know this might seem like a useless thing to do at this point, but trust me, this becomes way more important later, this is the backbone of control flow, which is the way we dictate our scripts.
Make your own comparisons! As a side note, you can also chain booleans together, so don't be afraid to experiment! Leave your resulting code in the comments below!
That's it for this iteration of how to train your python, I hope you'll join us next time to learn more about control flow!
Sorry it took so long for this series to pick up again, I needed some time to think about a few things and hit some bumps along the way, but no worries, we will continue training your python!
Thanks for reading!