Greetings Learners! Welcome to the new lecture on Python. Today we are moving towards an interesting and important concept. If you have come from a programming background, then you must know about the workings of loops. To make the repetition of the same sequence, we use loops. We are familiar with the sequences in detail as we have learned them in the previous lectures and have received the concept in the recent lecture. If you want to be a programmer, then you can not move forward without a solid base of loops; therefore, in this lecture, we will learn the concept of loops from scratch and then go on to a deep understanding of each loop in the next episodes. So have a look at the concepts that you will learn today.
What are the loops?
How do we understand the concept of loops from scratch?
What are some conditions of the loops that we have to understand before programming in loops?
What are the two basic types of loops in Python?
Discuss the syntax and types of loops.
What is the speciality of the while loop?
Why do we prefer the for loop when dealing with for loops?
What are the major differences that make Python better at looping than other types of programming languages?
Each of these is an important interview question, and will we explore the answers of every question mentioned above. In this lecture, the use of Jupyter notebook is not much but for the practicle implementations, this lecture is very important so let us start finding the answers of each question that we have just read now.
Introduction to Loops in Python
In programming languages, loops are the repetition of the same concept, again and again, to get the required output according to the requirements of the programmer. In other programming languages, there are many types of loops, and programmers use them in different ways. Usually, it has been observed that the programmer uses the same kind of loop most of the time that he finds useful or has a good grasp on the concept of a particular type of loop. Loops are defined as:
"Loops are a fundamental control structure in programming that allows you to execute a set of instructions repeatedly until a certain condition is met."
These are extremely useful to execute the conditions and calculations. The working of loops may be different, and the difference in the syntax is an important thing to be noticed, but keep in mind that the output of the loops is almost the same. No matter which type of loop the programmer uses, the output remains the same, just as at the backend, the working and the logic are the same.
How Do Loops Work in Python
When you are going to learn the loops, many people ignore the first step, which is to understand how the loops work at the backend of the code. We are not going into details about how the compiler executes the instruction, but just the steps that have to be followed by the compiler when the user runs the loop. Here are three basic steps:
Checking the Condition
Checking for the condition created by the programmer. The user always gives the condition at the start of the loop so that the compiler may understand whether the given loop is possible to execute or not. One must keep in mind that the compiler works with the general truths and facts of the real world. It means that if the programmer, say, tells the compiler that 2 is more significant than 10,000, the compiler will reject the condition.
Making the condition is the most important part of the loop for the programmer, and this is the only step where, if you have a strong base in the concept, the programming will become easy for you. This is the only step that will surely run in the loop no matter what condition the programmer applies. It is usually a single line of code, but it is crucial.
Execute the Body
Once the condition is satisfied, the body of the code is executed. This is the main step where you will see if you are successful in the loop or not. Once the condition is met and the outcome is true, the compiler enters the body of the loop specified by the programmer and gives the output. Most of the time, the programmer wishes that the condition becomes true, so the remaining code gets the attention of the compiler.
The whole processing is done in the body, and the compiler can give more than one true condition according to the type of condition and other requirements.
It is true that programmers created unusual and amazing products, but there is a limitation that the fact is never ignored. The programmers use the facts in a better way by using their creativity, which results in perfect outputs.
Coming Out of the Loop
It is obvious that many times, the condition is turned false, and then the compiler simply ignores the body and jumps out of the code. This will only happen if the condition is not satisfied, and this step is not connected with the one that we have just discussed. The detail of each of the steps can be understood by observing the flowchart of the loops given next.
Types of Loops in Python
In other programming languages, such as C++ and C#, there are many types of loops. However, in Python, there are primarily two types of loops, each of which has subtypes. In this lecture, you will understand the basic introduction to both of these, and after that, we will move towards the deep concepts and examples of these loops. So, at the most basic level, we can see that there are two types of loops in Python:
While loop
For loop
An introduction to both of them is given next.
While Loop in Python
The while loop starts with the keyword "while” and then the condition is written so that the compiler can check the condition and then execute the statements written after it accordingly. We introduce the while loop as:
“In programming, the while loop is one of the fundamental types of the loops in which the condition and the statement are the necessary parts of the syntax”
The condition is checked at the start of each iteration, and if it is still true, the loop executes, and this cycle continues until the condition is found to be true. The syntax of a while loop is given next:
Syntax of the While Loop:
while condition
statement(s)
As you can see, the condition is always given first, and the number of statements may vary according to the complexity of the program.
Types of While Loop in Python
If the concept is taken to a deeper level, we will see that while a loop is a simple type of looping in Python, it has further subtypes, and the names of these are
Infinite loop
Else statement
Single statement while loop
The details and examples of each of them will be discussed in the next lecture.
For Loop in Python
The for loop is the basic type of looping, and usually, I find it more convenient to use it. Iterations are usually used with the sequence, where the elements of the sequence are repeated again and again. To control the number of iterations, the programmer sets a condition with the “for” keyword, where the compiler always checks the condition and decides whether it has to enter the body or not. Just like the while loop, it can also have more than one statement, and the control is totally in the hands of the programmer.
Syntax of For Loop in Python
For iterator in sequence:
statement(s)
Here, you have to understand some concepts:
“For” is the keyword that initiates the loop and tells the compiler to work according to the information fed into the backend.
An iterator can be thought of just like the variable name and the programmer may call it anything according to his choice. Let us say, if it is a, then he will call it in the statement with the same name.
“In” keywords specify that the name of the iterator is now ended and the compiler is now entering into the sequence.
The sequence may be of any type no matter if it is a tuple, array, or list. The good thing about for loop is, it is associated with the sequences and according to the requirement, the programmers can access each and every member of the sequence without making much effort.
Statements may be one or more and it is important that every word of the sequence is correct so that the compiler will execute the instruction and then move towards the for keyword to check the condition again.
Difference Between Python Loops with Other Languages
From the beginning, we have been saying that Python is simpler than other languages. Moreover, there are other characteristics that make it unique and better. As we are studying the loops so I have made a little comparison between the loops of Python with other programming languages.
Sr# |
Name of the Loop |
Python |
Other Programming Languages |
1 |
For loop |
In Python, the for loop is more versatile because it gives the independence to use any data type whether it is numeric of the string, list, or tuple |
The other programming languages deal with the for loop in just a number of way. It means only the integers are allowed to feed into the for loop of other programming languages. |
2 |
Range function in for loop |
The range function is used in collaboration with the for loop. In Python, it is more concise and dealing with the range function in Python is easier than in other languages, |
In other programming languages, the range function is less concise. The programmers can also use the same function in other languages as well, but dealing with this function is more difficult than in Python. |
3 |
While loop |
The while loop in Python is almost the same as the other languages, but the syntax is different. |
In other programming languages, the while loop works the same as the Python as at the backend, the conditions and workings are the same. |
4 |
Break loop |
Only syntax is different |
Only syntax is different |
5 |
Identation |
It is an important point about Python that makes it unique among others. The identifier is mandatory at some points in Python loops, but in some areas, the unnecessary indentation causes the error. |
The spaces and indentations in other programming languages do not result in any change in the output of the compiler. |
Hence, by using these points, we can conclude that, like every programming language, Python also has many features that make it unique. The loops are more specific and more interesting in Python than in other programming languages, but we have less variety in the loops in Python.
The range function and other functions that have been mentioned above will be discussed with you in detail in the next episode of this lecture. Till then, let us revise the concepts that we have discussed. The concept of loops is important in Python,n and there are only two major types of loops that we have also introduced. The subtypes of these loops also exist. The working of the loops is easy in Python, and we have seen that not all loops have the same syntax, but the differing syntax is important as it differentiates the languages just like other syntaxes. There are three basic steps that are followed by the loops and while constructing the loops, these steps must be cleared in your mind. This was all for today, and in the next episode, you will see the practical implementations of the loops in Python one after the other. Then, try to practice these concepts and keep learning.