Python if, else & Elif Statement Learning

In this tutorial, we have a tendency to shall specialize in Python if, else and elif statement.

Decisions are one in all the foremost vital feature of any bug. It essentially helps to consistently and logically execute the program based mostly upon the input of the user (sometimes) or alternative factors that let the user receive the required output relying upon the input.

Decision-making is often provided by creating use of branching structures and loops. These Branching structures help in conditional programming. during this tutorial, you may find out about branching structures.

Python if Statement

An if statement or associate degree if block because it is often same, is beneficial to create choices supported conditions during a program typically based on the user input file.

And if block desires the keyword ‘if’ with the following condition and a group of statements to be dead just in case the condition seems to be True. A condition is usually an expression which might be evaluated either to true or false.

Code

if Condition:
	statement 1
	statement 2
	statement n

Example

a=10
if a>8: 
	print("A is Bigger than 8")

Output

A is bigger than 8

Here, a variable ‘a’ is initialized to ten. Then, associate degree if block is employed with the condition that checks whether or not a is bigger than eight or not. If it evaluates to be true, then the set of statements following that condition are going to be dead and so the program can take off of that exact block and execute a subsequent set of statements.

Note: we have a tendency to typically use indentations to create the program look a lot of legible that follows a far higher logical approach. Here, it’s vital to use indentations because it helps to construct the if block during a logical manner.

Python if-else Statement

The if-else condition is beneficial once you have multiple conditions to be evaluated during a program. Suppose you wish to examine for a selected condition and if that evaluates to false, you’ll then prefer another condition checking to judge it. This helps to supply an improved output supported verification.

In this sort of structure, the program can execute a minimum of one in all the blocks; either if block instead block.

Note: You want to use correct indentations in if-else structure and if you don’t do this, you may get a blunder because the Python Interpreter won’t be ready to perceive the distinction between the if block and else block.

Code

if Condition:
	statement 1
	statement 2
	statement n
else:
	statement 1
	statement 2
	statement n

Example

a=10
if a<8: 
	print("A is Smaller than 8") 
else: 
	print("A is Bigger than 8")

Output

A is Bigger than 8

Here, a variable ‘a’ is initialized to ten. Then, associate degree if block is employed with the condition that checks whether or not a is bigger than eight or not. If it evaluates to be true, then the set of statements following that condition are going to be dead and so the program can take off of that exact block and execute a subsequent set of statements.

Note: we have a tendency to typically use indentations to create the program look a lot of legible that follows a far higher logical approach. Here, it’s vital to use indentations because it helps to construct the if block during a logical manner.

Python if-else Statement

The if-else condition is beneficial once you have multiple conditions to be evaluated during a program. Suppose you wish to examine for a selected condition and if that evaluates to false, you’ll then prefer another condition checking to judge it. This helps to supply an improved output supported verification.

In this sort of structure, the program can execute a minimum of one in all the blocks; either if block instead block.

Note: You want to use correct indentations in if-else structure and if you don’t do this, you may get a blunder because the Python Interpreter won’t be ready to perceive the distinction between the if block and else block.

Code

if Condition 1:
	statement 1
	statement 2
	statement n 
elif Condition 2:
	statement 1
	statement 2
	statement n 
elif Condition 3:
	statement 1
	statement 2
	statement n 
else:
	statement 1
	statement 2
	statement n

Example

a=4
if a==1: 
	print("A is 1")
elif a==2: 
	print("A is 2") 
elif a==3: 
	print("A is 3") 
elif a==4: 
	print("A is 4") 
elif a==5: 
	print("A is 5") 
else: 
	print("A is not in the middle of 1 and 5")

Output

A is 4

Leave a Comment

error: Alert: Content is protected!!