if else Statement in Python | Python Notes & Assignment | Digital Community

 Decision Control Statements

 

if else Statement in Python

 

In this post, we are going to discuss Decision Control Statements in Python but before these, if You are not seen our video on this topic please see it first, then you will understand the concepts very well.


 

 

·      The if Statement

 

·      Concept of Indentation

 

·      The if-else Statement

 

·      The if - elif - else Statement

 

Decision Control Statements in Python are those statements which decide the execution flow of our program.

 

In other words, they allow us to decide whether a particular part of our program should run or not based on certain conditions.

 

The 4 decision control statements in Python are:

 

  if

  if….else

  if…elif…else

  nested if

 

 

The if Statement

 

The if the statement in Python is similar to other languages like in Java, C, C++, etc.

 

It is used to decide whether a certain statement or block of statements will be executed or not.

 

If a certain condition is true then a block of statement is executed otherwise not.

 

  Syntax:

                                                if (expression):

                                                            statement1

                                                            statement2

                                                            .

                                                            .

                                                            statement...n

 

  Some Important Points:

 

§  Python does not use { } to define the body of a code block, rather it uses indentation.

 

§  A code block starts with indentation and ends with the first unindented line.

 

§  The amount of indentation is up to the programmer, but he/she must be consistent throughout that block.

 

§  The colon after if( ) condition is important and is a part of the syntax. However, parenthesis with the condition is optional

 

 

The if – else Statement

 

  The if … else statement evaluates test expression and will execute the body of if only when the test condition is True.

 

  If the condition is False, the body of else is executed.

 

  Indentation is used to separate the blocks.

 

  Syntax:

                                                if (expression):

 

                                                            statement 1

                                                            statement 2

                                                else:

 

                                                statement 3

                                                statement 4

 

 

Indentation and colon are important for else also

 

The if – elif - else Statement

 

  The elif is short for else if. It allows us to check for multiple expressions.

 

  If the condition for if is False, it checks the condition of the next elif block and so on.

 

  If all the conditions are False, the body of else is executed.

 

  Syntax:

                                                if (expression):

                                                            statement 1

                                                            statement 2

 

                                    elif (expression):

                                                statement 3

                                                statement 4

 

                                    else:

                                                statement 5                 

                                                statement 6

 

Although it is not visible in the syntax, but we can have multiple elif blocks with a single if block.

 

The nested if Statement

 

 

  We can have an if...elif...else statement inside another if...elif...else statement.

 

  This is called nesting in computer programming.

 

  Any number of these statements can be nested inside one another.

 

Indentation is the only way to figure out the level of nesting

 

  Syntax:

                                                if (expression):

 

                                                            if (expression):

 

                                                            statement 1

                                                            statement 2

 

                                                            else:

 

                                                            statement 3

                                                            statement 4

                                                statement 5

                                                statement 6

 

 

Assignment

 

 

  Whether a number is even or odd.

 

  WAP to accept a character from the user and check whether it is a capital letter or a small letter. Assume user will input only alphabets.

 

 

  WAP to accept a character from the user and check whether it is a capital letter or small letter or a digit or some special symbol

 

  WAP to accept 3 integers from the user and without using any logical operator and cascading of relational operators, find out the greatest number amongst them

 

 

  WAP to accept a year from the user and check whether it is a leap year or not.

 

A year is a leap year if:


It is exactly divisible by 4 AND at the same time

          not divisible by 100

                  OR

         it is divisible by 400

 

For example:

 

2017 is not a leap year

2012 is a leap year

1900 is a not leap year

2000 is a leap year

 

 

If you have any doubt regarding this topic then, you can ask your doubt in the comment section, I will respond to you as soon as possible.

 

 

  

 

DIGITAL COMMUNITY

We Make IT Happen.

 


No comments:

Post a Comment

If you have any doubts, let me know.