Conditional Declarations with Python Implementation

Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp

Contents

This article was published as part of the Data Science Blogathon

Introduction

Decision making is as important in any programming language as it is in life. Decision making in a programming language is automated by conditional statements, in which Python evaluates the code to see if it meets the specified conditions.

Conditions are evaluated and processed as true or false. If this is determined to be true, the program runs as needed. If the condition is determined to be false, the statement that follows the If condition is executed.

Python has six conditional statements that are used in decision making: –

1. If the statement

2. If another statement

3. Nested if statement

4. And … elif ladder

5. Short hand if statement

6. Short hand if-else statement

26444image_750x_5da4702b7dc57-2554176

Image source: Link

Let's take a look at how each of them works.

Yes statement

The If statement is the most fundamental decision-making statement, in which the code executes based on whether it meets the specified condition. It has a body of code that only executes if the condition in the if statement is true. The declaration can be a single line or a block of code.

The if statement in Python has the following syntax:

if expression
 Statement

#If the condition is true, the statement will be executed.

Examples for better understanding:

Example 1

num = 5
if num > 0:
    print(on one, "is a positive number.")
print("This statement is true.")
#When we run the program, the output will be:
5 is a positive number.
This statement is true.

Example – 2

a = 25
b = 170
if b > a:
  print("b is greater than a")
output : b is greater than a

If it's another statement

This statement is used when specifying that both the true and false part of a given condition will be executed. When the condition is true, the statement inside the if block is executed; if the condition is false, the statement is executed outside the if block.

The if statement … Else in Python has the following syntax:

 if condition :
              #Will executes this block if the condition is true
    else :
              #Will executes this block if the condition is false

Example for better understanding:

num = 5
if num >= 0:
    print("Positive or Zero")
else:
     print("Negative number")
output : Positive or Zero

If … Elif..else Statement

In this case, the If condition is evaluated first. If it is false, the Elif instruction will be executed; if it is also false, the Else instruction will be executed.

The If… Elif..else statement in Python has the following syntax:

if condition :
    Body of if
elif condition :
    Body of elif
else: 
    Body of else

Example for better understanding:

We will check if the number is positive, negative or zero.

num = 7
if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")
output:  Positive number

Nested IF declaration

A nested IF statement is one in which an If statement is inside another If statement. Used when a variable needs to be processed more than once. If statements, If-else y If … elif … else can be used in the program. In nested If statements, the sangria (blank space at the beginning) to determine the scope of each statement must take precedence.

The nested if statement in Python has the following syntax:

if (condition1):
#Executes if condition 1 is true
if (condition 2):
  #Executes if condition 2 is true
  #Condition 2 ends here
#Condition 1 ends here

Examples for better understanding:

Example 1

num = 8
if num >= 0:
    if num == 0:
        print("zero")
    else:
        print("Positive number")
else:
    print("Negative number")
output: Positive number

Example 2

price=100
quantity=10
amount = price*quantity
if amount > 200:
    if amount >1000:
        print("The amount is greater than 1000")
    else:
        if amount  800:
            print("The amount is between 800 and 1000")
        elif amount  600:
            print("The amount is between 600 and 1000")
        else:
            print("The amount is between 400 and 1000")
elif amount == 200:
    print("Amount is 200")
else:
    print("Amount is less than 200")
The output : “The amount is between 400 and 1000.”

Short hand if statement

Short Hand if statement is used when only one statement needs to be executed within the if block. This statement can be mentioned on the same line that contains the If statement.

The if Short Hand statement in Python has the following syntax:

if condition: statement

Example for better understanding:

i=15
# One line if statement
if i>11 : print (“i is greater than 11″)
The output of the program : “i is greater than 11.”

Short hand if-else statement

Used to mention If-else statements on a line where there is only one statement to execute in if and else blocks. In simple words, if you only have one statement to execute, one for if and one for another, you can put them all on the same line.

Examples for better understanding:

# single line if-else statement

a = 3
b = 5
print("A") if a > b else print("B")
output: B

# single line if-else statement, with 3 terms

a = 3
b = 5
print("A is greater") if a > b else print("=") if a == b else print("B is greater")
output: B is greater

Summarize,

· If the condition is used to print the result when only one of the listed conditions is true or false.

· When one of the conditions is false, the If-else condition is used to print the statement.

· When there is a third possible outcome, Elif statement is used. In a show, any number of Elif conditions can be used.

· By declaring all the conditions in a single declaration, we can reduce the amount of code that needs to be executed.

· Nested if statements can be used to nest one If condition inside another.

Conclution

If you are reading this, you are most likely learning Python or trying to become a Python developer. Learning Python or another programming language begins with understanding the fundamental concepts that form its foundation..

At the end of this text, you should understand the various If else conditions used in Python.

About the Author

Prashant Sharma

Nowadays, I am pursuing my Bachelor of Technology (B.Tech) from the Vellore Institute of Technology. I am very excited about programming and its actual applications, including software development, machine learning and data science.

I hope you like the article. If you want to connect with me, you can connect in:

Linkedin

or for any other questions, you can also send me an email

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.