Understanding the conditional Statement and Loop of python

From my last tutorial, we have a good basic understanding of python for example How to install to get started, how to declare the variables and what are the data types are available in python. I recommended checking the last tutorial for better understanding. Here are the links Introduction to python to get started, Variables declaration of python and Data types of python.

In this tutorial, we will learn the conditional and loop flow of python. Let’s get started to understand the basic concept of conditional and loops. Using conditional statements, we can write Python code that makes decisions and repeats actions.

1. If else and elif

The if… statement is used to evaluate whether a condition is True or False and, depending on the result. Suppose we want to write a program, that will determine whether a number is greater than other. In python every condition or loop it should end with colon :

Result: 20 is greater than 10

If we do not write or execute in else condition then we can use  pass . For example.

Now come to see another example to understand the elif condition.

The execution of the program will start from the first statement, if the first statement is true then it will execute else it will go to check the elif condition otherwise else condition.

Result: 30 is the greatest number

2. While loop

Python while loop is used to repeatedly execute some statements till the condition is true. It means use the while loop to check a condition before each execution of the loop.

Here while loop always checks the condition before executing the statement.  In python, there is no do while loop.

Result :

********While loop test**********
1
2
3
4

In the above example, we can write the else condition also, if the while condition is failed, it will execute the else condition. This is the good feature in python. Let’s check the same example with else condition.

3. For loop

Python’s loop is more flexible than that of other languages. We can do more interesting things here. Python’s for loop is versatile. First, let’s check the example of the range which can execute like for loop.

Output :

********For loop range test**********
1
2
3
4

Similarly, we can see the example of the sequence for the loop.

As I above mention for loop is very versatile in python. It means we can use reverse order traverse or we can traverse the loop based on sorted items. Let’s check both for an example.

Output:

********For loop reverse test**********
5
4
3
2
1

Let’s check the item traverse based on the sorted item.

Output:

********For loop sorted test**********
1
2
3
4
5

Now the question arises here if you are java or other language know? how can use the index based traverse the item? So I can say python is so smart language. yes, you can iterate with index and value both together.  Let’s see this as an example.

Output :

********For loop index and value test**********
value of 0 is = 1
value of 1 is = 2
value of 2 is = 3
value of 3 is = 4
value of 4 is = 5

4. Break and Continue

Python break and continue statements are used only in loop. It helps to modify loop behavior. Suppose you’re executing a loop, at one phase you need to terminate the loop or skip some statements then you need these statements.

Output:

********For loop break**********
1
2
3
break

Let’s check the same example by using continue for the loop with skip an item.

OutPut:

********For loop continue**********
1
2
skipped 3
4
5

Now we have a good understanding of the loop and conditional statement of python. In my next tutorial, we will learn basic understanding about the function in python.

If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you.

Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on below section.

Happy coding 🙂

 

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.