Loops in Python
Rushikesh J.
Software Test Engineer @Vinz Global | Robot Framework | Manual Testing, Automation Testing | Python | Selenium | GIT | JIRA | Immediate Joiner | API Testing Using Postman | Jenkins
Loop: If you want to repeat any statement for specific time, or if you want to generate a number according to your instruction, you can use loop.
Explanation:
?Programming instruction executes linearly in the order in which we declare?
?But we can alter that sequence using different way?
?One of them is Looping way
?If we need to repeat a statement for specific time we can do it.
?For example ‘Python’ repeat 10 time on the screen.?
?“Python” will repeat until its condition is false?
Different Loops in Python
?for loop?
?while loop
For Loop in Python :
?We can generate number in different ways, using for loop
?For example, generate even / odd number from 1 to 10.
?We can repeat a statement or set of statement up to fixed time
?For example repeat ‘Python Programming Language’ 10 times
?We can iterate (getting one by one item) over sequence for example list, tuple, dictionary, string.
Syntax:
for var in range(start number, stop number):
? print(var)
for variable in sequence:
? print(variable)
Example:
for i in sequence:
? print(i)
Where sequence may be any string, list, tuple or dictionary (keys or items)