Edit Python blog entry to fix typo and add another map example
This commit is contained in:
parent
cf7ba688a1
commit
7eb6a41602
@ -90,7 +90,7 @@ Output:
|
|||||||
|
|
||||||
## 2. Nicer iteration with zip() and enumerate()
|
## 2. Nicer iteration with zip() and enumerate()
|
||||||
|
|
||||||
Python's for loop is commonly known in other programming languages as a for-each loop. This is great if you just want each item in an iterable, but sometimes you want the index too! Instead of having to resort to `range(len(array))`, instead you can use `enumerate()` and tuple expansionto easily get both the index of the element and the element itself:
|
Python's for loop is commonly known in other programming languages as a for-each loop. This is great if you just want each item in an iterable, but sometimes you want the index too! Instead of having to resort to `range(len(array))`, instead you can use `enumerate()` and tuple expansion to easily get both the index of the element and the element itself:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
array = ["a", "b", "c", "d", "e"]
|
array = ["a", "b", "c", "d", "e"]
|
||||||
@ -222,6 +222,12 @@ Output:
|
|||||||
[1, 2, 3, 4, 5]
|
[1, 2, 3, 4, 5]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
It's most useful in assigning variables easily when you know the format the input will be in.
|
||||||
|
|
||||||
|
```python
|
||||||
|
a, b = (map(int, input().split()))
|
||||||
|
```
|
||||||
|
|
||||||
## 5. List generators
|
## 5. List generators
|
||||||
|
|
||||||
You can generate a new list using inline `for`.
|
You can generate a new list using inline `for`.
|
||||||
|
Loading…
Reference in New Issue
Block a user