Skip to main content

If else in python : Beginners Python Programming Tutorial - How to become a Python Jedi - Part 5

Welcome back friends to another post on TheCodingProject and we are back with the fourth chapter of the Beginner Python programming tutorial - How to become a python Jedi. This time we are going to learn about decision making in python using the If Else statement in python.

The If-Else statement?
In Python Programming, decision making is done by using a conditional statement like the If-Else statement. An If statement consists of 3 parts →
  • If keyword
  • a conditional operator
  • the values which needs comparison

Now it should be kept in mind that the only values that an If condition returns are a True or a False , Using this True or a False we can decide if the condition that we are trying check is right or wrong.
When Coding knowledge came to my rescue→
Let me tell you a story about how coding knowledge came to rescue . I have this neighbour named Alex who doesn't quite seem to be a regular guy. Wait ! actually he never seemed to me as a regular human 😟. This one day I was visiting him at his home for some work when I noticed something unusual. His living room is decorated with his family photographs, he has the photographs of his entire family tree over there starting from his great grandfather down to him. The weird thing that I noticed was that all the men in these photographs looked similar to him as if he himself was present throughout his family history. Is Alex a Vampire ? 😓 With this frightening thought in mind I ran back to my house and jumped straight to the computer and started to code. Using my superhuman brain I made a python program which would hack into his personal computer and collect all his personal information and then the program would calculate his age & use that knowledge to decide if Alex was really a Vampire. Now, that program is highly confidential so I won’t show you my entire code but as you guys are so much interested I will show you the snippet that is really pivotal in deciding Alex’s truth →

Copy & paste the above code in your editor and run this code. In the above code we have an If condition & an else condition. Let’s assume we have the age as 100. Now when the above code runs the code touches the If condition & checks if age is greater than or equal to 0 or age is less than or equal to 99. Notice that we have used an or operator so the condition becomes True even if only one of the condition matches & as age is 100 ( greater than 0) so the If condition returns a True & the control enters the If statement to print the statement inside the print method.

Let’s modify our If-Else statement to make it more robust. Copy and paste the below code in your editor and give it a spin→

As you can see this time we have an and operator instead of an or. So this time our If condition will return True  only when age is greater than or equal to 0 and less than or equal to 99. Now as we have the value of age out of the bounds of 0 to 99 so the control skips the If condition & jumps to the line of code immediately after that which is the Elif  statement. This kind of expression is known as a  Nested if-else statement, wherein we have another If condition immediately after the first If condition to put a second conditional check and in python programming language it’s written as Elif. In our case the control checks that age is not satisfying even the condition inside the nested If statement so it skips that too and jumps to the next condition i.e the Else  statement and as there are no further condition defined for this else statement, so it returns a True & the statement inside the print method is printed.

You can define n number of rules using the nested if-else conditional statements. Go ahead try to extend on the above example code by implementing your own rules with nested if-else statements. The code examples can be found in the Python Decision file in my Github repo.

I have tried my best to explain how to use if else in python, But if you really want to get deeper into it then checkout this book to get started with learning python in a very interactive manner --> Automate the Boring Stuff with Python: Practical Programming for Total Beginners

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

Subscribe to my youtube channel to watch the coding in this tutorial in action.

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.

Take me to Python Tutorial - Part 4                                        Take me to Python Tutorial - Part 6


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 you want to learn basic pyt…
Programmer’s skill set

Hey guys welcome to another article of Thecodingproject. Today I will share an answer to another interesting question for you budding programmers out there. When I first started learning to code this same question came to mind time and again and the question was what skills sets I should posses as a programmer ? What tool set should I start collecting in my coding toolbox ?

A programming language is just an ends to meet the automation/programming goal of a software engineer. So, I would like to touch base on the skills required by an automation engineer irrespective of the language skill set since it would be applicable to every type of programming language be it JAVA, R , Python etc. So, let's see what skills we need to have as a automation engineer-

Logical skills- This is a rather obvious skill that every programmer should have because a even writing the first line of code requires logical thinking so that you can frame your code around the correct logic. Des…

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 keywordvariable namecall to the range functioncolon operatorclause 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 soulmate i.e the Range function. As long as the variable I …