IF CONDITION
IF CONDITION
In Python, the if
statement is used to perform conditional execution of code. It allows you to execute a block of code only if a certain condition is satisfied. The basic syntax of the if
statement in Python is as follows:
syntax:
if condition:# code to execute if the condition is Trueelse:# code to execute if the condition is False
FLOW CHART:
Here, condition
is an expression that evaluates to either True
or False
. If the condition
is True
, the indented code block under the if
statement is executed. If the condition
is False
, the code block under the else
statement (if present) is executed instead.
EXAMPLE CODE:
Here's an example to illustrate the usage of if
statement
x = 10
if x > 0:
print("x is positive")
elif x == 0:
print("x is zero")
else:
print("x is negative")
In this example, the condition x > 0
is True
, so the first code block is executed, and the output will be "x is positive".
You can also use logical operators (and
, or
, not
) to combine multiple conditions in the if
statement.
Comments
Post a Comment