Skip to main content

Boolean in Python : How to become a Python Jedi - A Python programming tutorial - part 3






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 →
Operators
Results
True and True
True
True and False
False
False and True
False
True or True
True
True or False
True
False or True
False


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.



Comments

Popular posts from this blog

How To Install Tensorflow On Windows

Why I am writing this tutorial ? Recently I have started learning machine learning specifically DeepLearning for one of my pet projects, and during this quest of mine I wanted to learn TensorFlow library which is developed by Google. Learning is one thing but first I needed to install the library, but there was this problem which I faced during the installation. I am using Anaconda as my package manager and trying to install the Tensorflow library on my work computer which is behind a proxy, installing directly from the anaconda cloud while behind a proxy always displayed a proxy error. How did I resolve the issue? Well this is exactly what I am going to write down in the below tutorial. The Steps To Follow Before we get all cosy with this short tutorial on Tensorflow installation, let me tell you that this tutorial is on the assumption that you have already installed Anaconda on your machine and you know at least a few basic things or two about python. If y...

While Loop in python : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 6

Welcome back friends to another post on TheCodingProject and we are back with the sixth chapter of the Beginner Python programming tutorial - How to become a python Jedi. This time we are going to learn how to use while loop in python. The While Loop statement? In Python Programming, There are 3 types of loops → While loop For loop Nested loops Now to get into some action→ Let’s consider that two groups of aliens have landed on earth  out of these two one of the group is good & the other are the bad guys. The govt. Has given you the task of designing a system that takes in input from different eyewitnesses & then classify that whether the aliens that these eyewitnesses encounter are the good guys or bad guys. Copy & paste the above code in your editor and run this code. What do you get ? When the input is Autobots it matches the condition aliens == "Autobots" & it enters the loop to prints the statement pointed b...

For Loop in python 3 : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 7

I am back again with another part in the beginner python programming tutorial series. We will continue with "Loops" but this time we are going to learn how to use for loop in python 3.  Difference between For loop and While Loop A "For Loop" and "While Loop" have a common connection that both are looping statement, but major difference is that unlike while loop, a for loop gets executed a specified number of times. We can break down the For loop into the following components(Refer to the for loop flow chart below). for keyword variable name call to the range function colon operator clause or block of code Hovering over the flow-chart The above flow-chart is..well horrible I know, but I make my own drawings because I can't afford an artist. So, bear with me. In the above flowchart is a very basicy, laymanish representation of a for loop. The decision box contains the  For  statement along-with it's sou...