Python3 – if , if..else, Nested if, if-elif statements

There come situations in real life when we need to do some specific task and based on some specific conditions and, we decide what should we do next. Similarly there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases, conditional statements can be used. The following are the conditional statements provided by Python.

Let us go through all of them.

if Statement

If the simple code of block is to be performed if the condition holds true than if statement is used. Here the condition mentioned holds true then the code of block runs otherwise not.

Syntax:

if condition:           
   # Statements to execute if
   # condition is true

Flowchart:-
if-statement-in-java



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# if statement example
if 10 > 5:
   print("10 greater than 5")
  
print("Program ended")

chevron_right


Output:

10 greater than 5
Program ended

Indentation(White space) is used to delimit the block of code. As shown in the above example it is mandatory to use indentation in Python3 coding.

if..else Statment

In conditional if Statement the additional block of code is merged as else statement which is performed when if condition is false.

Syntax:

if (condition):
    # Executes this block if
    # condition is true
else:
    # Executes this block if
    # condition is false

Flow Chart:-
if-else-statement

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# if..else statement example
x = 3
if x == 4:
   print("Yes")
else:
   print("No")

chevron_right


Output:



No

Example 2: You can also chain if..else statement with more than one condition.

filter_none

edit
close

play_arrow

link
brightness_4
code

# if..else chain statement 
letter = "A"
  
if letter == "B":
  print("letter is B")
    
else
      
  if letter == "C":
    print("letter is C")
      
  else:
        
    if letter == "A":
      print("letter is A")
        
    else
      print("letter isn't A, B and C")

chevron_right


Output:

letter is A

Nested if Statement

if statement can also be checked inside other if statement. This conditional statement is called nested if statement. This means that inner if condition will be checked only if outer if condition is true and by this, we can see multiple conditions to be satisfied.

Syntax:

if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here

Flow chart:-
nested-if

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Nested if statement example
num = 10
  
if num > 5:
   print("Bigger than 5")
  
   if num <= 15:
      print("Between 5 and 15")

chevron_right


Output:

Bigger than 5
Between 5 and 15

if-elif Statment

The if-elif statement is shoutcut of if..else chain.While using if-elif statement at the end else block is added which is performed if none of the above if-elif statement is true.

Syntax:-

if (condition):
    statement
elif (condition):
    statement
.
.
else:
    statement

Flow Chart:-
if-else-if-ladder

Example:-

filter_none

edit
close

play_arrow

link
brightness_4
code

# if-elif statement exmaple
  
letter = "A"
  
if letter == B:
   print("letter is B")
  
elif letter == "C":
   print("letter is C")
  
elif num == "A":
   print("letter is A")
  
else:
   print("letter isn't A, B or C")

chevron_right


Output:

letter is A



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.