bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Python Conditional control flow

 

The ability to change the behaviour of a piece of code which is based on certain information in the environment is known as conditional code flow. The conditional logic in Python is primarily based on the “if … else” structure.

 

If Statement

If statement is used to test a condition, if condition is true then the code inside the if statement is executed otherwise that code is not executed.

Syntax 

if condition :

statement

if loop

e.g.

i=1

if i==1:

print  "Hello intellipaat"

Output –

Hello intellipaat

 

If Else

If Else is also used to test a condition, if condition is true then the code inside the if statement is executed otherwise else part is executed.

Syntax 

If condition:

statements

else:

statements

if else loop

e.g.

i=1

if i==2:

print  "Hello intellipaat"

else:

print “Hello”

Output

Hello

 

Nested If

It contains multiple if else condition. It is used to check the multiple conditions. This statement is like executing a if statement inside a else statement.

Syntax

if condition1:

statement

if condition2:

statement

elif condition3:

statement

else

statement

else

statement

e.g. 

 i = 1

if i < 3:

print "i is less than 3"

if i == 2:

print "value of I is equal to 2"

else:

print "value is not equal to 2"

else:

print "Not find true expression"

Output

i is less than 3

value is not equal to 2

 

Python break

It is a jump statement which is used to transfer the control to the end of loop.

break
e.g.

for i in [10,20,30,40]:

if i==30:

print "Item Found"

break

print i

Output 

10

20

Item Found

 

Python Continue Statement –

It is also a jump statement which is used to skip the current statement and forces to execute next iteration.

continue

 

e.g.

for i in [10,20,30,40]:

if i==30:

print "Item Found"

continue

print i

Output 

10

20

Item Found

40

This blog will help you get a better understanding of Automate Your Coding with Python!

"0 Responses on Python Conditional control flow"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 04th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Python Conditional control flow.