21 Python for Loop Exercises and Examples
In Python programming, we use for loops to repeat some code a certain number of times. It allows us to execute a statement or a group of statements multiple times by reducing the burden of writing several lines of code.
To get a clear idea about how a for loop works, I have provided 21 examples of using for loop in Python. You can go through these examples and understand the working of for loops in different scenarios.
Let’s dive right in.
1. Python for loop to iterate through the letters in a word
2. Python for loop using the range() function
3. Python for loop to iterate through a list
4. Python for loop to iterate through a dictionary
5. Python for loop using the zip() function for parallel iteration
6. Using else statement inside a for loop in Python
7. Nested for loops in Python (one loop inside another loop)
8. Using break statement inside a for loop in Python
9. Using continue statement inside a for loop in Python
10. Python for loop to count the number of elements in a list
11. Python for loop to find the sum of all numbers in a list
12. Python for loop to find the multiples of 5 in a list
13. Python for loop to print a triangle of stars
14. Python for loop to copy elements from one list to another
15. Python for loop to find the maximum element in a list
16. Python for loop to find the minimum element in a list
17. Python for loop to sort the numbers in a list in ascending order
18. Python for loop to sort the numbers in a list in descending order
19. Python for loop to print the multiples of 3 using range() function
20. Python for loop to print the multiples of 5 using range() function
21. Python for loop to print the numbers in reverse order using range() function
I hope this article was helpful. Check out my post on 18 Python while Loop Examples .
I'm the face behind Pythonista Planet. I learned my first programming language back in 2015. Ever since then, I've been learning programming and immersing myself in technology. On this site, I share everything that I've learned about computer programming.
3 thoughts on “ 21 Python for Loop Exercises and Examples ”
we need some more difficult problems. please give some different questions.
These are some examples of implementing for loop in Python. If you want more coding problems, check out this post: https://pythonistaplanet.com/python-programming-exercises-and-solutions/
myfamily = { “child1” : { “name” : “Emil”, “year” : 2004 }, “child2” : { “name” : “Tobias”, “year” : 2007 }, “child3” : { “name” : “Linus”, “year” : 2011 } }
Find the child the year greater than 2007. Output = child3
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Save my name and email in this browser for the next time I comment.
Recent Posts
Introduction to Modular Programming with Flask
Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules. In this tutorial, let's understand what modular...
Introduction to ORM with Flask-SQLAlchemy
While Flask provides the essentials to get a web application up and running, it doesn't force anything upon the developer. This means that many features aren't included in the core framework....