OfferTransform Your Career with Expert-Led IT Training. Flat discounts active!Explore Now
OnlineITGuru Logo
WEEKEND SPECIAL - UPTO 60% OFF
Software Development

Conditional Statements in Python

Last updated on Jun 15, 2020

Copy Link:
Conditional Statements in Python

Conditional statements in python performs actions (or) computations that depends on a certain condition(true(or) false).

Python supports different types of control statements. So, let us discuss one by one in detailed.

If statement :

Python If is a statement (or) a group of statements executes when a particular condition satisfies.

Conditional Statements in Python

Syntax :

If (condition = true)

statement

Ex: i=1

If(i==1)

Print (“condition satisfies(true)”)

Would you like to know how people use python for Data Science

If else statement:

In the above statement, there is no logic to execute if the IF condition fails. So this python if –else statement satisfies the condition.

As said above IF block executes if the condition satisfies. Otherwise Else block executes.

Conditional Statements in Python

Syntax:
if(Condition =true)

Statement 1

Else

Statement 2

Ex : i=1

If (i==1)

Print(Since i=1 true condition executes)

Else

Print(Since I not equal to 1  false condition executes)

Nested if :

There are some situations, where an operation needs to satisfy several conditions. Then we use Nested if condition. This is also called the Multiple If statements.

Conditional Statements in Python

Syntax :
If(condition = true)

Statement 1

If(condition=true)

Statement 2

elif(condition =true)

statement 3

else

statement 4

Ex: 

I=10

If(i<100)

Print(’10 is less than 100’)

If(i==20)

Print(’10 equals to 20’)

Elif

Print(‘we cannot equate 10 with 20’)

Else

Print(‘There is no true condition in the statements. So else block is executed’).

Since these concepts were common in many programming languages like C, JAVA .So I advice you to practice more code on this loop. Feel free to clarify the doubts at Software training institutes

Break statement

As like above, we cannot expect only one / two conditions. In some, there may be a 100 (or) even maybe a 1000 conditions. Moreover, there is only a need for one block of statements to execute if the condition satisfies. So in those cases, we uses break statement.

Conditional Statements in Python

Syntax:
If (condition 1 )

Statement 1

Break

If(condition 1)

Statement 2

Break

Else

Statement 3

Ex :

 I = 20

If ( 1==10)

Print(‘I not equal to 20’)

Break

If(i==20)

Print(‘Condition satisfies’)

Break

Else

Print(‘No condition satisfies’)

Continue :

This is somewhat similar to the Break statement. Hence the condition satisfies it skip the current statement and forces to process the next iteration.

Since these were common in every programming languages like C, JAVA, So, I would like to leave the code for you as an exercise. And if  you were struck up anywhere python online training will guide you

Why Choose Us

Master Your Future with OnlineITGuru

We don't just provide courses; we build careers. From expert-led live training to dedicated placement support, discover why thousands of professionals trust us for their digital transformation journey.

200+

Partner Companies

$120K

Highest Package

75%

Average Hike

98%

Placement Rate

Reliable Career Partners

Google
Microsoft
Amazon
Meta
Netflix
Apple