Python if Statement Tutorial with examples



Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we will be studying about the Python if Statement.

The Python if statement contains a Logical expression making use of which data is compared and a decision made based on the result of the data compared.

Syntax

Following is the syntax to use if statement -

if expression:
   statement(s)

Here, if the boolean expression evaluates to TRUE, the block of statement(s) in the if statement is executed. On the other hand, if the boolean expression evaluation is FASLE, then the first set of codes after the end of the if statement(s) is executed.

Flow Chart



Example

Below is a short example -

#!/usr/bin/python

var1 = 500
if var1:
   print "1 - Got a true expression value"
   print var1

var2 = 0
if var2:
   print "2 - Got a true expression value"
   print var2
print "Welcome to Web design tutorialz!"

Output

Below is the output of the above example -

1 - Got a true expression value
500
Welcome to Web design tutorialz!

RECOMMENDED POST: Python Operators

Alright guys! This is where we are rounding up for this tutorial. In our next tutorial, we will be discussing about the If...else Statements.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.