Python if else

Python Conditional Statements

Conditional Statements are special operations in Python that evaluate either the True or False state of the condition. The condition statement enables the program to make some decisions, which means how to answer each response that it gets. It is not possible to track all outcomes before, and these conditional statements are there to rescue. Different conditional statement options are available in Python and can work on different programs a developer can code. The conditional statements are straightforward to write, easy to add and change, and can handle many scenarios inside a program. When it comes to working with these conditional statements, there will be three options that you can work with. These are the if statement, the elif statement, and the if-else statement.

What are "If…else" statements in Python?

The "if..else" statements are used in Python to control the flow of the code. With the "if..else" statements, we can give our program the ability to decide what to do in a given situation.

The working of "if…else" statements is pretty simple. We write a condition after the if statement and some code inside the if block. If the condition holds true, then the code inside the if block is executed. We can also define an else block after the if block. When the condition after the if statement does not hold True i.e it returns False, then the code inside the else block is executed. This is simply telling the computer

"If this happens, do this

Or else,

Do this"

For example

			

my_number=2
if my_number > 0:
	print("Number is Positive")
else :
	print("Number is Negative")

				

Output:

Number is Positive

Here, the if condition holds True, therefore the code inside the if block is executed.

			

my_number=-2
if my_number > 0:
	print("Number is Positive")
else :
	print("Number is Negative")

				

Output:

Number is Negative

Here, the if condition is False, therefore, the code in the else block is being executed.

Chaining Multiple If statements

We can chain multiple if..else statements using the if..elif…else statement. elif stands for "else if". It works by checking the condition after the if statement. If True, then if block is executed. If it is False, then the condition after the elif statement is checked, If True then if block is executed. If it is False, then the succeeding elif statement is checked, and so on. If no elif condition holds True then the else block (if specified in the code) is executed.

For example

			

my_number=-5
if my_number > 0:
	print("Number is Positive")
elif my_number < 1:
	print("Number is Negative")
else:
	print("Number is 0")

				

Output:

Number is Negative

Nested If else statements

If..else statements can be nested. That means we can add an if..else statement inside the block of another if…else statement. It works by checking the condition after the outer if statement. If True, then the inner condition is checked, and the code of the inner block is executed. If the inner condition is false, then the inner else statement (if specified in the code) is executed. If the outer condition is False, then the outer else block (if specified in the code) is executed.

We will better understand this with the help of an example

			

my_number = 10
if my_number >= 0:
	if my_number == 0:
		print("Number is Zero")
	else:
		print("Number is Positive")
else:
	print("Number is Negative")

				

Output:

Number is Negative

Python range() Function

The range() function is commonly used to create a sequence. It takes three arguments: start, stop and step size. The start and step size arguments are optional, where the default value of start is taken as 0, and the default value of step size is taken as 1. The range() function creates a sequence of numbers between the start and stops (excluding the stop) with step size as the difference between the consecutive numbers in the range. For example

			

	for num in range(10):
		print(num)

				

Output:

0 1 2 3 4 5 6 7 8 9

break, continue and pass in Python

break statement terminates the execution of the loop. It stops the else block from executing.

			

num=2
while num<10:
	print(num)
	num+=1
	if num==5:
		break
else:
	print("Else block executed")

				

Output:

2 3 4

continue statement forces the loop to skip to the next iteration. It skips the code after the continue statement just for the current iteration of the loop.

			

fruits = ["Apple", "Mango", "Banana", "Watermelon"]
for fruit in fruits:
	if fruit == "Banana":
		continue
	print(fruit)
else:
	print("All the fruits have been traversed")

				

Output:

Apple Mango Watermelon All the fruits have been traversed

pass is used as a placeholder. It does nothing on its execution.

			

fruits = ["Apple", "Mango", "Banana", "Watermelon"]
for fruit in fruits:
	if fruit == "Banana":
		pass
	print(fruit)
else:
	print("All the fruits have been traversed")

				

Output:

Apple Mango Banana Watermelon All the fruits have been traversed