You are currently viewing Conditional Construct In Python
All Conditional statements in python >> bittutech

Conditional Construct In Python

Hello techies!! Welcome again in this domain www.bittutech.com. I hope you all are well and enjoying by articles and enhancing your skills.

Today’s Blog post is dedicated to 👉 Conditional statements in Python Programming Language.

Are you excited and ready to enhance your skills?? If yes!!! Then let’s begin read this interesting python concept👍.

Statement in Python–:

Statement are the instruction written in source code given to the computer for executing any task. These help users to obtain the desired output by working and manipulating data, making decision and repeating the set of instructions.

Types of Statement–:

Python statement are classified into three types–:

  1. Empty statement
  2. Simple statement
  3. Compound statement

1. Empty statement–:

The statement which does nothing. It is the simplest statement or most precisely, we say that it is an empty statement which serves as a placeholder in Python.

pass’ is a statement that are used to represents empty statement that does nothing in a program.

Its syntax is :  

pass

2. Simple statement–:

Simple statement is a single line executable statement in Python.

For example–:

name= input("Enter your name") # this is for giving some input to your machine
print(name) #display the given input
x=10

Which means that we are assigning a value “10” to the variable “x”. We can said that the statement is simple statement.

3. Compound statement–:

A compound statement represents a group of statements execute as unit. The compound statement is written as follows–:

<compound statement header>:
     <indented body containing    multiple simple and/or compound statement>

There are some points that should be remembered always–:

  1. The contained statement are not written in the same column as the control statement rather they are indented to the right and together are called a block.
  2. The first line of compound statement, its header contains(:) at the end of it.

For example–:

num1=int(input("Enter the number--:"))      #input the integer value
num2=int(input("Enter the number--:"))      #same here as above instruction
if num2 < num1:                              # conditional statement header area          
     t=num2*num2                              # body area of conditional statement     
     t=t+10
print(num2, num1,t)                       #display the value of num2, num1, t

Output–:

Decision-Making Statements in Python–:

The order of execution of the statement in program is known as flow of control. It is dependent on the programming constructs used while writing the program. Decision-making statements are used to control the flow of execution of a program depending upon the condition.

There are four type of decision-making statements in Python–:

  1. if-statement
  2. if-else statement
  3. if-elif statement
  4. Nested if-else statement

1. if Conditional Statement–:

This is a kind of conditional statement in which conditional expression is checked by “if” block, If it is ‘true‘ then indented statements will be running otherwise jump directly to the next non-indented block.

Syntax–:

Header

if(conditional expression):
     statement
     statement(s)

NOTE–:

  • Colon (:) is must at the end of conditional expression.
  • The statements inside an “if” are indented at the same level.

REMEMBER –:

  1. If the statement is TRUE, then the indented statement gets executed.
  2. The indented statement implies that its execution is dependent on the header.
  3. There is no limit on the number of statements that can appear under the if block.

Flow Chart for if Statement.

Example–:

term1=150
term2=245
if(term1<term2):
     print("term1 is less than term2")

Output–:

term1 is less than term2

2. if-else Conditional Statement–:

This is also a kind of conditional statement in which conditional expression evaluates and if the result will be true then ‘if’ block will be run otherwise ‘else’ block will be run.

Syntax–:

Header

if(conditional expression):
statement
statement(s)
else:
statement
statement(s)

Note–:

  • Colon (:) is must at the end of conditional expression.
  • The statements inside an “if” and “else” are indented at the same level.

For example–:

num1=20
num2=30

if(num1>num2):
     print("20 is greater than 30")

else:
     print("30 is greater than 20")

Output–:

30 is greater than 20

3. if-elif Conditional Statements–:

This a also kind of conditional statement in which more than one conditional expression evaluates.

Syntax–:

Header

if(conditional expression):
statement(s)

elif(conditional expression):
statement(s)

Or

Header

if(conditional expression):
statement(s)

elif(conditional expression):
statement(s)

elif(condition expression):
statement(s)

else:
statement(s)

non-indented statement

For example–:

# Get user input for age
age = int(input("Enter your age: "))

# Check the age and print the appropriate message
if age < 18:
print("You are a minor.")
elif 18 <= age < 65:
print("You are an adult.")
else:
print("You are a senior citizen.")


In this example, the program takes the user’s age as input and uses if-elif statements to check the age group. If the age is less than 18, it prints “You are a minor.” If the age is between 18 and 64, it prints “You are an adult.” If the age is 65 or older, it prints “You are a senior citizen.” The elif keyword allows you to check multiple conditions in sequence after the initial if statement.


Last Word-: I hope you enjoyed this interesting blog post, we’ll meet soon till then keep learning and keep supporting. Thankyou.

Share post

Prajjwal Singh

Tech Blogger || Web developer || Computer Networking Enthusiast || Microsoft SQL Database Management Expert || Software Debugger || Learned DOS OS Structure

Leave a Reply