If statements


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

Related Course:
Complete Python Bootcamp: Go from zero to hero in Python 3

If statements

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

 
#!/usr/bin/python
 
x = 3
if x < 10:
print("x smaller than 10")
else:
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:
print("Wrong!")
else:
print("Correct")

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 (==).

Nesting

The most straightforward way to do multiple conditions is nesting:

a = 12
b = 33
 
if a &gt; 10:
if b &gt; 20:
print("Good")

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 &gt; 10 and guess &lt; 20:
print("In range")
else:
print("Out of range")

Sometimes you may want to use the or operator.

Back
Next

21 thoughts on “If statements

  1. 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.

  2. Abhi - July 11, 2015

    #!usr/bin/python
    >>> guess=20
    >>> if guess>10 and guess<20:
    print "In range"
    else:print "out of range"

  3. Ms - July 6, 2015

    please can you help me, im doing interactive fiction in class on python and its very confusing please can you tell me how to make and correctly use a rucksack/ backpack, and why the sleep function isn’t working

    1. Frank - July 6, 2015

      Hi, I think rucksack/backpack is something specific to your class. This is how to use the sleep function:

      #!/usr/bin/python
      import time
       
      print "Hello"
      time.sleep( 1 )
      print "World"
  4. Fred - July 1, 2015

    Under the “Conditional operators” section, for the sake of readability, aren’t “lt;” and “less than” supposed to be on a line by themselves?

    Right now, you’ve got “lt;” stuck to the end of “greater than”, with “smaller than” in a column all to itself, and I’m pretty sure that that wasn’t your intention.

    1. Frank - July 1, 2015

      Thanks Fred! It’s been fixed

  5. Rhino - June 23, 2015

    Continually having problems with else, invalid syntax. (using 2.7.3)

    1. Frank - June 23, 2015

      Hi, syntax errors are usually related to spaces or typos. Could you post your code?

  6. Ned h - May 30, 2015

    In the first example the second print statement should, surely, read ;x is 10 or bigger’ because the inequality test is for less than 10 not less than 11

    1. Frank - May 30, 2015

      Hi Ned, sorry my native language is not English. I updated it, thanks for the heads up!

Leave a Reply