Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £35.99 £24.99. New subscribers only. T&Cs apply

Find out more

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

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now