Skip to main content

Command Palette

Search for a command to run...

Conditional statements in Python

Published
3 min read
Conditional statements in Python
A

I'm an aspiring Software Engineer specializing in web development. I have built various web apps and utilities using Javascript, ReactJS and NodeJS technologies.

I'm currently working on a full-stack project named JU Query, an online platform for University students to connect with peers, juniors and seniors, ask and answer doubts, help each other out as a growing community.

I'm a polyglot programmer in diverse languages such as C, C++, Java, Python, JavaScript and TypeScript.

I'm also a blogger where I have published various posts on programming, python language and course works to share my knowledge and spread awareness. I have received more than 10,000+ reads from platforms like Medium, Dev and Hackernoon.

I love to contribute to the world through community work. I'm a community leader where I spread awareness, promote and educate students on how to code. I have personally taught Internet basics and front end web development for over 400+ students. I have additionally participated in several initiatives as an NSS volunteer.

I'm a passionate learner who has gained knowledge from numerous events and workshops. I have learnt about Aeromodelling, Rover sensor project, Smart devices using the internet of things. And I have also won the runners up position in the open day competition conducted by IIAEM for building a cost-efficient Hydraulic Arm.

My hobbies include sketching, graphic design, reading books, and watching selective movies.

In programming, conditions are what give a language the power to showcase intelligence, by controlling the flow of a program.

That’s why conditional statements are also referred to as Control flow tools in a programming language.

And python uses the usual flow control statements known from other programming languages, with some twists.

Let’s start with the basic one:

if statement

Output:

Please enter your age: 19  
You are allowed to view R-rated series on Netflix

The above code is a good way to check one’s age and display an action to be performed, but wouldn’t be nice to have an alternate message pop up if one’s age is under 18.

That’s where we may use elif or else statements, and let’s look at how an if-else combination would look like:

if else statements

Output:

Please enter your age: 17  
Under 17 requires accompanying parent or adult guardian to view R-rated series on Netflix

As you can see this code provides better context than the previous one and delivers an ‘either or’ output based on the given user input.

What if we level up this code by giving in multiple inputs and writing some sophisticated conditions so that we get to check with the rating and corresponding age limit.

This is where we get to use them if … elif … elif … else sequence of conditional statements:

if … elif … else statement

Output:

Please enter the film rating: NC-17  
Please enter your age: 17  
You are not allowed to view the film

As you can see this code, it checks the age and rating corresponding to the input to provide you with accurate details on parental guidance. Cool, right?

And, by the way, if you’re wondering about the code having multiple if … elif … if … elif statements, don’t worry.

I’ve used something called nested conditional statements where one can write if .. elif .. else statements inside any of the if elif else statements by nesting under each other.

A Quick Note:

  • There can be zero or more elif parts, and the else part is optional.

  • The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation.

  • An if … elif … elif … sequence is a substitute for the switch or case statements found in other programming languages.

Who Am I?

I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my craziness categorized by platforms under one place: https://linktr.ee/AswinBarath

Join me to learn Python!

Checkout my Python Programming Series where my mission is to share my knowledge on Python: https://aswinbarath.hashnode.dev/series/python-programming

Learn what I know about Python from any of my favourite knowledge sources:

Keep Learning

Now, I guess this is where I say GoodBye👋.
But, hey it’s time for you to start learning with your newfound Knowledge(Power)👨‍💻👩‍💻 .
Good Job that you made it this far 👏👏
Thank you so much for reading my Blog🙂.

More from this blog

Aswin Barath Hashnode Blog

21 posts