DEV Community

Cover image for Learn Python #3 - Conditional Statements - if, if elif, and if elif else
Nitin Reddy
Nitin Reddy

Posted on • Edited on

2 1

Learn Python #3 - Conditional Statements - if, if elif, and if elif else

Hello everyone, Today we are going to talk about the if, if elif, and if elif else conditional statement in Python and try to understand the different use cases and different ways of writing the same conditional statement.

What the hell if, if elif, and if elif else is?

In Python to execute the statement based on certain conditions being satisfied, we use the if statement.
Let's look at a few of the examples:-

Example 1:-

going_to_office = True #variable that stores the boolean value
if going_to_office == True: print("Work from office")
Enter fullscreen mode Exit fullscreen mode

Here instead of checking the values of going_to_office against True, we can directly write it as

if going_to_office: print("Work from office")
Enter fullscreen mode Exit fullscreen mode

So if the condition is evaluated to True then the print statement is executed else not.

Example 2:-

Let us say we have

going_to_office = True
if going_to_office: 
     print("Work from office")
else:
     print("Enjoy work from home")
Enter fullscreen mode Exit fullscreen mode

So if the condition on line#1 is not satisfied then the control moves to else and the statement under the else condition is executed.

Example 3:-

Let us say we have the if elif else condition

going_to_office = False
be_at_home = True
if going_to_office: #condition1
    print("Work from office")
elif be_at_home:    #condition2
    print("Enjoy work from home")
else:
    print("Work from Starbucks")
Enter fullscreen mode Exit fullscreen mode

So if the condition1 and condition2 are not satisfied then the statement under the else condition is executed.

Understand

Conclusion

The basic conditional statements like if, if elif and if elif else is easy to understand, so do let me know what you guys think of this article💡.

Thanks in advance for reading this article...🚀

More than happy to connect with you on @twitter, LinkedIn

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️