Categories: If statementPython

if elif else statements in python language

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

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
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

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 python
Example
Example

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

Checking age for voting
you can not vote
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
Example – Calculate electricity bill

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

17000

 

Related post

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.