Loops in Python
Introduction to loops in python
In programming loops are structure that repeats a sequence of instruction until a specific condition is met. Loops are most important topic to start any programming language. Lets check why loops in python are so important
Assume you have to print “hello world” for 5 times without using loops and with using loops and see the difference.
Advantages of loops
We can reduce the size of the code and code becomes more readable with loops. If we need to do same operation for 100 times then we can’t do this by manual typing. So we go for loops.
Python for loop is different from other programming languages. For loop is used for iterating through string, list, set, tuple, dictionary . For loop is mostly used as iterator in Python.
For loop can be used in two ways , see the below code for understanding.
Range function in python
range function is mostly used in for loops to traverse the set of code for specified numbers of times.
While loop is a entry controlled looping statement. While loop works on the condition given in the loop. It runs until the given condition is true , once the condition becomes false it exits from the loop.
To understand more on while loop see the below code.
What is loop control statements?
loop control statements are the statements which controls the execution of loops. Types of loop control statements are
used to terminate the loop
used to escape from the present iteration
pass doesn’t effect the code but it is used when you need to statement but not to execute that
Output by using break statement :
Output by using continue statement :