• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

[Decision Making] if-else, nested elif Statements in Python 3 | Example Code

Aniruddha Chaudhari/18564/2
CodePython

Do you want to learn more about the if-else statement? How to implement the if-else statement in Python?

Let’s dive in…

Table of Contents:

  • What is if-else statement?
  • if stateemt in Python
    • if syntax
    • example
  • if-else statement in Python
    • if-else syntax
    • example
  • elif statement in Python
    • elif syntax
    • example
  • nested if else in Python 3
    • nested if-else syntax
    • example
  • Python coding questions for practice

What is the if-else statement?

In programming and algorithm, we always have a condition.

If it is true, do this.

If it is false (not true), do that.

First thing is to check the condition. Based on the result of the condition, decide what to do the next.

Let me make it simple for you.

Our whole life is conditional…

  • If it is 10 pm, sleep.
  • If it is at 9 am, go to the office.

Even the alarm at our home has an if-else statement implemented.

  • If it is 6 am, play the alarm.
  • Else, be silent.

This is the simplest explanation for decision making I can give to any novice to understand.

Now, moving to Python programming…

How to implement if-else logic in Python?

if statememt in Python

For implementing the if-else statement in Python 3, you have to follow a certain syntax. Your system only understands the syntax, the way you write.

Python syntax for the if statement:

if <condtion>:
    <block_of_your_code>

The ‘if’ condition will be followed by the code block. And it will be executed if the condition is true.  If the condition is false, control pointer will move out of the block without executing it.

Understanding syntax:

  • The colon (:)  after if-condition depicts the start of if the if-block. Don’t forget that.
  • All the lines of code inside if block should start with a consistent number of white spaces, called as an indentation in Python

Python Program for if statement:

Example: Write a Python program to check if the number is zero.

nNum = 0
if nNum == 0:
  print("Number is Zero.")
print("Out of if statement")

Output:

Number is Zero.
Out of if statement

Now let’s say assign value 1 to nNum to make the if condition false.

nNum = 1
if nNum == 0:
  print("Number is Zero.")
print("Out of if statement")

Output:

Out of if statement

Here, the print statement in if-block is not executed.

if-else statement in Python 3

What if you want to execute some code if the given condition is false?

Here is the if-else statement you need to implement.

Python syntax for the if-else statement:

if <condtion>:
  <your_if_code>

else:
  <your_else_code>

Python Program for if-else statement:

Example: Write a Python program to print whether if it is a zero or non-zero number.

nNum = 1
if nNum == 0:
  print("Number is zero.")
else:
  print("Number is not zero.")

Output:

Number is not zero.

elif statement in Python 3

We can also add one more if condition inside else block. This is called as nested if-else in programming.

Python syntax for the nested if-else statement:

if <condtion>:
  <your_if_code_block>

elif <condtion>:
  <your_elif_code_block>

else:
  <your_else_code_block>

What is the elif keyword in Python?

The elif keyword is short for else-if.

The block inside the elif will be executed when two conditions meet.

  • The first if condition should be false.
  • The elif condition should be true.

Python Program for elif statement:

Example: Write a program to print if the given number is a positive or a negative using elif statement.

nNum = 1
if nNum == 0:
  print("Number is zero.")
elif nNum > 0:
  print("Number is a positive.")
else:
  print("Number is a negative.")

Output:

Number is a positive.

Nested if else in Python 3

Can we add if statement inside another if statement in Python?

Yes.

Python syntax for the nested if-else statement:

if <condtion>:
  <your_code_block_1>
  if <condition_1_1>:
    <your_code_block_1_1>
  else:
    <your_code_block_1_2>

elif <condtion>:
  <your_elif_code_block>

else:
  <your_else_code_block>

Python Program for nested if-else in Python:

Example: Write a Python program to print if the number is positive, negative or zero, using nested if-else statement.

nNum = 1
if nNum != 0:
  if nNum > 0:
    print("Number is a positive.")
  else:
    print("Number is a negative.")
else:
  print("Number is zero.")

Output:

Number is a positive.

With this, you can check multiple conditions in Python if statement.

Practice Solving if-else Python Programming Questions:

What’s next?

Here is a simple Python program exercise for practice.

  • Write a Python program to print the month of the year based on the given user input.

Read how to take user input from the keyboard in Python.

Check more Python coding questions for practice.

This is all about decision making using if-else and nested if-else in Python 3. If you have any doubts in this complete tutorial for decision making in Python, feel free to write in the comment section.

Happy Pythoning!

Python Interview Questions eBook

Python
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Anil Kumar Reddy
    January 5, 2020 at 4:14 pm

    Hi Aniruddha,

    Please modify the if-else condition First line.

    nNum == 1
    if nNum == 0:
      print("Number is zero.")
    else:
      print("Number is not zero.")
    

    Thanks,
    Anil

    • Reply
      Aniruddha Chaudhari
      January 6, 2020 at 8:57 am

      Done.

      Thanks for the correction, Anil Kumar!

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard