🛠️ Scheduled Maintenance | We’ll be undergoing scheduled maintenance and upgrades between 00:00 PST Jan 26th until 00:00 PST Jan 28th. There may be brief interruption of services in that period. We apologize for the inconvenience.


Python IF Statement Exercise

Exercises 1 Question
Blog / Exercises / Python IF Statement Exercise

IF Statement Exercise: 

Assign 8 to the variable x and 15 to the variable y.

In the same cell, create 2 conditional statements.

Let the first one print “At least one of the conditions is satisfied.” if x is greater than 3 or y is even.

Let the second one print “Neither condition is satisfied.” if x is less than or equal to 3 and y is odd.

Change the values assigned to x and y and re-run the cell to verify your code still works.

The tutorial you may need: Learning How to Use Conditionals in Python

python solution, if statement, python if statement exercise

Solution to the IF statement exercise:

After learning about IF statements with multiple conditions in this tutorial, you should have the necessary skills to solve the task in front of you.

This exercise can be divided into two subtasks.

For the first part, we begin by introducing the first IF statement we need. Remember that in Python operations, the ‘%’ sign gives us the remainder after doing integer division. Therefore, if

y%2 == 0

then y must be even.

Knowing this, the two conditions for the first IF statement should be as follows:




Since only one of the conditions needs to be satisfied, we insert ‘or’ between them when writing our code. This leaves us with the following line

if x>3 or y%2==0:

Great! On the next line, we write the message to be displayed on the screen using the print function. Don’t forget to indent the line, so that it is only ever called if we satisfy the conditions of the IF statement.

if x > 3 or y % 2 == 0:

    print ("At least one of the conditions is satisfied.")

Now for the second part of the exercise.

We already know that


indicates an even number, so


should mean that y is odd. Knowing this, our two conditions are as follows:



]code lang="py"]y%2==1[/code]

Since we need both conditions to be true simultaneously, we need to add ‘and’ between them. Thus, our IF statement becomes

if x<=3 and y%2==1:

Before we are done we need to call the print-function with the appropriate message, just like we did for the first IF statement.


if x <= 3 and y % 2 ==1:

    print ("Neither condition is satisfied.")


To check whether our work is accurate, we should define x and y and assign them values. We must make sure to do that before running the IF statements. Otherwise, we‘ll receive an error message.

And that is all.  Not too complicated, right?

Want to practice some more IF statements? Be sure to check out our other exercises!

Earn your Data Science Degree

Expert instructions, unmatched support and a verified certificate upon completion!

Leave a Reply

Your email address will not be published.