Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

if elif else statements in python language

Posted on October 1, 2017January 24, 2020

Table of Contents

  • if elif else statements in python language
    • if condition in python
      • python if else
      • Flow diagram
      • python if elif else 

if elif else statements in python language

In this tutorial, we will discuss the concept of “if” “elif” “else” statements in python language.

In this post, we are going to learn how to use the if-else statements to decision making

if condition in python

if test_expression:
    statement(s)

Flow diagram of the if-else statement in Python

if elif else statements in python language
Flow diagram

 

Here, first, the program evaluates the test_expression, only when the test_expresssion is true, The statement will be executed

if the test expression is false. The flow of control exit from the if body and skipped the execution

age=19
if age>18:
     print("you are a teen age boy")
print("age is always checked")
z_score=1.1;
if z_score>1.00:
       print("your Z_score enough ")
print("you can enter uyniversity")

When we executed the above program, it produces the following result

you are a teen age boy
age checking is complete
your Z_score enough 
you can enter uyniversity

 

python if else

If… elif…else.. statement in python are used for decision making.

 Syntax of the if-else statement is
 
if elif else statements in python language
Syntax

Flow diagram

When the test_expression is true,body of if statement(s) is executed.  When the test_expression is false, it executes else part of statements.

When we executed the above program, it produces the following result

Checking voting availability

you are allowed to voting

 

In the above example, age is declared and initialized as 32

The test expression is true and body of if (statement(s)) is executed and loop-control exits from the loop (else part is skipped)

 

python if elif else 

if elif else statements in python language
Syntax

 

When the test_expression1 is true, execute the body of if statement(s).
When the test_expression1 is false, evaluate test_expression2.
When the test_expression2 is true, execute the body of elif_1 statements,
When the test_expression2 is false, evaluate test_expression3.
When the test_expression3 is true, execute the body of elif_2 statements,

When the test_expression3 is false, execute else part statements.

Flow diagram
if elif else statements in python language
If elif else python
Example
if elif else statements in python language
Example

When we executed the above program, it produced the following result

Checking age for voting
you can not vote
if elif else statements in python language
Example

When we executed the above program, it  produced the following result

nine
Write a program to calculate electricity bill  using if elif else statements
if elif else statements in python language
Example – Calculate electricity bill

When we executed the above program, it produced the following result

17000

 

Related post
While Loop in Java      while Loop in C++     While Loop In C

Do-while Loop in Java    Do-while Loop C++     Do-while loop in C 

For Loop in C++       For Loop in C         For loop in java

If condition C++       If condition in Java   If condition in C

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes