Given fig. is for the syntax and flowchart of the if else statement in python.
lets learn how syntax (if else statement) works from given Diagram.
Python starts script execution from top and goes to end.
After step by step successful execution of script the control falls into the if statement.
after that The flow jumps to Condition.
and begins testing the Condition
1. if the condition tested is true. then executes statements inside if body.
i.e. ''' #execute this block
# if condition is true'''

2. if the condition tested is false. then the code inside else executes i.e.
#execute this block
# if condition is false"

"statements outside if else " always executes if the condition is true or false.


Example 1: Python script to show the use of if else statement .


p=8;
q=6;
if p<q:    #test-condition
  print(p," is smaller than ",q);
else:   
  print(q," is smaller than ",p)


Output:
6 is smaller than 8
The above program illustrates the use of if else statements to check minimum of two numbers.
1. In the above script, we have initialized two variables p, q with value as 8, 6 respectively.
2. Then, we have used if p<q: with a test-expression to check which number is the smallest . We have used a relational operator < in if statement. Since the value of p is greater than q, the condition becomes false and dose execute execute code inside body of if statement.
3. Then, control transfers to the else block and executes the code or statements i.e. print(q," is smaller than ",p) whic gives output-->6 is smaller than 8
After that, the control goes outside of the block and script terminates with a successful result.


Example 1: Python script to find Given number is Even or odd.

""" Python script to find given number is even or odd using modulus (%) Operator """
x=int(input("Enter Any Number"))
if x%2==0:
  print(x," is Even")
else:
  print(x," is Odd")

Output:
Enter Any Number
5
5 is Odd
Program Explanation:
x=int(input("Enter Any Number"))=: The input function Shows message "Enter Any number" on the Console. It accepts any input as String even though we enter integer values , so we have converted this input into integer using int().
if x%2==0: this expression evaluate to false. The result of x%2 is 1. Then our expression became 1==0 which is false and skip the if block, And after that control transfer to the else block and executes print(n," is Odd") statement which displays output 5 is odd.

Previous Topic:-->> if statement in Python || Next topic:-->>if else ladder in Python.