Monday, 1 May 2017

How to become a Python Jedi - A Python programming tutorial - part 3(Boolean operators)

Welcome back friends to another episode of TheCodingProject and we are back with the third chapter of the Python programming tutorial - How to become a python Jedi. This time we are going to get a taste of boolean operators.

What are Boolean Operators ?
Computers don’t understand the common human language, they only speak and understand machine level language and this language is made up of only two numbers 0 & 1. These numbers are known as binary numbers & this language is known as binary language. Now if you are a crazy nerd like me and you wanna know more then dive in here. The numbers 0 & 1 also represent decision making as they also represent True (for 0) & False (for 1) and these two operators are known as boolean operators.

Enough talk man, I’m here to learn code!!😠
Okay Okay I just took a trip in my emotion boat. I know you want to learn code so let’s just cut the talk talk & dive to code code. Yayyy!!
Boolean operators in action
Copy the below code and paste it in pycharm and any other editor that you have code →

What the above code gives us ? We remember from above section about boolean operators that they represent True (for 0) & False (for 1) and in the above example for the first print statement when we try to compare OS1 with OS2 the python interpreter checks that IOS(OS1) is not equal to Android(OS2) so the interpreter returns a False. In the second print statement we are checking if ‘IOS’ is not equal to ‘Android’ which resolves to true and that’s why the second print statement returns True.
We shall now look into some more examples. Copy and paste the below code in your editor and run the code →

Let’s say you and me are to arrange a party tonight and we list down what we need for the party but we have a difference of opinion and to come to a conclusion we turn to the geeky method of decision making and that’s through python. We list down our choices in the following variables → my choices are listed in drinks and food, your choices are listed in for_party. Now, we check that is your choice similar to my choice? The first print statement gives us a False because the values mojito is not equal to the either drinks or food. You have noticed that we have used the boolean operator or in the first statement which tells the interpreter to check if the value of variable drinks or value of variable food is equal to the value of variable for_party. Run the second print statement as well and analyse why we get a True over there. Here’s a hint for you → Notice the is not statement.

To summarize what we have learned through the above examples just go through the below table, it is a kind of cheat sheet during the usage of boolean operators →
True and True
True and False
False and True
True or True
True or False
False or True

So, folks this was all for this week but we shall return with another part of this tutorial with some new concepts to learn.

Get the code files for this tutorial in Github.

Subscribe to my YouTube channel to watch new python programming tips and tutorials.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.

No comments:

Post a Comment