š ļø 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
/ / 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

## 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.

`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

`]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!