Saturday, July 5, 2014

2.4 Basic Python Programming Boolean and Logical Expression

As for today, the tutorial will generally be about boolean expression in Python programming. We will generally touch more towards the theory and the various boolean expression that are common in Python programming.

Boolean expression, in general, gives a true or false value in Python programming. There are various operators that enable us to write a boolean expression. I will first explains what each operator stands for and after which I will illustrate them on Python.

1. == : This operator means that whether the values are equal to each other. (Don't not confuse this with the '=' assignment operator. '=' is used for assigning values to variables and not for comparing them to give a true or false value.)

2. != : This operator represents not equal. If the values in the boolean expression with this operator is not equal, the expression will be true.

3. > : This operator represents greater than. For example, if you type something like 7 > 5 in python, it will gives to a true value as 7 is indeed greater than 5.

4. < : This operator represent less than. For example, 5 < 7 in python will give you a true value.

5. >= : This operator represent greater or equal than. Therefore in this case, if you put 5 >= 5, it will give you a true value.

6. <=: This operator represent less or equal than.

7. is : This operator represent equal and exact the same, which means not only the numeric value have to be the same, the types have to be the same as well. For instance, if you enter something like "7 is 7.0", this will give you a false value in this case as one of the is an integer while another is a float.

8. is not: This represents not equal and not the same.

Now, let's look at how this will work in python.


This is a simple example I made to illustrate the purpose of each boolean expression and how do they compare numbers. In this example I limited the types of values to integers and floats, of course, you may explore other types like strings and this expression does work as well. 

It should be easy to solve as they are all primary school mathematics. So after you are done typing the code, we can run it with on command prompt if you are using windows. If you forget how does it all work, you are refer to this link for more information: Python tutorial 2.3

After you succeeded running your code on command prompt, you will see something like this appearing:


Note that the values appearing are all true or false since all of them are boolean expressions which yield only true or false values. The values here should actually tally with the ones in the comments as they are generated from your code. However, if yours have something different, please go back to your code and check the line of code that yield something different. Perhaps you may mistype the operators or the variables. This happens a lot in programming and we should have more patient and focus when we type and check on our codes. 

Right now, since most of our codes and examples are pretty short ones, we may not need to worry too much on the checking. However, when you progress on, your codes will tend to get longer and longer. It will take more than one or two days to finish checking your codes eventually. Then how do we actually reduce this time on such checking? There is this method where by we can insert a expression right in the middle of our code to check for bugs and errors. If the code works in the middle, it means the error is in the bottom half of your code. However, if the code doesn't work in the middle, it means that you probably have made something wrong in your first half of the code already. (The latter do not mean that you have done nothing wrong in the bottom half of your code as well.) Therefore, debugging and checking often takes up quite an amount of time when you have become a seasoned programmer. I have not experienced that myself but I hope I will experience debugging of my own program after I am able to write a decent program which helps to solve problems that we may face everyday. 

You might then wonder what can all these boolean variables do for us? Why do we need to learn them in the first place? 

These boolean expression can helps us in restricting your program to do a certain set of tasks when the value is true and a different set of task when the value is false. 

We will go through this in the next upcoming tutorial. For now, you may like to familiarize yourself with the different boolean operators. Try to include some strings and other types of variables into different operators and see what boolean variable you may get. Try to understand the logic behind the value and don't accept them as such. If there are anything you have your doubts with, feel free to drop a comment or a message. I will try my best to help you with it. 

I will probably do up the next tutorial later or tomorrow depending on how much time I have later. 

Well then, enjoy your dinner and your Saturday night. Play hard and have as much fun as you can before we all go back to work and school. 

Smile guys...

No comments:

Post a Comment