Tutorial Gateway

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

Python If Else

The Python If Else Statement is an extension to the If Statement (which we discussed in the earlier post). The If condition will only executes the statements when the given condition is true and if the condition is false, it will not execute statements.

In real world, it would be nice to execute something when the condition fails. To do so, Python If else statement is used. Here, Else statement will execute the statements when the condition fails.

Python If Else Syntax

The syntax of If Else Statement in Python is

if (Test condition):
     # If the condition is TRUE then these statements will be executed
     True statements
else
     # If the condition is FALSE then these statements will be executed
     False statements

If the test condition present in the above structure is true then True statements execute, otherwise, False statements executed.

Flow Chart for Python If Else Statement

The flow chart of the Python If else statement is

Flow Chart for Python If Else Statement

Python If Else Statement Example

In this Python If Else statement program we are going to place 4 different print statements. If the condition is true we will print 2 different statements, if the condition is false we will print another 2 statements using if else statement.

# Example for Python If Else Statement

marks = int(input(" Please Enter Your Subject Marks:  "))
if marks >= 50:
    print(" Congratulations ") #s1
    print(" You cleared the subject ") #s2
else:
    print(" You Failed") #s3
    print(" Better Luck Next Time") #s4

Please save this Python file and run the script by clicking the Run Module or simply click F5

Python If Else Statement 3

We enter 60 as marks, which is greater than 50 that’s why it printed (s1 and s2 statement) inside the If statement block.

Python If Else Statement 4

To demonstrate the If else statement, we entered 30 as marks it means Condition is FALSE so, s3 and s4 statements inside the else block will print.

Python If Else Statement 5

In this If Else Statement example, First, we declared marks variable and asks the user to enter any integer value. int() restrict the user not to enter non integer values

marks = int(input(" Please Enter Your Subject Marks:  "))

If you look at the python if else statement, If Value stored in the marks variable is greater than or equal to 50 then following print statement will execute.

if marks >= 50:
    print(" Congratulations ") #s1
    print(" You cleared the subject ") #s2

If it is less than 50, below statement (inside the Else) will execute.

 print(" You Failed") #s3
 print(" Better Luck Next Time") #s4

Filed 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 Handling
  • 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

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy