Best post on conditional statements in python – Part 1

What are conditional statements in python?

Generally we are writing straight line programs in which statements are executed one after another till end of line character.
Conditional statements are also known as branching statements or decision making statements.
In conditional statements an expression or condition is tested and depending on the test condition it returns true or false than the respective block of code is executed.
The conditional statements has three main parts:

  • Test : In this expression to be tested is mentioned.
  • Block for True condition : It consists of block of code to be executed when condition is True .
  • Block for False condition : It consists of block of code to be executed when condition is False. It is and optional block of code.




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

Conditional Statements in Python (itvoyagers.in)
<strong>Conditional Statements in Python (itvoyagers.in)</strong>




if statement

This is the most simple and easy decision making statement in python programming. In this, if statement will check for certain condition and if the condition is true then it will execute block of code written in it. If the condition is false then it will not execute block of code.
Like while loop if 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.

flowchart for if statement in python (itvoyagers.in)
<strong>flowchart for if statement in python (itvoyagers.in)</strong>

Syntax
Here is the simple syntax for if statement −

if (condition):
    code block if condition is True

Example 1

"""
Author : ITVoyagers (itvoyagers.in)

Date :9th August 2020

Description : Program to show use of if statement

"""
if 0:
    print ("if condition is true i will be printed")
print("if condition is false i will be printed")

OUTPUT

if statement in python (itvoyagers.in)
<strong>if statement in python (itvoyagers.in)</strong>

Example 2

"""
Author : ITVoyagers (itvoyagers.in)

Date :9th August 2020

Description : Program to show use of if statement

"""
i=int(input ("Enter a Number"))
if i<15:
    print ("Condition is True")
    print(i, "is less than 15")

OUTPUT

if statement in python (itvoyagers.in)
<strong>if statement in python (itvoyagers.in)</strong>




if-else statement

In this is we have two separate blocks of code and one of them get executed based on certain condition. We have if and else two separate blocks, if block checks the condition and if the condition is true then if block will get executed and if the condition is false then else block will get executed.
In simple word there are two separate blocks of code and which block will get executed depending on condition mentioned in if’s parameter.
Like while loop if 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.

flowchart for if...else statement in python (itvoyagers.in)
<strong>flowchart for if&#8230;else statement in python (itvoyagers.in)</strong>

Syntax
Here is the simple syntax for if..else statement −

if (condition):
    code block if condition is True
else:
    code block if condition is False

Example 1

"""
Author : ITVoyagers (itvoyagers.in)

Date :9th August 2020

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

"""
if 0:
    print ("if condition is true i will be printed")
else:
    print("if condition is false i will be printed")

OUTPUT

if...else statement in python (itvoyagers.in)
<strong>if&#8230;else statement in python (itvoyagers.in)</strong>

In below example we have take a value i from user, then in if parameter we are checking if value of i is less than 15 or not.

Example 2

"""
Author : ITVoyagers (itvoyagers.in)

Date :9th August 2020

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

"""
i=int(input ("Enter a Number"))
if i<15:
    print ("Condition is True")
    print(i, "is less than 15")
else:
    print("Condition is False")
    print(i, "is greater than 15")

OUTPUT

if...else statement in python (itvoyagers.in)
<strong>if&#8230;else statement in python (itvoyagers.in)</strong>




if-elif-else statement

We use elif block for multipath decision making. 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 else block.

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.

flowchart for if...elif...else statement in python (itvoyagers.in)
<strong>flowchart for if&#8230;elif&#8230;else statement in python (itvoyagers.in)</strong>

Syntax
Here is the simple syntax for if…elif…else statement −

if (condition):
    code block if condition is True
elif (condition 2):
    code block if condition 2 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...else statement

"""
a=int(input("Enter a number"))
if a<0:
    print("Number is negative")
elif a>0:
    print("Number is positive")
else:
    print("Number is zero")

OUTPUT

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

<strong>if&#8230;elif&#8230;else statement in python (itvoyagers.in)</strong>



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