Python IF Statement: Exercise

Martin Ganchev 24 Apr 2023 1 min read

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:

x>3

and

y%2==0

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

y%2==0

indicates an even number, so

y%2==1

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

x<=3 

and

y%2==1

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 Introduction to Python course.

GRAB 1 OF 200 ANNUAL PLANS UP TO
72% OFF

Martin Ganchev

Instructor at 365 Data Science

Martin holds an MSc degree in Economic and Social Sciences from Bocconi University. His diverse academic and research experience combined with his friendly and explanatory approach to teaching have made him one of the most beloved instructors on our team. Some of the courses he has authored include: SQL, SQL + Tableau, SQL+Tableau+Python, Introduction to Python, Introduction to Jupyter, to name a few.

GRAB 1 OF 200 ANNUAL PLANS UP TO
72% OFF
Top