Best post on conditional statements in python – Part 2




Remaining conditional statements in python

Python programming consists of various types of decision making statements.
Conditional statements in python:




if-elif-elif-else statement (multiple elif)

We use elif block for multipath decision making. This conditional statements in python allows us to mention condition similar to if statement. In this is we have many separate blocks of code and one of them get executed based of certain condition. We have three block starting with if block, elif block and else block. First if block will check if mentioned condition is true or not, if the condition is true then if block will get executed and if the condition is false then pointer shifts to very next elif block. elif block will check for its condition, if the condition is true then elif block will get executed and if the condition is false then pointer shifts to very next elif block. And this goes on until one condition get satisfied or last block i.e. else block will get executed.

In simple words if block and elif blocks will check for certain condition one by one and who ever’s condition get satisfied first then respective block is executed, and if no condition is satisfied then last block i.e. else block will get executed.

We cannot use elif block and else block without if block.

Like while loop if block and elif block also accepts boolean value which means if we enter 0 as condition then it will be considered as False and non-zero value will be considered as True.

decision-making-statement-if-elif-elif-else-itvoyagers-1
flowchart for if…elif…elif….else statement in python (itvoyagers.in)

Syntax
Here is the simple syntax for if…elif…else conditional statements in python−

if (condition):
    code block if condition is True
elif (condition 2):
    code block if condition 2 is True
elif (condition 3):
code block if condition 3 is True
.
.
.
. else: code block if both conditions are False




Example 1

"""
Author : ITVoyagers (itvoyagers.in)

Date :9th August 2020

Description : Program to show use of if...elif...elif...else statement

"""
deposit=int(input("Enter a number"))
if deposit < 10000:
    interest=deposit * 0.10  
    print("interest is",interest)
elif deposit < 15000:
    interest=deposit * 0.20
    print("interest is",interest)
elif deposit < 25000:
    interest=deposit * 0.25
    print("interest is",interest)
else:
    interest=deposit * 0.30
    print("interest is",interest)





OUTPUT

if elif elif else statement

if…elif…elif….else statement in python (itvoyagers.in)

Nested if statement

When we write if statement inside on another if statement is known as nested if-else.
We can nest if statements as many as we want. We can also write if statement is else block of if-else 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
Here is the simple syntax for if…elif…else statement −

if condition1:
    if condition2:
        print("i will print if both conditions are True")
    else:
        print("I will print if condition 2 is False")
else:
    print("I will print if condition 1 is False")

Code given below is one of the classic example of nested if-else statement. In this example we try to find out greater number among given three number(p,q and r).

First we compare value of p with value of q, the condition is “p>q” which is false, hence if block will not get executed instead else block will get executed.

In else block we have one more if-else statement. This time condition in if statement is “q>r” which is also false, hence else block will get executed.

"""
Author : ITVoyagers (itvoyagers.in)

Date :9th August 2020

Description : Program to show use of nested if...else statement

"""
p=int(input("Enter a number"))
q=int(input("Enter a number"))
r=int(input("Enter a number"))
print("p=",p,"q=",q,"r=",r)

if p>q:
    if p>r:
        print("p is greater")
    else:
        print("r is greater")
else:
    if(q>r):
        print("q is greater")
    else:
        print("r is greater")




OUTPUT

nested if else statement
nested if else statement in python (itvoyagers.in)

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......



For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon.......

Leave a Comment