Python : Conditionals


if-else statement example in python

if-else Statements

We can use the concept of branching to have our code alter its execution sequence depending on the values of variables. We can use an if statement to evaluate a comparison. We start with the if keyword, followed by our comparison. We end the line with a colon. The body of the if statement is then indented to the right. If the comparison is True, the code inside the if body is executed. If the comparison evaluates to False, then the code block is skipped and will not be run.

def is_positive(number):
if number > 0:
return True
return False
Output:is_positive(-5) will return False
is_positive(0) will return False
is_positive(13) will return True

We just covered the if statement, which executes code if an evaluation is true and skips the code if it’s false. But what if we wanted the code to do something different if the evaluation is false? We can do this using the else statement. The else statement follows an if block, and is composed of the keyword else followed by a colon. The body of the else statement is indented to the right, and will be expected if the above if statement doesn’t execute.

elif Statements

Building off of the if and else blocks, which allow us to branch our code depending on the evaluation of one statement, the elif statement allows us even more comparisons to perform more complex branching. Very similar to the if statements, an elif statement starts with the elif keyword, followed by a comparison to be evaluated. This is followed by a colon, and then the code block on the next line, indented to the right. An elif statement must follow an if statement, and will only be evaluated if the if statement was evaluated as false. You can include multiple elif statements to build complex branching in your code to do all kinds of powerful things!

def number_group(number):
if number > 0:
return "Positive"
elif number < 0:
return "Negative"
return "Zero"
print(number_group(10)) #Should be Positive
print(number_group(0)) #Should be Zero
print(number_group(-5)) #Should be Negative




Follow my "TECHIE" journey and hope you learn something on your way!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Docker API with Spring Boot

Docker API with Spring Boot

Essay - How it all started

7 Basics You Must Know For Your First Programming Interview

Strategy Design Pattern in Depth

Litecoin MimbleWimble April Update

Getting Started With Amazon Braket

User case of OpenMLDB on anomaly detection of transaction system in AIOps field

Top iOS App Development Trends to Watch in 2022?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Follow my "TECHIE" journey and hope you learn something on your way!

More from Medium

Code with Python

HARMAN Python Training Day2

Python — Advent of Code: Day 1

Why I Like Python