Python

Example Of If-Else Statement In Python – Q&A

We learned how to use Python’s if statements in the last tutorial. We shall study another control phrase, “if..else,” in this manual.

When we want to run a specific block of Python code only when a specific condition is true, we use if statements. If..else statements are essentially an extension of the “if” statement that allow us to execute alternative sets of statements depending on whether a given condition is true or false. The if..else statement can be used to achieve tasks like printing “even number” when the number is even and “odd number” when the number is not even.

Python’s if..else statement syntax

if condition:
   block_of_code_1
else:
   block_of_code_2

block_of_code_1: This would execute if the given condition is true
block_of_code_2: This would execute if the given condition is false

Python if-else example

num = 22
if num % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button