Python Programming Part 2 : A Beginner's Guide to Unlocking the Power of Python
Python Programming Part 2 : A Beginner's Guide to Unlocking the Power of Python
The if statement is one of the most fundamental control flow statements in Python. It allows you to execute a block of code only if a certain condition is true. The if statement is written in down -
# THE MAIN STRUCTURE
if condition1: # code to execute if condition1 is true elif condition2: # code to execute if condition1 is false and condition2 is true else: # code to execute if both condition1 and condition2 are false
Python that allows you to execute different blocks of code based on whether a given condition is true or false.
Example - 01
python
#A_if_Condition
xyz = 25
if xyz > 10:
print("xyz is not bigger than 15")
In this example, the condition xyz > 5 is true, so the indented block of code beneath the if statement is executed, and the message "x is bigger than 5" is printed.
You can also use else and elif (short for "else if") statements to provide alternative branches:
Example - 02
#if-else statement
y = 3
if y > 5:
print("y is bigger than 5")
else:
print("y is not bigger than 5")
---------------------------------------
#if-elif-else_statement
z = 7
if z > 10:
print("z is bigger than 10")
elif z > 5:
print("z is bigger than 5 but not bigger than 10")
else:
print("z is not bigger than 5 also 10")
In the first example, if y is greater than 5, the first block of code is executed; otherwise, the block under else is executed.
In the second example, if z is greater than 10, the first block is executed. If it's not, but z is greater than 5, the second block is executed. If neither condition is true, the block under else is executed.
------------------------------------------------------------------------------------------------------
The VIdeo's Includes PDF And Drive Link - Click Here To Download
------------------------------------------------------------------------------------------------------
We are showed and tech using this pdf to understand !
[ In BANGLA ]