Jul 01, 2022 iOS

New Ideas on How to Use If Else Statement in Python Efficiently

New Ideas on How to Use the If Else Statement in Python Efficiently :Certain prerequisites must be met for a program to compile. And the Python if-else statement is a useful tool to achieve this. Python’s if-else statement determines if a code is true or false. If a condition is true, the compiler executes a statement; if it is false, the alternative statement or condition runs.

How can you get started with the if-else conditions in Python? You will discover how to use the Python if-else statement in this tutorial.

Understanding the If Else Statement in Python

Do you recall doing arithmetic exercises that require the greater-than or less-than sign?

Implementing the if-else statement in Python won’t be difficult if you know how to use the logical conditions from high school mathematics lectures.

  • Less than: a < b
  • Greater than: a > b
  • Less than or equal to a <= b
  • Greater than or equal to a >= b
  • Equals: a == b
  • Not equal: a != b

Indentation Rule 

The indentation rule, as it is known in the language, is applicable when using the Python if-else statement. This indicates that after an “if” statement, there should be whitespace before the next line of code.

Compare these two codes for clarification.

X = 45
Y = 12

if X > Y:
print(" X is greater than Y")
X = 45
Y = 12
if X > Y:
   print(" X is greater than Y") 

The first instance will result in a mistake ( IdentationError: expected an indented block). Keep a close eye on the indentation rule because it’s crucial when using Python’s nested if-else expressions.

Example: How to Use if-else Statements in Python

Let’s use a simple example to understand how to use the if statement.

a = 20

if a > 50:

   print(" yes!!!! you are eligible")

print(" this is outside the block, and explains what we talked about in indentation")

#output

this is outside the block and explains what we talked about in indentation

Using the Python if-else statement

a = 0.5
b = 0.1

if a > 5:

    print(" yes!!!! you are eligible")
else:
   print(" this was printed because", a, "is not greater than", b) 

#output

this was printed because 0.5 is not greater than 0.1

Using the Python nested if statement 

a= 10
b = 3

if a > b:
   if a%2==0:
      print(" a is an even number") 
   print(" a is greater than b")
else:
   print(" this was printed because", a, "is not greater than", b, "and", a, "is not an even number") 

print(" this is outside the block, and explains what we talked about in indentation")

#output

a is an even number

a is greater than b

this is outside the block and explains what we talked about in indentation

What is the Elif Conditional Statement? 

Python uses the Elif keyword to execute another statement if the prior statement is not true. 

For example;

a = 13 
c = 11
b = 0.13 
if b > a:
  print("obi is a male child" )
Elif c%2==1:
  print("obi is a female child" )

#output

obi is a female child

What is the Short Hand python If else statement? 

The shorthand Python ”ïf-else” is a good choice if you want to rapidly verify a condition in a line of code so you can use the output to continue your work. It can be placed on the same line as the if statement, which is why it is known as abbreviated if-else.

Example of a shorthand if-else statement;

(I) 

a = 12
b = 39
print("eligible") if a > b else print("ineligible ")

#output

ineligible

(ii)

a = 33
b = 30

print("eligible") if a > b else print("null ") if a b else print("ineligible ")

#output

eligible

You can use an if-else conditional statement in Python each time you want to solve problems that will require two outputs(true or false).

Index