If statements

In Python you can define conditional statements, known as if-statements.
A block of code is executed if certain conditions are met.

Video: If statements Get PDF Get Exercises Quiz

If statements

Consider this application, it executes either the first or second code depending on the value of x.

x = 3
if x < 10:
   print("x smaller than 10")
   print("x is bigger than 10 or equal")

If you set x to be larger than 10, it will execute the second code block.   We use indentation (4 spaces) to define the blocks.

A little game:
A variable may not always be defined by the user, consider this little game:

age = 24
print "Guess my age, you have 1 chances!"
guess = int(raw_input("Guess: "))
if guess != age:

Conditional operators

A word on conditional operators

Operator Description
!= not equal
== equals
> greater than
< smaller than

Do not confuse the assignment operator (=) with the equals operator (==).


The most straightforward way to do multiple conditions is nesting:

a = 12 
b = 33
if a > 10:
    if b > 20:

This can quickly become difficult to read, consider combining 4 or 6 conditions.  Luckily Python has a solution for this, we can combine conditions using the and keyword.

guess = 24
if guess > 10 and guess < 20:
    print("In range")
    print("Out of range")

Sometimes you may want to use the or operator.

21 thoughts on “If statements

  1. Erica - November 21, 2015

    I am new to python and just learning the syntax. (I am experienced in other languages).
    What is the syntax if I want more than one line of code between if: and else:, and more than one line after else: ?

    1. Frank - November 23, 2015

      Hi Erica, simply add the lines between if and lines. Python uses indention to define the code blocks. All lines should have the same indention. By default Python imposes four spaces.

  2. Eric - September 16, 2015

    hello, I’m using a version 3.5 of Python which may be a little different from the tutorial. when I type “else” after “if”, it always says invalid syntax. I’m totally confused of that.

    1. Frank - September 16, 2015

      Hi Eric,
      Could you post the code you use? Make sure your indention is right. Python expects four spaces for every code block.

  3. Maarten Franzen - August 20, 2015

    How can you repeat this little game? age = 24

    print “Guess my age, you have 1 chances!”
    guess = int(raw_input(“Guess: “))

    if guess != age:
    print “Wrong!”
    print “Correct”

    Do i need a while statement?Iam totaly new to this..

    1. Frank - August 20, 2015

      It depends on how many repetition you want. For an if-statement, the code will be executed onlyonce. If you don’t know in advanced how many times the code will be executed, use a while loop.

  4. Will - August 19, 2015

    as soon as i type in else: and press enter, it comes up SyntaxError : invalid syntax?

    1. Frank - August 19, 2015

      You are in the python interpreter directly. Use a text editor and save the text file as program.py, then run it: python program.py
      Alternatively use an IDE such as pycharm, komodo ide, atom + plugin.

      1. Lim - August 21, 2015

        for the IDE such as pycharm, komodo ide, atom + plugin, which one is best and which one is free? Could you suggest one?

        1. Frank - August 21, 2015

          It is a matter of preference, all of them use the same Python interpreter underneath.

          Pycharm community edition is free. Atom + plugin is free. Komodo IDE is not free.
          If you like a programming environment with many windows on the screen, go with PyCharm. For a minimalist appearance, use atom + script plugin. All of the IDEs use the same python programming language underneath.

          Lately I like atom + script plugin a lot because of the minimalist appearance, it helps me to focus on the code and has a good UI. The script plugin can be installed from inside the atom IDE and programs run using the ctrl+shift+b shortcut. The PyCharm community edition runs out of the box and doesn’t need additional plugins.

          If you come from a .NET or Java background, there are Python plugins for Visual Studio, Netbeans and Eclipse.


          A full list of programming environments can be found here

  5. Neil - August 9, 2015

    after pressing the enter after writing the if block a error showing before entering the else block”IndentationError: expected an indented block”. i am using python in OS X yosemite in terminal.

    1. Staff - August 9, 2015

      Hi Neil! Python uses indention very strictly, make sure every indent (block) is 4 spaces. Tabs or another amount of spaces than 4 will raise an error. Let me know how it works out.