Conditional Statements In Python


Python – Conditional Statements

Conditional Statement revolves around the if-elif-else keywords. In Python whenever there is a requirement of analyzing boolean conditions, then these conditional expressions are used.

In order to examine the condition, whether the given conditions are true or false these expressions play an important role to execute the statements.

Python if else Statements –

In this section, we will discuss the operations and methods of if-else statements in Python 
Programs –

What is if…else statement in Python Programming?

If...Else statements provides an ability to the programmer to examine conditional checks in the programs. Whenever there is a requirement of decision making then if else statements are used.

If Statement Syntax in Python –

if conditional_test:

In the above expressions, the conditional test will be evaluated, and if the first expression comes out to be true, then the statements will be executed; otherwise the statements will be jumped off.
In Python take care of indentation, whenever a block of if is represented indentation must be followed. Indentation will help you to represent the body of if statement as a block.

Flowchart of if Statement in Python Programming

Flowchart of if Statement in Python Programming

Python Examples – if Statement

number = 5
if number > 2:    
   print(number," is bigger than 2.")
print(“Given Number is”,number)

The above Python code will give following output-

5 is bigger than 2
Given Number is 5

In the above example the number is compared with 2. If the required condition is true then   print(number," is bigger than 2.") will be executed, and if the condition comes out to be false then the above statement will be skipped and print(“Given Number is”,number) will be directly executed.

Python if else Statement Syntax –

if conditional expression:
    Body of if
    Body of else

The if-else statement will first execute conditional expression, and if the condition comes out to be true then body of if will be executed.  In other case when conditional statement is false then python interpreter will directly jump over to the else statement

Python if else Flowchart –

Python if else Flowchart –

Example – Python if else Statement

Write a python program to  find whether provided no. is positive or negative.

Number = 3                 #initializing Nuber with 3
if Number >= 0:    #checking whether provided no. is greater than 0 or not
    print("Positive or Zero")
else:                      #if above condition is satisfied then else will be skipped
    print("Negative number")

Output of above if…else program will be –
Positive or Zero

Yourself you can try 
Number = 0,-2
For above number the output of the program will be –
Positive or Zero, Negative number respectively

Python if elif else Statement –

Elif is a short form of else if. It is used for simultaneous testing of the conditional expression. When there is an expression that is skipped due to fallacy of condition then Elif simultaneously checks the next conditional expression

Python Syntax for if elif else Statement –

if test expression:
    Body of if
elif test expression:
    Body of elif
    Body of else

Flowchart of Python If Elif Else Statement -

Flowchart of Python If Elif Else Statement -

Example -

Write a Python Program to demonstrate the working of if…elif statement –

number = 5                                          # initialization of variable
if number > 2:    
    print("Number is bigger than 2.") 
elif number < 2:                                   # Optional clause (you can have multiple elifs)    
else:                                                       # Optional clause (you can only have one else)    
      print("Number is 2.")

Output of above python program is -

Number is bigger than 2

Python Nested If Statement –

Nested if statements is used when there is a requirement of successive conditional check. In this practice we use to do nesting of if…else…if statement. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.

Syntax of Nested if Statement-

if expression1:
   if expression2:
   elif expression3:
   elif expression4:

Example of Nested If Else Statement -

number = float(input("Enter a number: "))   #it will create a popup for input 
if number >= 0:
   if num == 0:
        print("Positive number")
    print("Negative number")

Entered no. – 2
Positive number

Entered no. –  0

Some Other Python Conditional Satetements -

Using the cmp function to get the comparison result of two objects-

Python includes a cmp function which allows you to determine if one object is less than, equal to, or greater than another object. This function can be used to pick a choice out of a list based on one of those three options.
Suppose there is requirement to print 'greater than' if x > y, 'less than' if x < y and 'equal' if x == y.

['equal', 'greater than', 'less than', ][cmp(x,y)]
x,y = 1,1 output: 'equal'
 # x,y = 1,2 output: 'less than' 
# x,y = 2,1 output: 'greater than'
cmp(x,y) returns the following values
ComparisonResult x < y -1 x == y 0 x > y 1

This function is removed on Python 3. Now we can use the cmp_to_key(func) helper function located in functools in Python 3 to convert old comparison functions to key functions.

Post a Comment

Previous Post Next Post