additions to notes on Python loops
This commit is contained in:
parent
7f81cb18fb
commit
6fd534fa48
1 changed files with 34 additions and 0 deletions
|
@ -30,6 +30,40 @@ Done
|
||||||
|
|
||||||
## For
|
## For
|
||||||
|
|
||||||
|
There are three main types of `for` loop in Python and they all use the `for...in` clause:
|
||||||
|
|
||||||
|
1. Traditional `for` loop with range:
|
||||||
|
|
||||||
|
```py
|
||||||
|
for i in range(0, 5):
|
||||||
|
print(i)
|
||||||
|
```
|
||||||
|
|
||||||
|
This is equivalent to the following in JavaScript:
|
||||||
|
|
||||||
|
```js
|
||||||
|
for (let i = 0; i <= 5: i++) {
|
||||||
|
console.log(i)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
2. `for` loop with iterable:
|
||||||
|
|
||||||
|
```py
|
||||||
|
fruits = ["apple", "banana", "cherry"]
|
||||||
|
for (fruit in fruits):
|
||||||
|
print(fruit)
|
||||||
|
```
|
||||||
|
|
||||||
|
3. `for` loop with `enumerate()` function:
|
||||||
|
Iterate over an iterable whilst also keeping track of the index of the current element:
|
||||||
|
```py
|
||||||
|
for index, fruit in enumerate(fruits):
|
||||||
|
print(f"Index {index}: {fruit}")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Further examples of `for in range()` loops
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Loop over a set of values in a range
|
# Loop over a set of values in a range
|
||||||
print('Print out values in a range')
|
print('Print out values in a range')
|
||||||
|
|
Loading…
Add table
Reference in a new issue