if else in python

The if else in Python is a part of flow control. In this section we will try to understand how the if-else block works in Python.

If you are new to Python, you may visit the other beginner friendly articles in the Python Primer category.

Flow control plays an important role not only in Python programming but in any programming language. 

For example, we can assume a simple model based on our day-to-day experience.

If someone asks us to go to a place that the stranger does not know, what do we do?

In general, we tell that person many possibilities such as, you can turn left, then turn right, and then you will find a bus whose number is such and such, etc.



If you are a complete beginner your journey to learn TensorFlow might start from here.

For the TensorFlow beginners we have a dedicated category – TensorFlow for Beginners.
But besides that, you may need to learn several other machine learning and data science libraries.

As a result, you may check these categories as well – NumPy, Pandas, Matplotlib.

However, without learning Python, you cannot learn the usages of these libraries. Why? Because they all use Python as the Programming language.

Therefore please learn Python at the very beginning and start learning TensorFlow.

And, finally please check our Mathematics, Discrete Mathematics and Data Structures categories specially. We have tried to discuss from basic to intermediate level so that you can pick up the core ideas of TensorFlow.


Therefore, in our life, there are different possibilities that always coexist. 

In programming languages the same thing happens. 

If the condition is true, execute, else don’t execute. 

Certainly, this number of steps might be complicated. But it depends on how we look at the problem.

Let’s see a simple code that will explain a lot.


# if the condition is true the code block executes
# else the next block executes

number = 100
check = 99

if number > check:
  print(f"The {number} is greater than {check}.")

# output
The 100 is greater than 99.

The main philosophy of the if block states that condition should be true. 

If the condition is false, the if block never works.

However, what happens when the condition is false? The else block comes to save us.

number = 100
check = 199

if number > check:
  print(f"The {number} is greater than {check}.")
else: 
  print(f"The {number} is less than {check}.")

# output
The 100 is less than 199.

However, apart from this simple true and false game, there might be other possibilities. Right? 

A number could be greater than another number, or less than another number, and finally it could be equal to that number. 

Between the if else in Python, there is always another possibility and we tackle that block with elif.

Let’s see the code.

number = 100
check = 100

if number > check:
  print(f"The {number} is greater than {check}.")
elif number == check:
  print(f"The {number} is equal to {check}.")
else: 
  print(f"The {number} is less than {check}.")

# output
The 100 is equal to 100.

As we can guess, the if-elif-else blocks could be very complicated in some cases.

There could be different layers of such blocks. 

So in the next section, we will discuss the nested if-else block. 

For more such Python Primer code, please visit the respective GitHub repository.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

C, C++, Java and Game Development

Twitter

Comments

One response to “if else in python”

  1. […] our previous section we have seen examples of if-else. In this section, we will take a look at Python while […]

Leave a Reply