• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » if else in python
Next →
← Prev

Python if…else Statement

By Dinesh Thakur

In this tutorial, you will learn decision-making using different if-else statements in the Python programming language. Let’s one by one explore them.

In python, decision making performs by the following statements.

StatementDescription
if StatementThe if-block is used to test a particular condition. If the condition is true, a code block will execute.
if-else StatementThe if-else statement is similar to the if statement except that it also contains the code block to verify the condition’s false case. If the condition is given in the if statement is false, then else statement will execute.
Nested if StatementNested if statements allow us to use an if-else statement in an outer if statement.

We’ll be covering the following topics in this tutorial:

  • Indentation in Python
  • What is Python If Statement?
  • Python if…else Statement
  • Python if…elif…else Statement
  • Python Nested if statements

Indentation in Python

Python does not allow the use of block-level code parentheses. In Python, a block declares by indentation. If two statements are at the same level of indentation, they are part of the same block. In general, four spaces give to indent the statements.

Indentation is the most common part of the python language because it declares the code block. All one-block statements have the same indentation level. We will see how the actual indentation takes place in Python.

What is Python If Statement?

Python if Statement is the most simple decision-making statement. It decides whether a specific statement or particular block of code will be executed or not based on the condition provided in the if Statement. If a specific condition is true, then a block of Statements is executed; otherwise, not.

Python if Statement Syntax

if [conditional expression]:
  statement(s)

Python if Statement SyntaxThe if” statement is used to make decisions. Once the limitations have been defined, the code’s body will only run when the ‘If’ statement is true.

The first line of the statement, i.e., if [conditional expression]: it ends with a colon, and the condition is a boolean expression that evaluates either True or False. The program evaluates the conditional expression and will only execute statement(s) if the expression is true. The statement(s) will not execute if the test expression is false.

In the next line, we have a block of statements. A block is just a set of one or more declarations. When a block of statements followed by an if clause, it is known as an if block.

Please note that each statement within the if block is indented to the right of the if keyword by the same amount. Many languages such as C, C++, Java, PHP use curly braces ({}), but Python uses indentation. Each statement must block inside if the same number of spaces. Otherwise, a syntax error will occur.

Python interprets Non-zero values as True. None and 0 interpret as False.

Python if Statement Flowchart

First, the test expression checks whether the expression is true, the if-statement is executed. If it is false, the statement present after the if-statement executes. In either case, an external if-statement code line evaluates by default.

Example: Python if Statement

#Let’s see an example of the implementation of an if statement.

num = 5
if (num <10):
  print (“5 is less than 10”)
  print (“Statement after if statement”)

Output:

Python if Statement
In the above example, the condition expression is num < 10.
The body of if performed if this is true.
When the variable num is less than to 10, condition expression is true and statements inside the body of if are executed.
If the variable num is greater than 10, condition expression is false and statements inside the body of if are skipped.
The print() statement falls outside of the if block (unindented). Hence, it is executed regardless of the test expression.

Python if…else Statement

You can combine else statement with an if statement. Else statement executes a code if the conditional expression in a if-statement is 0 or a FALSE. The else statement is an optional statement.

Syntax of if…else

if expression:
  block_of_code_1
else:
  block_of_code_2

Python if else Statement Syntax
In Python, if statement informs the program what to do if the condition is true. If the condition is false, the program only executes what comes after if statements. In situations where we want the program to execute some statements if the condition is true and some other statement only if the condition is false, we use if else in Python.

The else keyword must be on its line and be at the same level indentation level as the if keyword. A colon must follow the else keyword. Any code included in the else statement must be marked with the same level.

Python if..else Flowchart

Python if..else FlowchartAs depicted by the flowchart above, the Python program first evaluates the test expression. If the condition is true, then the if statement executed. If the condition is false, then the else statement is executed. The body of if and else statements start with indentation.

Example of Python if…else

num = 5
if num >= 0:
  print("Positive or Zero")
else:
  print("Negative number")

Output :

Python if else
In the above example, when num is equal to 3, the test expression is true if-statement is executed and else part is skipped.

If num is equal to -5, the test expression is false and else part is executed and if statement is skipped.

If num is equal to 0, the test expression is true and if statement is executed and else part is skipped.

Python if…elif…else Statement

Here, the elif stands for else if in Python. The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Only one block among the several if…elif…else blocks is executed according to the condition.

Similar to the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if.

Syntax of if…elif…else

if test expression:
  Body of if
elif test expression:
  Body of elif
else: 
  Body of else

Python-elif-Statement-Syntax

Flowchart of if…elif….else in Python programming

Flowchart of if elif elseExample of if…elif…else

num = 1.5
if num > 1:
 print("+ve Number")
elif num == 0:
 print("Zero")
else:
 print("-ve number")

Output :

Python if elif else outputPython Nested if statements

We can have an if…elif…else statement inside another if…elif…else statement. This is called nesting in computer programming.

Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.

Python Nested if Example

num = float(input("Enter any Number: "))
if num >= 0:
 if num == 0:
   print("Zero")
 else:
   print("+ve Number")
 else:
   print("-ve Number")

Output:

Python Nested if Example

You’ll also like:

  1. Python Condition Statement
  2. What is Python? | Introduction to Python Programming
  3. Python Features | Main Features of Python Programming Language
  4. Python Operators and Operands – Types of Operators in Python
  5. What is for-loop in Python With examples?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.