The Python If statement is one of the most useful decisions making statements in real-time programming. The If statement allows the compiler to test the condition first, and depending upon the result, it executes the code block. While a given test condition is true, then only the code within the if block executes.
Python If Statement Syntax
The if statement in Python Programming has a simple structure:
if (test condition): Statement2 Statement3 …………. …………. Statementn
When the test condition in the If statement is true, Statement1, Statement2, ……., Statementn will execute. Otherwise, all too them will skip. Let us see the flow chart for a better understanding.
Python If Statement Flow Chart
While the test condition is true, STATEMENT1 is executed, followed by STATEMENTN. If it is False, STATEMENTN executes. Because it’s out of the if block and has nothing to do with the result.
Python If Statement Example
This program will check for the positive number using the Python if statement. First, please open your favorite IDLE to write the script, and here we are using 3.5.0. Once you open IDLE, Please select the New File as shown below, or else click Control + N
Once you click on the New File, a new file window will open to write a script for Python If statement example. Please add the below script in that new file.
# Example number = int(input(" Please Enter any integer Value: ")) if number >= 1: print(" You Have Entered Positive Integer ")
Once completed, Click on the File, and select the Save option. Please save the file as per you want.
Let’s run the script by selecting Run Menu and clicking the Run Module or pressing F5.
Click the Run Module will pop up a Shell with the message, “Please Enter any integer Value:”. We entered 20 and it is a positive integer.
Please Enter any integer Value: 20 You Have Entered Positive Integer
First, we declared a number variable and asked the user to enter any integer value. int() restricts the user not to enter non-integer values.
number = int(input(" Please Enter any integer Value: "))
When you look at the below Python if statement, the Value stored in the number variable is greater than or equal to 0, then the line will be executed.
if number >= 1: print(" You Have Entered Positive Integer ")
Here we entered 20, which is greater than 0 that’s why it printed the lines inside the If statement block.
Python If Statement Example 2
In this Python if statement example, we will show you what happens to the code outside the If block by altering example 1.
# Example number = int(input(" Please Enter any integer Value: ")) if number >= 1: print(" You Have Entered Positive Integer ") print(" This Message is not coming from IF STATEMENT")
This Python If statement code is the same code that we used in the first example. However, this time, we added one more print outside the If block with the message.
Please Enter any integer Value: 20 You Have Entered Positive Integer This Message is not coming from IF STATEMENT
We entered 50, which means the condition is TRUE. So, it is displaying the print function inside the If statement and Outside the If block.
Please Enter any integer Value: 50 You Have Entered Positive Integer This Message is not coming from IF STATEMENT
Let’s try the negative values to intentionally fail the condition. While the condition failed here (number < 1), the compiler prints nothing from the If condition block. So, it printed only one print function, which is outside the block.