Conditional statements in python 3

Untitled Document

In this tutorial, you will learn about Conditional statements in python 3, If statements, If else statements, Nested if, Nested if else statements and Elif ladder statement. There are situations in our day to day life that when we need to take some decision we decide what to do next. Similarly I n programming language we need to take decision and based on this decision next code is executed. You need to execute the statements only if the condition is satisfied. We use the conditional statement when the block of code is executed and check the output is true or false. Also called as Decision making Statements. In programming we want to check condition and change behavior of program in this we use conditional statement.

Following are conditional statements in python :-

  1. If statements.
  2. If else statements.
  3. Nested if else statements.
  4. Elif ladder.

We discuss the conditional statement one by one as follows.

If statement :

The if statement is very simple conditional statement. An "if statement" is written by using the “if “keyword.      Here, if condition after execution will be either true or false. If statement accepts Boolean values if the value is true then it will execute the block of statements below it otherwise not.
     A few important things to note about if statements:

  1. The colon (:) is significant and required. It separates the header of the compound statement from the body.
  2. The line after the colon must be indented. It is standard in Python to use four spaces for indenting.
  3. All lines indented the same amount after the colon will be executed whenever the “test expression” is true.
  4. Not Equals: a != b
  5. Less than: a < b
  6. Less than or equal to: a <= b
  7. Greater than: a > b
  8. Greater than or equal to: a >= b

     These conditions can be used in several ways, most commonly in "if statements" and loops.
Syntax for If statement in python:
                                 If test expression
                                         Statement(s)
Here the program has test expression ,it will execute the statements if the test expression is true .If the test expression is false statements are not executed.The body of the if statement is indicated by the indentation. Body starts with an indentation. Python interprets non-zero values as True. None and 0 are interpreted as False.

Flowchart:-
Below shows the flowchart of if  statement:-


if statement Let’s see an example of the implementation of an if statement:-
a=10
If (a<30):
Print (“10 is less than 30”)
Print (“statement after if statement”)
O/p:-

    1. is less than 10

                Statement after if statement.

If else statements

The else statement can be combined with an if statement. If statement alone tell us if condition is true it will execute block of code if false it won’t But if we want to do something else if the condition is false. Here comes the else statement. We can use the if else statement with if statement to execute a block of code when the condition is false. The if else statement evaluate test expression and will execute body of it only when test condition is true.
If the condition is false the body of else is executed.
Indentation is used to separate the blocks.
There is no limit on the number of statements that can appear under the two clauses of an if else statement, but there has to be at least one statement in each block.
It is useful to have a section with no statements. In that case, you can use the pass statement, which does nothing expect act as a placeholder.

Syntax for If else statement:-

Flow chart for if else:-

if else  statement

Example of If else statement:-
Suppose
 num=6
If num>=0
print("Positive or Zero")
else:
print("Negative number")
The above example if num is equal to 3, the test expression is true and body of it is executed and else is skipped.
If num is -10, test expression is false and body of else is executed and body of it is skipped.
If num is 0, test expression is true and body of it is executed and body of else is skipped.

 

Nested if else statements :-

Nested if-else statements mean that an if statement or if-else statement is present inside another if or if-else block.
Python provides this feature as well this in turn will help us to check multiple conditions in a given program.
An if statement present inside another if statement which is present inside another if statements and so on.
Nested if else means that you will be writing if-else statements inside another if-else statements.
You can write same if else block inside an else block too.
Python provides the feature of nesting so this in turn will help us to check multiple conditions in a given program.
An if statement present inside another if statement which is present inside another if statements and so on.
A nested if is an if statement that is the target of another if statement.
Yes, Python allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
If (condition1)
# executes when condition1 is true
If (condition2):
# executes when condition2 is true
if  Block is end here

Flow chart:-

Nested  if else statements Following is example:-
num=3
if(num>0)
print(“number is positive”)
if(num<10)
print(“number is less than 10”)
Output:
number is positive
number is less than 10
In the above example, we have declared a variable called ‘num’ with the value as 3.
First, it will check the first if statement if the condition is true, then the block of code present inside the first if statement will be executed then it will check the second if statement if the first if statement is true and so on.

if else ladder in python :-

 

Here, the elif stands for “else if “in Python.  The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".
As the name suggest a program which contain ladder of flif statement structured in form of ladder.
If there are more  than two conditions just two if conditions in elif else statements, then it is referred as if elif else ladder.
The structure of a ladder is in terms of if statements.
If one of the if conditions turns out to be true, then the rest of the ladder is just bypassed and the body of that particular if block is executed.
If all the if conditions turn out to be false, then the body of the last else block is executed.
Syntax:-
           If test expression:
           Body of if
           Elif test expression:
           Body of if
           else
           Body of else
The elif is short form for “else if”.
It allows checking multiple expressions.
If condition for if becomes false, then check the condition of next elif block and so on.
If all condition are false then else block is executed.
The if block can have only one else block. But it can have multiple elif blocks.

 

Flowchart:

if statement

example:- if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")

O/p:-
When num is positive, positive no is printed.
If num is equal to 0, zero is printed.
 If num is negative, negative numbers is printed.

 

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India