The Python If statement is one of the most useful decisions making statements in real-time programming. Python If statement allows the Python compiler to test the condition first, depend upon the result, it executes the code block. If a given test condition is true, then only statements within the if statement block executes.
Python If Syntax
The if statement in Python Programming has a simple structure:
if (test condition): Statement 2 Statement 3 …………. …………. Statement n
If the test condition in the Python If statement is true, the statements (Statement 1, Statement 2, ……., Statement n) will execute. Otherwise, all these statements will skip. Let us see the flow chart for a better understanding.
Python If Statement Flow Chart
If a test condition is true, STATEMENT 1 executed, followed by STATEMENT N. If it is False, STATEMENT N 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 Python if. First, please open your favorite IDLE to write Python script, and here we are using Python 3.5.0. Once you open Python IDLE, Please select the New File as shown in the 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 example. Please add the below script in that new file
# Example for Python If Statement 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 Save option
Please save the Python file as per you wanted
Let’s run the script by selecting Run Menu, and clicking the Run Module or press F5
Click the Run Module will pop up Python shell with the message, “Please Enter any integer Value:”. We entered 20 and it is a positive integer
First, we declared a number variable and asked the user to enter any integer value. int() restrict the user not to enter non-integer values
number = int(input(" Please Enter any integer Value: "))
If you look at the below Python if statement, If Value stored in the number variable is greater than or equal to 0, the statement 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 statement inside the If statement block.
Python If Statement Example 2
In this Python if statement example, we will show you what happens to the statements outside the If block by altering the example 1.
# Example for Python If Statement number = int(input(" Please Enter any integer Value: ")) if number >= 1: print(" You Have Entered Positive Integer ") print(" This Message is not coming from PYTHON 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 statement outside the If statement block with the message This Message is not coming from PYTHON IF STATEMENT
We entered 50, it means If condition is TRUE. So, Python is displaying print statement inside the If statement and Outside the If block
Let’s try the negative values to intentionally fail the Python If condition. If the condition failed here (number < 1). compiler prints nothing from the Python If condition block. So, it printed only one print statement, which is outside the block.