Data Science

Conditional Statements in Python

9 min read
avatar
Ailead Technologies
Share
AILEAD TECH

Introduction

Were there times you were programming and desired you could write it in a very clever way?

The use of conditional statements in coding is a way of programming your decisions and thought processes. Conditional statements are a technique for instructing your code to operate in a specific manner, like "If this happens, then do that." Is this not surprising? Let's learn it together.

If Statements

The if statement is basically how your program says, "If this condition is true, then go ahead and do that particular thing." It's one of the most simple concepts in programming, but at the same time, it can be so loaded with meaning and power depending on how you use it.

 

Here’s a quick example ⬇️

In this line of code above, Python makes comparison on the value of x with the value 5 where it tests for greater than. Should the input be greater than five, you’ll have “x is greater than 5” displayed on the screen. If not, Python simply passes it by without bothering to consider It.

Elif Statements

When you have many options that you can check during coding, the elif is utilized. Since “elif” means “else if “. It is more of a tool for your program to flex one test after the other as you conduct your assessment. If the first condition is not true then the program goes to the next step of the loop and checks and continues it until the right condition is found. Again, this makes it easier dealing with conditions where there are several ways of proceeding.

Dealing with several outcomes in your python is made extremely easy by this.

Let see this example ⬇️

Here’s what’s happening above:

First, Python compares x bigger than 15.

If true, it runs that block and stops it.

If not, it goes to else part and works for elif x == 10.

If none of these is true then comes the else block of the program.

 This is similar to the elimination process in that you decide one outcome after another until you locate the right one for you.

Else Statements

The else statement is what will happen if none of the conditions in the if statement are met. If none of the ‘if’ or any of the ‘elif’ conditions has been met then the else statement can be used.

See this example ⬇️:

In the example above, we are asking Python the question ‘Is `x > 5’?.

If this condition is not met, Python goes to the else block and print out “x is 5 or less”.

You see how simple it is?

 

Now lets talk about Nested Ifs

Nested Ifs

What if you want to check conditions inside other conditions? That's when nested if statements are used.

 See this example Below⬇️

What’s happening above again?

1. Python checks first if `x > 5`. If this is true, it enters that block.

2. The first block contains another `if` to check whether `x` is even.

3. If both are true, it will display both messages.

Nested `if` statements allow you to create more detailed logic, but don't make them too deep; it can become confusing fast.

Condition statements are an important part of Python logic; they allow the code to change based on different situations and respond in the right way. Try all this statements in your next project. These condition statements  will become  familiar as you keep on practicing and reading more of our blog posts.

Thats not all there is to your coding, if you want more great tips and tricks,join to our newsletter and Fundamentals of Data Science and AI community to get access to special Python tutorials and inside knowledge on how to become a python pro. It only takes a minute, and you will be glad.Click here to Join now.

 

 

 

 

 

 

 


avatar
Ailead Technologies
Share
WhatsApp