python logo

if statement python


Python hosting: Host, run, and code Python in the cloud!

In Python, conditional statements allow you to control the flow of your program based on specific conditions. These conditional statements are commonly referred to as if-statements.
When an if-statement is evaluated as true, the corresponding block of code is executed.

Related Course: Python Programming Bootcamp: Go from zero to hero

Understanding If Statements in Python

To better grasp the concept, let’s look at an example. Depending on the value of the variable x, either the first or the second block of code will execute.

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

By changing the value of x, you can control which block of code gets executed. Note that in Python, we use indentation (typically 4 spaces) to define code blocks.

Try it Out:
Variables in Python don’t always have to be hardcoded. For instance, consider this simple game where the user has to guess an age:

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

if guess != age:
print("Wrong!")
else:
print("Correct")

Understanding Conditional Operators

When working with if-statements, you’ll often rely on conditional operators to evaluate conditions.

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

It’s essential to understand the difference between the assignment operator (=) and the equals operator (==).

Delving into Nested Conditions

For scenarios where you need to evaluate multiple conditions, nested if-statements can be used:

a = 12
b = 33

if a > 10:
if b > 20:
print("Good")

While nesting is effective, it can make code harder to read, especially with many conditions. Thankfully, Python offers a more concise approach using the and keyword to combine conditions.

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

In some cases, you might also find the or operator useful, allowing for either of the conditions to be true for the code block to execute.


Enhance your Python skills further with these Downloadable Python Exercises.

← Previous | Next →






Leave a Reply:




Ned h Sat, 30 May 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

Frank Sat, 30 May 2015

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

Rhino Tue, 23 Jun 2015

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

Frank Tue, 23 Jun 2015

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

Fred Wed, 01 Jul 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.

Frank Wed, 01 Jul 2015

Thanks Fred! It's been fixed

Ms Mon, 06 Jul 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

Frank Mon, 06 Jul 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"
Abhi Sat, 11 Jul 2015

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

Neil Sun, 09 Aug 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.

Staff Sun, 09 Aug 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.

Will Wed, 19 Aug 2015

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

Frank Wed, 19 Aug 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.

Maarten Franzen Thu, 20 Aug 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!"
else:
print "Correct"

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

Frank Thu, 20 Aug 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.

Lim Fri, 21 Aug 2015

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

Frank Fri, 21 Aug 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.

https://atom.io/
http://komodoide.com/
https://www.jetbrains.com/pycharm/

A full list of programming environments can be found here
https://wiki.python.org/moin/IntegratedDevelopmentEnvironments

Eric Wed, 16 Sep 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.

Frank Wed, 16 Sep 2015

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

Erica Sat, 21 Nov 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: ?

Frank Mon, 23 Nov 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.