£199.99 £139.99 for one year of Unlimited learning. Offer ends on 14 November 2022 at 23:59 (UTC). T&Cs apply

Find out more
Conditional expressions
Skip main navigation

Conditional expressions

Conditional expressions - Python
© Coventry University. CC BY-NC 4.0

Conditional expressions are our way of asking the program a question. When you write one in your program and run it, the program will decide if the answer to the question is true or false.

A conditional expression will typically contain three parts.

  1. A left operand
  2. A conditional operator
  3. A right operand

A small example of this is shown in the image below. The left operand being the 5, the conditional operator being the “>” symbol and the right operand being the 3. This just asks a very simple question – Is 5 greater than 3?

In[1]: 5>3
Out[1]: True

When that code is run, you can see in the output block that it was true. We can also store this inside a variable like so

In[2] : ExpressionVariable = 5>3
print(ExpressionVariable)

True

But ‘Greater Than’ isn’t the only conditional operator, there are many more…

Conditional operator Name Example
== Equals X == Y
!= Not Equals X != Y
< Less Than X < Y
> Greater Than X > Y
<= Less Than or Equal to X <= Y
>= Greater Than or Equal to X >= Y

Equals (==)

The Equals operator is used to check if the given operands are the same. In the example below, we have two variables, X and Y which have each been assigned a number.

If we were to use the Equals operator to check if our X variable was equal to 5, our conditional expression would resolve to True. A few more examples are shown after being run in Jupyter Notebook below.

In [1]: X = 5
Y = 10

In[2]: print(X == 5)
print(X == 6)
print(X == Y)

True
False
False

Not Equals (!=)

The Not Equals operator is the exact opposite of the Equals operator. It is used to check if the given operands are not the same and will resolve to True if not equal to each other.

In [1]: X = 5
Y = 10

In[2]: print(X != 10)
print(X != 5)
print(X != Y)

True
False
True

Less Than (<)

The Less Than operator is used to check if the Left operand is less than the Right operand.

In [1]: X = 5
Y = 10

In[2]: print(1 < 2)
print(X < 3)
print(X < Y)

True
False
True

Greater Than (>)

The ‘Greater Than’ operator is used to check if the Left operand is greater than the Right operand.

In [1]: X = 5
Y = 10

In[2]: print(10 > 2)
print(X > 15)
print(X > Y)

True
False
False

Less/Greater Than or Equal to (>=, <=)

These operators do the exact same job as the Less Than and Greater Than Operators (>,<), but they also check if the value of the left operand in the conditional expression is equal to the right operand.

In [1]: X = 5
Y = 10

In[2]: print(X <= 5)
print(X <= 6)
print(X >= Y)

True
True
False

Calculations

We can also do calculations in our conditional expression on the fly. In the below example we are checking if the sum of X and Y is equal to 15.

In [1]: X = 5
Y = 10

In[2]: print((X + Y) == 15)

Your task

Time to put this into practice.
Look at the following examples and decide whether they would be True or False if you were to run them in your program.
  • 5 > 3
  • 19 < 10
  • (10 / 5) == 2
  • (30 * 2) + 3 > 70
  • "False" != False
  • True == 1
© Coventry University. CC BY-NC 4.0
This article is from the free online

Get ready for a Masters in Data Science and AI

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education