If Statement #
The if statement or control flow statement is used to control the program's operation when it meets the specified conditions. The conditions can be any expression that evaluates to a boolean value (True or False). If the condition is True, the code block following the if statement will be executed. If the condition is False, the code block will be skipped. For example, if today is Monday, set the alarm clock to ring at 6 a.m.
For example, in the following code, when we run it, the system will display This is run. However, if we change the value from True to False, we will see that the system does not display anything.
if True:
print("This is run")
Here's how to write an if statement:
if condition:
# code block to be executed when the condition is true
For example:
def more_than_50(number):
if number > 50:
return True
return False
print(more_than_50(100)) # True
print(more_than_50(10)) # False
This code defines a function called more_than_50 that takes a single parameter number. The function first checks if the value of number is greater than 50 using an if statement. If number is greater than 50, the function returns True. If number is not greater than 50, the function returns False.
Exercise #
We have created a function is_even that takes a number as an argument. Write the function body so that the function returns True if the number is even, and returns False if the number is not even.
Tests #
is_even(0)should returnTrueis_even(15)should returnFalseis_even(50)should returnTrue
Hint #
You can check if a number is even by using the % operator. For example, 20 % 2 == 0 will be True, and 21 % 2 == 0 will be False.
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules