1/5
## Loops: For-Each We've looked at `while` loops and `for` loops. Let's look at the `for-each` loop. Let's say we have a list: ```python my_list = [0, 1, 2] ``` We could loop through all the elements in this list by saying `for` each element `in` my list: ```python for element in my_list: print("My number is " + str(element)) print("Done!") ``` We can see this is going to loop through each element in the list. So if we hit play we see `My number is 0`, `My number is 1`, and `My number is 2`. Let's add some more numbers to our list: ```python my_list = [0, 1, 2, 3, 5, 7] ``` And then hit play. Now, we see our original numbers 0, 1, 2, and then our new numbers 3, 5, and 7. Now, let's look at how to use a loop for repeating a specific task a large number of times. Let's say we wanted to print "Hi" 1,000 times. It would be tedious to write: ```python print("Hi") print("Hi") print("Hi") ``` 1,000 times. Is there a better way? There sure is! In Python, we can do repetitive tasks with something called "loops". We could do something as simple as `for` number `in` range 1,000 print: ```python for number in range(1000): print("My number is " + str(number)) print("Done!") ``` I recommend you don't do this, because you're going to get a massive cell that's outputted here. You know what, screw it. Let's try it. `For` number `in` range 1,000. Let's go ahead and hit play. If we scroll down we see just a ton of numbers. Some of this was even truncated and we'd have to scroll for a long time. But you can see it's printing out every single number between 0 and 999. So let's leave this as three so it's a lot more digestible.
A comprehensive guide to for-each loops in Python - This lesson introduces for-each loops and how they can be used to iterate through the elements in a list. The lesson uses practical examples to explain how the loop works and provides a basic overview of the for-each loop syntax.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on January 14, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on January 14, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar