Conditional statement in Python

in #python7 years ago

Intro_Python.png

Welcome to the basic python tutorial series. In this tutorial we will discuss about the conditional statement in python. In programming language, there is need to control the order of execution of statement. It can be achieve using if..else statement in python.

The if..else block decides which statement need to be execute based on the logical expression included. The general syntax is as follow.

if (expression):
execution block 1
else:
execution block 2

If the expression evaluate to True then block 1 will be executed else block 2 will be executed.In if..else conditional statement else is optional. You can ignore else statement if you do not want to execute any code in block 2.

Let's see the example.

a = 5
b = 2

if(a>b):
print "a is greater than b"
else:
print "b is greater than a"

Nested if..else statement: We can have multiple condition check in if else statement. In order to achieve it we can use nested if else statement as shown below.

a = 2
b = 2

if(a>b):
print "a is greater than b"
elif(b>a):
print "b is greater than a"
else:
print "a is equal to b"

The output of above code will be "a is equal to b".

There can be scenario where it require to evaluate multiple logical expression. This can be achieve using logical operator such as 'and','or' etc. as shown below.

a = 5
b = 2
c = 3

if(a>b and a>c):
print "a is greatest number"
else:
print "a is not greatest number"

The list of comparison operator is as follow.

< Less than
Greater than (>)
<= Less than or equal to
= Greater than or equal to
== Equal to
!= Not equal to

The list of logical operator which can be used with the multiple comparison operator is as follow.

and: All the logical expression is evaluated to True
or : At least one of the logical expression is evaluated to True
not : Negation of evaluated logical expression.i.e if logical expression is True it will take it as False and vice versa.

That's it for this tutorial. We will see you in the next tutorial till then "Happy Coding".

Please join the @labwork team and Up vote,follow and resteem.
Comments are always appreciated.

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by labwork from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Congratulations @labwork! You received a personal award!

1 Year on Steemit

Click here to view your Board of Honor

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @labwork! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!