Definition:
To check the condition in Python, if the criteria are met (true), the code inside the if block will execute.
If the condition is not true (false), then the code inside the else block will execute.
Now you will clearly understand the code logic with the help of the flowchart.
IF-ELSE Flowchart :
First step: → You will write the program
|
Second step: → Now you will check the condition (yes/no)
If you find "Yes", then
|
Execute Code
If you find "No", then
|
Skip the Code
|
End the program
Syntax:
if condition:
# code to execute if the condition is true
else:
# code to execute if the condition is false
Example:
number = 8
if number > 10:
print("Number is greater than 10")
else:
print("Number is 10 or less")
Output:
Exercise
Use the if-else statement to check whether number = 8 is greater than 10 and print the result using Python.
