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.

1
2
3
englishGrade = "D"
If(englishGrade == "D"):
	print ("You are going to Level 2")

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.

1
2
3
4
5
6
7
age = 16
if(age => 18):
    print("This person is an adult")
    else
    {
        print("This person is a teenager")
    }

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.

1
2
3
4
5
6
7
gotJob = True
if(gotJob == True):
    print("Woohoo you are going places")
elif(gotJob == False):
    print("Hang in there buddy")
else:
    print("Write your own destiny")

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.

1
2
3
4
5
6
7
8
gotJob = True
Single = False
if(gotJob == True and Single == False):
    print("Woohoo you are a real player you know.")
elif(gotJob == False and Single == True):
    print("Hang in there buddy")
else:
    print("Write your own destiny")

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.

1
2
3
4
5
6
7
8
9
10
11
gotJob = True
single = False
gotHouse = True
if(gotJob == True and single == False and gotHouse == True):
    print("you got it sorted")
elif(gotHouse == False and gotJob == True and single == False):
    print("almost there, be patient")
elif(gotJob == False and gotHouse == False and single == False):
    print("Don't worry be happy")
else:
    print("get off your bum and get working")

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.

1
2
3
4
5
6
7
isSnowing = True
isRaining = True
isCold = False
if (isSnowing == True or isRaining == True or isCold == True):
    print("Wear a sweater")
else:
    print("Summer time babyeh")

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.

Tags: , , , ,