for loop in py (Python) with 7 different examples.

Introduction.

for loop in py image only

Loop in a programming language is a fundamental concept. And the for loop in py (Python) is one of the important concepts.

for loop in py (Python) iterates over the items of a sequence and again executes the same block of statements.

With the for loop, we can execute a set of statements, once for each item in a list, tuple, set etc.

In this article let’s cover the 7 different ways to use for in py (Python). We are going to use a Command-line Python Shell to execute the examples.


1. Print each subject in the subject list using for loop in py.

So here we are using the list in python, and in the list, there will be subjects.

>>> 
>>> subject = ["english", "hindi", "marathi", "sanskrit"]
>>> for sub in subject:
...     print(sub);
... 
english
hindi
marathi
sanskrit
>>> 

2. looping through a string.

Suppose you have the string “english”, then it will separate the letters and print them into the new line. Let’s understand this with an example.

>>> 
>>> for i in "english":
...     print(i)
... 
e
n
g
l
i
s
h
>>> 

3. Print the subject Key in a dictionary using for loop in py.

So dictionary in python has a key-value format, we are going to use the subject: marks

>>> 
>>> subject = {'english': 60, 'hindi': 45, 'marathi': 70, 'sanskrit': 90}
>>> for sub in subject:
...     print(sub);
... 
english
hindi
marathi
sanskrit
>>>

Now let’s print the value of the key, there are 2 ways to do that!

>>>
>>> subject = {'english': 60, 'hindi': 45, 'marathi': 70, 'sanskrit': 90}
>>> for sub in subject:
...     print(subject[sub]);
... 
60
45
70
90
>>>

So here we have fetched the value by the key indexes, now let’s try using the value() method.

>>> subject = {'english': 60, 'hindi': 45, 'marathi': 70, 'sanskrit': 90}
>>> for sub in subject.values():
...     print(sub);
... 
60
45
70
90
>>>

4. Use break statement using for loop.

So with the help of a break statement, we can stop the loop and it will not go further.

>>> subject = ["english", "hindi", "marathi", "sanskrit"]
>>> for sub in subject:
...     if sub == "marathi":
...             break
...     print(sub)
... 
english
hindi
>>>

If we got the character “marathi” then break the loop, So that’s why we got “english” and “hindi”.

5. Use continue statement with for loop.

The continue statement helps us to stop the iteration and with the next item.

>>> subject = ["english", "hindi", "marathi", "sanskrit"]
>>> for sub in subject:
...     if sub == "marathi":
...             continue
...     print(sub)
... 
english
hindi
sanskrit
>>>

So continue statement stopped at character “marathi” and then print the next item that is “sanskrit”.

6. range() function with for loop in py

So the range() function returns a sequence of numbers, by default it starts with 0 and increments with 1.

>>> for ran in range(5):
...     print(ran)
... 
0
1
2
3
4
>>>

Now suppose we have to print the numbers in the range of 1 to 9, then we will use the range(1,10).

>>> for ran in range(1,10):
...     print(ran)
... 
1
2
3
4
5
6
7
8
9
>>>

Now suppose we have to do increment with 2, then use range(1,10,2).

>>> for ran in range(1,10,2):
...     print(ran)
... 
1
3
5
7
9
>>>

7. Use else clause with for loop.

else clause is executed when the loop is finished. Now let suppose after printing all the numbers from 0 to 9 we have to print a message the loop is ended.

>>> for x in range(10):
...     print(x)
... else:
...     print("Loop Ended!!!")
... 
0
1
2
3
4
5
6
7
8
9
Loop Ended!!!
>>>

Conclusion

So the for loop in py (Python) is to print the statements, else clause in a repeated manner. Execute the block of code for a fixed number of lines. If you have any questions or feedback, feel free to leave a comment below!

If you want to learn more about MySQL and MongoDB then refer to the link. There is an online compiler from which you can learn Python.

One Reply to “for loop in py (Python) with 7 different examples.”

Give your valuable time