Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python If Statement

by suresh

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

From the above code, 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.

NOTE: Once you hit the enter after the semicolon, Python starts the next statement with a Tab space. This Tab space acts as {} (curly braces) in other languages. By clicking backspace, one can exit from the If statement

Python If Statement Flow Chart

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 screenshot or else click Control + N

Python If Statement 1

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

Python If Statement 2

Please save the Python file as per you wanted

Python If Statement 3

Let’s run the script by selecting Run Menu, and clicking the Run Module or press F5

Python If Statement 4

Click the Run Module will pop up Python shell with the message, “Please Enter any integer Value:”. From the below screenshot, see that we entered 20 and it is a positive integer

Python If Statement 5

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. To demonstrate this, we altered the example 1 and added the following code.

# 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")

If you observe the above Python If statement code, it 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

Python If Statement 6

Let us run the Code. From the below screenshot, you can observe that we entered 50, it means If condition is TRUE. So, Python is displaying print statement inside the If statement and Outside the If block

Python If Statement 7

Let’s try the negative values to intentionally fail the Python If condition

Python If Statement 8

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.

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy