Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - If Else statement

Python

python logo

In this tutorial we will learn about If Else statement in Python.

The if statement is a decision making statement and it controls the flow of the execution of the program.

If

We use the if keyword to create an if statement. The body of the if statement is indented with tabs/spaces.

If syntax

if condition:
  # 
  # if block code
  #

Code inside the if block is executed only when the given condition is satisfied. Otherwise, it is ignored.

Example #1

In the following Python code we are checking whether x is even.

# variable
x = 10

if x % 2 == 0:
  print("x is even.")

print("End of code.")

The above code will print the following output.

x is even.
End of code.

If the value of x is odd then we will get the following result.

End of code.

This is because the if block is ignored as the condition is not satisfied.

If Else

If we want to create an "either-or"/"two-options" then we use the if-else statement.

We use the else keyword to create the else block.

If Else syntax

if condition:
  # 
  # if block code
  #
else:
  #
  # else block code
  #

Code inside the if block is executed only when the given condition is satisfied. Otherwise, else block code is executed.

Example #2

In the following Python code we will get either "x is odd" or "x is even" message depending on the value of x.

# variable
x = 10

if x % 2 == 0:
  print("x is even")
else:
  print("x is odd")

print("End of code.")

As x is 10 so, we will get the following output.

x is even
End of code.

If we change the value of x to 11 then we will have the following result.

x is odd
End of code.

Elif

If we want more than two options then we create a chain of if else statement using elif keyword.

Elif syntax

if condition1:
  # 
  # condition1 block code
  #
elif condition2:
  # 
  # condition2 block code
  #
else:
  #
  # else block code
  #

Code inside the condition1 if block is executed only when the given condition condition1 is satisfied.

If condition1 fails then condition2 is checked. If it is satisfied then the condition2 block is executed.

If no condition is satisfied then, else block code is executed.

You can have multiple elif block based on your requirement.

Example #3

In the following Python code we are checking is x an even number or odd number or zero.

# variable
x = 0

if x == 0:
  print("x is zero")
elif x % 2 == 0:
  print("x is even")
else:
  print("x is odd")

print("End of code.")

As x is 0 so, we will get the following output.

x is zero
End of code.

Shorthand If

If there is only one statement in the if-block then we can write that in one line like the following.

# variable
x = 10

if x % 2 == 0: print("x is even")

print("End of code.")

Shorthand If Else

If there is only one statement for both if and else block then we can write them all in one line.

The following code will print "x is even" if the if-condition is satisfied otherwise, it will print "x is odd".

# variable
x = 10

print("x is even") if x % 2 == 0 else print("x is odd")

print("End of code")

Using and, or and not operators

We can also use logical operators to check multiple conditions.

Example #3

In the following Python program we are going to print "foo" if value of x is divisible by 3, "bar" if value of x is divisible by 5 and "foobar" if value of x is divisible by both 3 and 5.

# variable
x = 10

if x % 3 == 0 and x % 5 == 0:
  print("foobar")
elif x % 3 == 0:
  print("foo")
elif x % 5 == 0:
  print("bar")

print("End of code.")

The above code will print "bar" as x is 10 and divisible by 5. Try changing the value of x to get different result.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT