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 →


# Examples of boolean values
# Task --> compare ios and android
OS1 = 'ios'
OS2 = 'android'
# Compare OS
print(OS1 == OS2) # returns a false as ios is not android
print(OS1 is not OS2) # returns a true as ios is not android

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 →



# some more examples
# use the "and" operator & 'or' operator
drinks = "beer" # we need beer
food = "wafers" # we need wafers
# your choice -->
for_party = "mojito" # only need mojito
# is your choice similar to my choice or not
print((for_party) == (drinks or food))
#
## to check if your choice aren't equal to my choice
print((for_party) is not (drinks or food))

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.



Popular posts from this blog

What I learned from writing a guest post

                                                     Recently I wrote a python tutorial for Datacamp , while writing this tutorial I learned a couple of things which I am going to share with you right here in my blog post. May be you can use my experiences in your own endeavours as a writer. Idea The first step is idea generation. You can either search through any previous topics in your blog post if you have one or you can go through your past learning, experiences or any problem statement that you might have gone through and you will find that while doing that you can get tons of ideas to start your article. If you fall short of ideas then you can ask for help from may be your social media followings your friends or your family, for example I asked the people at Datacamp  themselves and they suggested me a couple of topics which got me st...

My new Youtube channel

New YouTube channel I always say that thecodingproject exists because of you, it exists because I want to share with you the knowledge and experience that I have gathered over time and make it available for all of you so that you can pursue your passion and love for coding. It's been my continuous endeavor to present you content that is both interesting and useful so that learning coding is no longer boring. To achieve this I have launched my first YouTube channel . Now all of my python coding tips & tricks are also available on YouTube. Visit the link and subscribe to my channel .

Expressions in Python : How to become a Python programming Jedi - A Python programming Tutorial Part 1

Long Long ago in a Galaxy far far away there was a planet named Quora. There was a growing agitation among the planet’s inhabitants to learn the way of the Python programming Force and become a master in the way of the Python programming Jedi, but very few knew that the secret art of the Jedi is embedded in a book named as TheCodingProject…. So, let’s begin the journey of learning the secret art of the Python programming Force and we shall together make the journey towards becoming the Python Jedi…. What is Python? As per Wikipedia “Python is a widely used high-level , general-purpose , interpreted , dynamic programming language .” Python is an open source programming language which was conceived in the late 1980s and its implementation began in December 1989 by Guido van Rossum (more on it here ). For us it’s sufficient to know that Python is : High level language - Python is a high-level programming language because it provides strong “:abstractio...