Python loop- Come to a sequence of data!
In the computer lab, my friend and I doing a repeated task. Then we thought is there any method to do this task easily?
We found something amazing! That was a python loop.
Python loop repeating a task till condition doesn’t satisfied. It’s like the automation of tasks.
The second question is How can you use a loop? What’s the type?
You can use the loop with two methods:-
• for loop
• while loop
1. Python- for loop
Suppose, you want a repeated sequence of elements. How will you do it?
You can do it with for loop. The for loop giving a particular sequence of elements.
The for loop can be used in list, tuple etc.
Here, you can see that the list elements have come in sequence.
Suppose you have to stop at number 20 in for loop and wants to display a message. How will you do it?
You can do it with if conditions.
Here, you can see that the first number 1 passed through for loop. It doesn’t match to if condition so it directly printed.
When 2nd number 20 passed through for loop. It catches the if condition and display the if message.
After, displaying if ‘s message, it will display the number 20.
The third time it will pass the number 45. It directly prints because it doesn’t satisfy the if condition.
• for loop for string
Here, you can see that string “hello” has come in sequence. The string can come in sequence.
• for loop in range() method.
To know how to use the range() method in for loop. First, you have to understand how the range() method works?
Here, you can see that number has started from 0 and end at last-1 I.e. number 3. Number 4 will not be included.
Finally, you had seen how a range method works!
Next, you are going to see how to use it in for loop.
Here, you can see that all the number has come in sequence.
Number 4 is not included. Because range() method does not include 4. The last element will be a last-1 number.
• else method in for loop
Suppose, your loop has finished and you have to display the message for finished.
Here, you can see that after completing the sequence of number message has displayed.
• Nested for loop
Suppose you need one loop inside another loop. The outside loop will be a row and inside the loop will be a column.
The outer loop is a row and the inner loop is a column.
First, the sequence started with the number 0 then inner it will go to numbers 0 and 1.
2. Python- while loop
The while loop executes as soon as the condition is true.
When the condition becomes false the loop will be stopped.
Here, you can see that number starts from 0 and end at number 2.
1. break statement in while loop
The break statement in the while loop breaks the sequence at a specific point.
Here, you can see that sequence has broken in number 2.
The sequence doesn’t reach number 3.
2. Continue statement in loop
The continue statement in for loop change the sequence at a particular point.
The continue statement change the current iteration and start a new one.
3. Else statement in while loop
The else statement in the while loop executes when the condition is no longer true.
Basically, In this section, you had seen that while loop executes till the condition is true.
Loops are used to automate the repeated task in python.
There are two types of loops in python. The first is for loop and the second one is while loop.
The for loop uses for a repeated sequence of elements. The while loop executes till the condition is True.
There are continue and break statements in python while loop.
Break statement in python while loop breaks the sequence of the elements.
Continue statement change the current sequence and start a new sequence.
"Quality brings clarity"
- A new way to format the string in Python 3.6: fstring - October 11, 2021
- Ultimate new guide for python beginners in 2021 - October 3, 2021
- How to download famous thonny easily for python? - September 20, 2021