• Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials – Company wise
      • Infosys Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Dell Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • TCS Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation – E Books
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
      • Quant for Placements
    • Register
    • Login
  • [email protected]
  • ..
Login / Register
Apply Now
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login

Cart

If-elif-else Statements in Python

  • January 27, 2020
  • CODE OF GEEKS
  • 0


Consider a situation where you are required to define a correct course of action based on a particular condition. You can do this with the help of if statements or if-else statements.

Generally, when there are more than two choices then if-else ladder is used. Let us analyze them in detail.

1. If Statement

This statement is used to execute one or more statements depending on whether a condition is true or false.

Syntax

if(conditions):
    statement 1
    statement 2

Let us consider a program to see the working of if statements.

Program : Check whether a given number is even or odd using if statements only.

num = 4
if(num%2 == 0):
     print("Even")
if(num%2==1):
     print("Odd")

Output

Even

In the above program we have used two if statements of which first is for checking even number and second one is for checking odd number. Here, for num=4, we get the answer from first if, but even after getting the output interpreter will check other conditions as well. This is an inefficient approach. To save our program from unwanted longer execution we use if-else statements.

2. If-else Statement

This statement follows following idea :

if this then do-this else do-this

In other words, if-else statements executes a group of statements when a condition is True, else it executes another group of statements.

Syntax

if(conditions):
    statement 1
else:
    statement 2

When a given condition is True, then Statement-1 will execute. When a given condition is False, then Statement-2 will execute.

Let us code the same program of even-odd checker using if-else.

num = 4
if(num%2 == 0):
     print("Even")
else: 
     print("Odd")

Output

Even

So, if-else is faster than if statements.

3. if … elif … else Statement

There may be a situation, when you want to test multiple conditions and drive the output accordingly. In such cases where there are wide range of statements, if-elif-else statements are useful.

Syntax

if(condition1):
    statement 1
elif(condition2):
    statement 2
else:
    statement 3

When condition1 is True, then Statement-1 will execute. When a given condition2 is True, then Statement-2 will execute. If both conditions are False, then Statement-3 will execute.

Do you know ?

elif statement in Python is same as else-if statement in other languages.

Let us consider a program to see the working of if-elif-else statements.

Program : To check whether a given number is zero, positive or negative.

n=4
if(n==0):
    print("Zero")
elif(n>0):
    print("Positive")
else:
    print("Negative")

Output :

Positive

Talking about Indentation –

Indentation refers to the spaces that are generally used in the beginning of a statement. Statements with same Indentation belong to same group. Indentation plays a vital role in Python programing as it defines the hierarchy of statements.

Cook this !

Write a Python program to find the greatest among given three numbers.

That’s all, see you in the next lecture.





Tags: if elif statement in pythonif else in pythonif else pythonif else statement pythonif else statements pythonif statement in python
  • Previous Looping in Python
  • Next Control Statements in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Table of Content – Python

  1. Introduction to Python Programming
    • Python and its features
    • Key Differences : C vs Python
    • Key Differences : Java vs Python
    • Python Flavors
    • PVM and Memory Management in Python

  2. Python Programming Fundamentals
    • 'Hello World' using Python
    • Comments in Python
    • Variables and Garbage Collection in Python
    • Datatypes in Python
    • Determining the Datatype of Literal | type() method
    • Tokens in Python
    • Keywords in Python
    • Identifiers and Naming Convention in Python
    • Literals in Python

  3. Python Operators
    • Operators in Python
    • Python Mathematical Functions

  4. Python User Inputs
    • Taking Inputs | input() function in Python

  5. Python Control Statements
    • Control Statements in Python
    • if-elif-else Statements in Python
    • Looping in Python
    • while loop in Python
    • while-else loop in Python
    • for loop in Python
    • for-else loop in Python
    • Infinite loops in Python
    • Nested loops in Python
    • break Statement in Python
    • continue Statement in Python
    • pass Statement in Python
    • assert Statement in Python
    • return Statement in Python
CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Courses

  • Mail Us
  • Login/Register

Recent Posts

Introduction to Functions in Python with Example
  • November 28, 2021
return Statement in Python with Example
  • November 27, 2021

Copyright 2021 CODE OF GEEKS. All Rights Reserved.

  • →