Day #14 - If Else Conditional Statements in Python

Day #14 - If Else Conditional Statements in Python

ยท

4 min read

Introduction

Welcome to my 14th blog post. On day 14 of my python coding journey, I learnt about conditional if else statements and their categories. Let's dive deep into the details

So let's get started......

Conditional statements

Suppose we want to evaluate a conditional expression, i.e if the statement is TRUE or NOT i.e FALSE, then in that case we make use of conditional expression if...else statements.

Let us take a generic example

For instance, let us say that IF it is raining I will carry UMBRELLA else I wont carry anything.

So if the expression evaluates to FALSE then in that case the false statement is taken into consideration and its plan is executed.

Based on the above explanation, the conditional expressions are divided into 4 categories viz:

  • if

  • if-else

  • if-else-elif

  • nested if-else-elif.

let us look in detail how each statement works

If....else statement

if the expression evaluates to True:

In this case the statement inside of True is executed and the control moves out of if....else block

if the expression evaluates False:

In this case the block of code inside else statement is executed and after execution return to the code out of the ifโ€ฆโ€ฆelse block.

Example:

applePrice = 210
budget = 200
if (applePrice <= budget):
    print("Alexa, add 1 kg Apples to the cart.")
else:
    print("Alexa, do not add Apples to the cart.")
Alexa, do not add Apples to the cart.

Elif Statements

Sometimes, the programmer wants to evaluate more than one condition, this can be done using an elif statement.

Working on an elif statement

Execute the block of code inside if statement, if this expression evaluates to True. then return the control of the elif block.

Execute the block of code inside the first elif statement if the expression inside it evaluates True. Then return to the code out of the if block.

Execute the block of code inside the second elif statement if the expression inside it evaluates True. Then return to the code out of the if block....Execute the block of code inside the nth elif statement if the expression inside it evaluates True. After execution return to the code out of the if block.

Execute the block of code inside else statement if none of the expression evaluates to True. After execution return to the code out of the if block.

Example -

num = 0
if (num < 0):
    print("Number is negative.")
elif (num == 0):
    print("Number is Zero.")
else:
    print("Number is positive.")
Number is Zero.

Nested if statements

We can use if, if-else, elif statements inside other if statements as well.

Example:

num = 18
if (num < 0):
    print("Number is negative.")
elif (num > 0):
    if (num <= 10):
        print("Number is between 1-10")
    elif (num > 10 and num <= 20):
        print("Number is between 11-20")
    else:
         print("Number is greater than 20")
else:
    print("Number is zero")
Number is between 11-20

Resources Used

You can watch the video of Day#14 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. Took another step of learning python with Conditional statements in python.

Thank you if you read this post and have found this post useful of my learnings from Day #14 of python

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย