Conditions—Python

Most of the time in programming, our programs execute one line at a time. So for example we might see something like this:

name = input('Enter your name => ')  # This statement runs first
print('Hello ', name)  # Now this statement runs next

Sometimes our programs hit a fork in the road and they need to make a decision about the direction they are going to take. Python gives us conditional statements to help make such decision.

A conditional statement is a condition that changes the course of the program’s execution based on a true or false evaluation. Let’s start with a code example to help make this more clear.

gender = 'F'
name = input('Enter your name => ')

if gender == 'F':
    print('Hello Ms. ', name)   # This code only executes for women
                                # (gender == 'F')
else:
    print ('Hello ', name)  # This code executes for anyone else
                            # (gender != 'F')

This code snippet demonstrates a conditional statement. We begin with a variable named gender that is set to the value ‘F’ for female. Ideally, if we know the gender of a person, we should offer the correct greeting for that person.

The if gender == 'F': is the conditional statement. What this statement is doing is using a true/false expression to decide if we should print ‘Hello Ms.’ instead of just the standard ‘Hello’ greeting. If our gender variable is ‘F’, we will print the ‘Hello Ms.’, otherwise we print the ‘Hello’ greeting.

We can handle multiple conditions with the elif keyword. Let’s build on our example:

gender = 'F'
name = input('Enter your name => ')

if gender == 'F':               # First check if the gender is F
    print('Hello Ms. ', name)   # This code only executes for women
                                # (gender == 'F')

elif gender == 'M':             # Now check if gender is M
    print ('Hello Mr. ', name)  # This time print a greeting for men
                                # (gender == 'M')
else:
    print ('Hello ', name)  # This code executes for anyone else
                            # (gender != 'F')

Adding the elif keyword is a way to stack up multiple conditions and define an action for each condition. Now I should point out at this time that elif and else are completely optional. So it is ok to do this:

gender = 'F'
if gender == 'F':
    print('Found female')
print ('Done checking for m/f') # At this point, the code continues
                                # executing normally

We can also check for false conditions by using the != operator.

gendar = 'F'
if gender != 'M':   # Execute the code inside of the if block if
                    # if the gender is not male
    print('Found female')
print('Done checking for m/f') # Return to normal execution
Advertisements

1 thought on “Conditions—Python”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s