Feb. 13, 2013

Python If...Elif...Else Statement

What are Conditions?

Conditions tests if a something is True or False, and it uses Boolean values
(type bool) to check that. 

You see that conditions are either True or False (with no quotes!).
2 < 5
3 > 7
x = 11
x > 10
2 * x < x
The results of these tests decides what happens next. 

When to use a Condition?

If you want to check if the user typed in the right word or to see if a number
is higher / lower than 100. 

Syntax Explained

First, lets look at Pythons if statement code block. 

Rememeber, to indicate a block of code in Python, you must indent each line
of the block by the same amount.
If ...else

    if condition:

    elif condition:

If, elif and else are keywords in Python.

A condition is a test for something ( is x less than y, is x == y etc. )

The colon (:) at the end of the if line is required. 

Statements are instructions to follow if the condition is true. 

These statements must be indented and is only being run when the if condition
is met. 
Typical conditions are: xy, x<=y, x>=y, x!=y and x==y. 

If you want more choices, you will have to include at least two conditions . 

The "else" MUST be preceded by an if test and will ONLY run when condition of
the if statement is NOT met. 

else will run if all others fail. 

If you only have two choices in your construction, use if ..else 

If there are more than two options, use if ..elif ..else.. that will make
it easier to read

elif is short for "else if"

Conditional Tests

An If statement sets an condition with one or more if statement that will be
used when a condition is met.

There can be zero or more elif parts, and the else part is optional. 

The keyword 'elif' is short for 'else if', and is useful to avoid excessive

An if ... elif ... elif ... sequence is a substitute for the switch or case
statements found in other languages.
x = raw_input("What is the time?")

if x < 10:
 print "Good morning"

elif x<12: 
 print "Soon time for lunch"

elif x<18: 
  print "Good day"

elif x<22: 
 print "Good evening"

  print "Good night"
You can also use it to control that only specified users can login to a system.
# Allowed users to login
allowed_users = ['bill', 'steve']

# Get the username from a prompt
username = raw_input("What is your login? :  ")
# Control if the user belongs to allowed_users

if username in allowed_users:
    print "Access granted"

    print "Access denied"

Python If statement Code Block

To get an easy overview of Pythons if statement code block
if :
    [do something]
elif [another statement is true]:
    [do something else]
    [do another thing]
For more reading, please see Python's official documentation. 

Share this article

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Download Our Free Guide To Learning Python

* indicates required

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.