1/5
## Loops: For-Range We've learned about while loops, but there are other ways to loop, too. Let's look at a different kind of loop called a for-range loop. We previously used the code: ```python my_number = 0 while my_number < 3: print("My number is " + str(my_number)) my_number = my_number + 1 print("Done!") ``` We can rewrite this using a for-range loop, which is more concise than the while loop: ```python for number in range(3): print("My number is " + str(number)) print("Done!") ``` The `for` keyword lets Python know that we are going to use a for-range loop. The loop will iterate over the numbers in `range(3)` � that is, the numbers 0, 1, and 2. For each number, it will execute the code in the loop block (which prints "My number is" plus the number). Let's run this code and see what happens. We can see the output: ``` My number is 0 My number is 1 My number is 2 Done! ``` So, `range(3)` says to loop from 0 to 2. We can see the result in the output: the code prints out "My number is" plus the number 0, then 1, then 2. This is a much more concise way to accomplish the same result as the while loop we used before. In the for-range loop, we don't need to worry about incrementing a counter variable because Python does that for us automatically.
A concise introduction to Python for-range loops - The lesson shows how for-range loops are a more concise way to repeat code compared to while loops. This lesson demonstrates using range, a built-in Python function, to create a series of numbers for iteration.
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