EMAIL SUPPORT

dclessons@dclessons.com

LOCATION

AU

Conditional Statements - Python

Conditional Statements - Python

Control Statements:

Control Statements are those statements whish control or change the flow of execution. There are large number of control statements used in python, but here we are going to use some of them related to out topic.

  • If Statements
  • If ---- Else Statements
  • If…elif….else statements

If Statements

In this statements, the condition mentioned at If will be tested, and if this condition is true, the statements related to If will be executed.

Syntax of If Statements is:

If Condition:

    Statements

If the condition is true, then the statements given after (:) are executed. One or more statements can also be written after: .

Let’s understand the If Statements using one simple example.

Example 1: Write a program to display a group of message when any condition is true.  

str = 'dclessons'                                                                                                                                                      if str == 'dclessons':                                                                                                                                                      print('dclessons')                                                                                                                                                      print('this is online portal')                                                                                                                                        print('learn latest technologies')                                                                                                                            Output:===========================                                                                                                          dclessons                                                                                                                                                                this is online portal                                                                                                                                                  learn latest technologies

How to use Indentation:

Indentation are the spaces which are used in the beginning of any statements. The statements with same spaces or indentation belong to same group or suite. By default Python uses 4 spaces which can be increased or decreased by programmer.

Lets understand the Indentation in If ….Statements

If x == 1:                                                                                                                                                                  ----print(‘a’)                                                                                                                                                              ----print(‘b’)                                                                                                                                                                ----if Y == 12                                                                                                                                                             --------print(‘c’)                                                                                                                                                           --------print(‘d’)                                                                                                                                                          print(‘exit’)

Lets understand the rule of Indentation here

If x ==1:

    Print(‘exit’)

The above two statements belong to same group , as they don’t have any spaces before them. So if X==1 is false , then it will print exit.

Now the below indentation have 4 spaces before it and are part of same group, as mentioned below.

Print(‘a’)

Print(‘b’)

If Y == 12

The above Indentation belong to same group and are executed at same level. These statements are inside If x == 1 statement and if this condition is true then the above three statements will be executed.

Now if the condition if Y == 12 is true then the next two statements will be executed.

The If …. else Statements

If and else statements executes the group of Statements, When the condition of If is true, it’s executes it related statements and if the condition is false, Python jumps to else condition and based on result, its related statements are executed.


Comment

    You are will be the first.

LEAVE A COMMENT

Please login here to comment.