EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Python 3 Tutorial Python 3 else if
 

Python 3 else if

Updated June 12, 2023

Python 3 else if

 

 

Definition of Python 3 else if

Python 3 else if statement comprises a block of code that executes. If we combine an else statement with an if statement. The statement of else is optional, and there can only be one after if. When we wish to run a program only if a specific condition is met, we need to make a decision. Python makes decisions using the if elif else statement.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is python 3 else if?

  • In real life, there is a situation where we are required to complete a given task and must select what to do next based on a set of circumstances. In programming, there are times when a task must be completed only if a certain condition is true.
  • Else if statements can help in certain situations. The conditional statements are provided by python.
  • If the statement is true, then if the statement is executing the statement of block; if the stmt is false, then if statement will not execute a set of statements.
  • The code of block is incorporated as an else statement in a conditional if statement, which is executed when the if statement is false.
  • Below is the syntax of python 3 else if statements are as follows.

Syntax:

if (condition)
		# Block of statement
	else (condition)
		# Block of statement.
  • If loop condition was true, then else if the statement analyses the test expression and executes the body of the if statement.
  • If suppose loop condition was false, then otherwise the body is run. The blocks are separated using indentation.
  • The test expression must first be verified. The body of the if statement is executed if the expression returns true. If it’s false, the statement will be executed following the if statement.
  • Below is the flowchart of else if statement in python is as follows.

Flowchart of Python 3 else if

  • When compared to a simple if statement, else if will add extra stage to the decision-making process.
  • Starting of the else if stmt. is similar to that of a basic if statement; but, if the condition is false, the indented phrase will be printed instead of nothing.
  • The statement itself specifies that if a particular condition is true, then the statements inside the “if block” should be executed, after giving a false statement, the “else” block should be executed.
  • If the condition is false will the “else” block be executed? The Boolean expression is evaluated using the if-else statement. When a statement is true, the “if” block’s code will be executed.

Python 3 else if conditionally

  • In below, examples find the absolute value of the supplied integer py. If py > 0, the software should display the value py; otherwise, it should display the value -py.
  • The sequential program is unable to achieve this behavior. The program should choose the next step based on the conditions.

Code:

py = int (input())
if py > 0:
    print (py)
else:
    print (-py)

Output:

Output 1

  • It is used in this program as a conditional statement. We added a condition of py > 0 after the if, followed by a colon. Then we add a block of instructions that will only run if the stmt is true.
  • This block may be followed by the word else, a colon, and a new set of instructions that will only be performed if the stmt is false.
  • Because the stmt is false in the example above, the ‘else’ block is run. Spaces should be used to indent each block.
  • Any Python instruction, including another conditional statement, can be placed in the ‘true’ and ‘false’ blocks. Inner condition blocks have twice as many spaces indented.
  • The below example shows how python 3 else if statement is working, are as follows.

Code:

py1 = 15
py2 = 25
if py1 > 35:
    if py2 > 65:
        print ("Stmt 1")
    else:
        print ("Stmt 4")
else:
    if py2 > 30:
        print ("Stmt 2")
    else:
        print ("Stmt 3")

Output:

Python 3 else if Output 2

  • We have declared two variable names as py1 and py2 also we have assigned 15 and 25 values respectively to them.
  • In the above example, we have declared two variables and assigned values to them. In the first expression, we have given false conditions so the loop will go in the second else condition to execute the condition.
  • After going into the second condition we have given a false condition in the first if block, so it will execute the last block of else statement. It will be printing the “stmt 3”.

Python 3 else if statement

If statement is true, the If statement in Python instructs the program what to do. In the event that statement is false, the program simply executes the statements following the if statements.

Example #1

The below example shows python else if statements are as follows.

Code:

py = 15
if (py == 5):
    print ("py is 5")
elif (py == 25):
    print ("py is 25")
elif (py == 15):
    print ("py is 15")
else :
    print ("not found")

Output:

Output 3

  • In the above example, we have declared a variable as py and assigned 15 values to this variable. Also, we have used the else if statement in the else if statement we have used elif statement two times.
  • After becomes a true condition of the statement the output is showing as “py is 15” because we have assigned a value of py variable is 15.

Example #2

  • The below example shows else if statements with the preceding conditions are as follows. We have declared two-variable names as py1 and py2 and assigned 15, 25 values to them.
  • After declaring the value the condition statement is checking the true condition in the first if statement. Then it will executing a true statement block.

Code:

py1 = 15
py2 = 25
if py2 > py1:
    print ("Py2 is greater")
elif py1 == py2:
    print ("py1 and py2 values are same")
else:
    print ("Py1 is greater")

Output:

preceding conditions Output 4

Conclusion

If statement is true, then if statement is executing the statement of block; if the condition is false, then if statement will not execute a set of statements. Python 3 else if statement comprises a block of code that executes. If we combine an else statement with an if statement.

Recommended Articles

This is a guide to Python 3 else if. Here we discuss the definition, What is python 3 else if, Python 3 else if conditionally. You may also have a look at the following articles to learn more –

  1. Python 2 vs Python 3
  2. Python 3 Cheat Sheet
  3. Python 3 Commands

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW