Python Selection – If statements

In every programming language you need to know these three very important parts of programming which are selection, iteration and sequence. These 3 parts of programming are used very frequently and we need know them before we can utilise them in our programming. When it comes to learning about programming try it out, make mistakes and learn from them. Its not expected that you will be able to do all the things the first time around. You will need time to learn them all so be patient and don’t be so hard on yourself.

Selection is the logical IF or Switch statement. In selection we are given a chance to make a decision in programming such as If its raining outside take an umbrella and if its not then don’t take it. In programming we can justify a decision by giving the program some parameters to follow once a decision has been reached.

In the code abode we are checking to see a students English grade. If they have a D in English then we can put him or her on level 2 course. Now try changing the grade to C and run the code.

You have realised that the program will not do anything because it doesn’t have any action to take if the grade isn’t D.

Take a look at the example below for another If statement. In this one we include an additional condition which only runs if that first given condition is not met.

One of if statements partner in crime is else. This you can imagine sets a default value for the if statement when the primary condition is not met. Above example is pretty easy to understand try it out.

There are many more conditions we can add to the if statement. for example we can have another else if condition tagged in there for secondary condition to run if that is met. Lets look at the example below.

In the example above we are using else if ELIF secondary condition to the if statement. To break down what we wrote here in plain English we have create a Boolean variable called got job and given a value of true. We are checking in the first condition if the person has a job then we are responding appropriately to it.

Now notice in the ELIF part of it, it says got job equals to false. Now in the secondary condition we are checking if that person doesn’t have a job then we can give them some motivation to start looking for once. Finally the else statement just states write your own destiny if that got job variable doesn’t have a value in it all together. Try changing it to true, then to false and then to nothing and see if all that runs as planned.

Now I’m sure there are plenty of ideas going through your head about if statements and let me tell you that you are most likely going to be able to do all of them. Now if you are thinking we have covered all that there is on if statements well may be not so much just yet. There are few gems lets to discover in this section. Meet AND in if statement. Check the example below for some mind melting.

This is a similar example to the once we have covered before just with a little improvements. We have added a new variable called single and given a value of false. Now inside the if statement we are able to check both of these conditions simultaneously. If this certain person has a job and is not single then hey we are very happy for them. A little clap and smile goes a long way. Then we are checking if that person doesn’t have a job and is single, then we should encourage them to turn their lives around. Finally if none of the conditions are met we simply give them a ZEN quote from anywhere.

What is happening here is we are checking both variables using AND keyword in python. Inside the if statement we can use this keyword and achieve tremendous results. We can check more than just two. True adding another one to the list and see if you can achieve the same results.

See the example below for multiple AND in an if statement.

Now that you have seen the mighty power of IF statement… Wait there is one more I have to show you. It’s the OR condition to be used in if statements. To understand the OR statement think of it this way, if it’s raining or snowing or its cold outside we will need to wear a sweater. Let’s look at this example if code.

If one of these condition is met, it will return the value “Wear a sweater” if it returns nothing TRUE then it will return the else statement.

We used simple example to explain the complex mechanisms of IF statement. Take this light heartedly don’t worry if it seems too simple, There are other full project tutorials you can try these on.

Comments are closed.