Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f665b67

Browse files
committed
finish enumerate()
1 parent ffca067 commit f665b67

File tree

3 files changed

+344
-0
lines changed

3 files changed

+344
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# enumerate(iterable, start=0)\n",
8+
"\n",
9+
"`iterable` must be a sequence, an iterator, or some other object which **supports iteration**. The `__next__()` method of the iterator returned by `enumerate()` **returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable**."
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"If you've ever wanted to keep track of both index and value when manipulating a list,\n",
17+
"you probably did something like this:"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 7,
23+
"metadata": {},
24+
"outputs": [
25+
{
26+
"name": "stdout",
27+
"output_type": "stream",
28+
"text": [
29+
"The 0th fibonacci number is 1.\n",
30+
"The 1th fibonacci number is 1.\n",
31+
"The 2th fibonacci number is 2.\n",
32+
"The 3th fibonacci number is 3.\n",
33+
"The 4th fibonacci number is 5.\n",
34+
"The 5th fibonacci number is 8.\n",
35+
"The 6th fibonacci number is 13.\n",
36+
"The 7th fibonacci number is 21.\n"
37+
]
38+
}
39+
],
40+
"source": [
41+
"fibonacci = [1, 1, 2, 3, 5, 8, 13, 21]\n",
42+
"for i in range(len(fibonacci)):\n",
43+
" print(\"The {}th fibonacci number is {}.\".format(i, fibonacci[i]))"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"But if we wanted to start counting from 1 or something, we would need to do:"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 8,
56+
"metadata": {},
57+
"outputs": [
58+
{
59+
"name": "stdout",
60+
"output_type": "stream",
61+
"text": [
62+
"The 1th fibonacci number is 1.\n",
63+
"The 2th fibonacci number is 1.\n",
64+
"The 3th fibonacci number is 2.\n",
65+
"The 4th fibonacci number is 3.\n",
66+
"The 5th fibonacci number is 5.\n",
67+
"The 6th fibonacci number is 8.\n",
68+
"The 7th fibonacci number is 13.\n",
69+
"The 8th fibonacci number is 21.\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"fibonacci = [1, 1, 2, 3, 5, 8, 13, 21]\n",
75+
"for i in range(len(fibonacci)):\n",
76+
" print(\"The {}th fibonacci number is {}.\".format(i+1, fibonacci[i]))"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"You can achieve the same results using the `enumerate()` function, but you instead get\n",
84+
"separate variables for your index and element, so it's a bit easier to read."
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 9,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"name": "stdout",
94+
"output_type": "stream",
95+
"text": [
96+
"The 0th fibonacci number is 1.\n",
97+
"The 1th fibonacci number is 1.\n",
98+
"The 2th fibonacci number is 2.\n",
99+
"The 3th fibonacci number is 3.\n",
100+
"The 4th fibonacci number is 5.\n",
101+
"The 5th fibonacci number is 8.\n",
102+
"The 6th fibonacci number is 13.\n",
103+
"The 7th fibonacci number is 21.\n"
104+
]
105+
}
106+
],
107+
"source": [
108+
"fibonacci = [1, 1, 2, 3, 5, 8, 13, 21]\n",
109+
"\n",
110+
"for index, number in enumerate(fibonacci):\n",
111+
" print(\"The {}th fibonacci number is {}.\".format(index, number))"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {},
117+
"source": [
118+
"In more difficult problems, referring to `i+1`for counting, but using index `i`, could get confusing since \n",
119+
"they don't match. In this scenario, the `enumerate()` function gives us a little more flexibility since\n",
120+
"we can specify any `start` that we want:"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 10,
126+
"metadata": {},
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"The 1th fibonacci number is 1.\n",
133+
"The 2th fibonacci number is 1.\n",
134+
"The 3th fibonacci number is 2.\n",
135+
"The 4th fibonacci number is 3.\n",
136+
"The 5th fibonacci number is 5.\n",
137+
"The 6th fibonacci number is 8.\n",
138+
"The 7th fibonacci number is 13.\n",
139+
"The 8th fibonacci number is 21.\n"
140+
]
141+
}
142+
],
143+
"source": [
144+
"fibonacci = [1, 1, 2, 3, 5, 8, 13, 21]\n",
145+
"\n",
146+
"for index, number in enumerate(fibonacci, start=1):\n",
147+
" print(\"The {}th fibonacci number is {}.\".format(index, number))"
148+
]
149+
}
150+
],
151+
"metadata": {
152+
"kernelspec": {
153+
"display_name": "Python 3",
154+
"language": "python",
155+
"name": "python3"
156+
},
157+
"language_info": {
158+
"codemirror_mode": {
159+
"name": "ipython",
160+
"version": 3
161+
},
162+
"file_extension": ".py",
163+
"mimetype": "text/x-python",
164+
"name": "python",
165+
"nbconvert_exporter": "python",
166+
"pygments_lexer": "ipython3",
167+
"version": "3.6.4"
168+
}
169+
},
170+
"nbformat": 4,
171+
"nbformat_minor": 2
172+
}

‎built_in_functions/enumerate.ipynb

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# enumerate(iterable, start=0)\n",
8+
"\n",
9+
"`iterable` must be a sequence, an iterator, or some other object which **supports iteration**. The `__next__()` method of the iterator returned by `enumerate()` **returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable**."
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"If you've ever wanted to keep track of both index and value when manipulating a list,\n",
17+
"you probably did something like this:"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 7,
23+
"metadata": {},
24+
"outputs": [
25+
{
26+
"name": "stdout",
27+
"output_type": "stream",
28+
"text": [
29+
"The 0th fibonacci number is 1.\n",
30+
"The 1th fibonacci number is 1.\n",
31+
"The 2th fibonacci number is 2.\n",
32+
"The 3th fibonacci number is 3.\n",
33+
"The 4th fibonacci number is 5.\n",
34+
"The 5th fibonacci number is 8.\n",
35+
"The 6th fibonacci number is 13.\n",
36+
"The 7th fibonacci number is 21.\n"
37+
]
38+
}
39+
],
40+
"source": [
41+
"fibonacci = [1, 1, 2, 3, 5, 8, 13, 21]\n",
42+
"for i in range(len(fibonacci)):\n",
43+
" print(\"The {}th fibonacci number is {}.\".format(i, fibonacci[i]))"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"But if we wanted to start counting from 1 or something, we would need to do:"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 8,
56+
"metadata": {},
57+
"outputs": [
58+
{
59+
"name": "stdout",
60+
"output_type": "stream",
61+
"text": [
62+
"The 1th fibonacci number is 1.\n",
63+
"The 2th fibonacci number is 1.\n",
64+
"The 3th fibonacci number is 2.\n",
65+
"The 4th fibonacci number is 3.\n",
66+
"The 5th fibonacci number is 5.\n",
67+
"The 6th fibonacci number is 8.\n",
68+
"The 7th fibonacci number is 13.\n",
69+
"The 8th fibonacci number is 21.\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"fibonacci = [1, 1, 2, 3, 5, 8, 13, 21]\n",
75+
"for i in range(len(fibonacci)):\n",
76+
" print(\"The {}th fibonacci number is {}.\".format(i+1, fibonacci[i]))"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"You can achieve the same results using the `enumerate()` function, but you instead get\n",
84+
"separate variables for your index and element, so it's a bit easier to read."
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 9,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"name": "stdout",
94+
"output_type": "stream",
95+
"text": [
96+
"The 0th fibonacci number is 1.\n",
97+
"The 1th fibonacci number is 1.\n",
98+
"The 2th fibonacci number is 2.\n",
99+
"The 3th fibonacci number is 3.\n",
100+
"The 4th fibonacci number is 5.\n",
101+
"The 5th fibonacci number is 8.\n",
102+
"The 6th fibonacci number is 13.\n",
103+
"The 7th fibonacci number is 21.\n"
104+
]
105+
}
106+
],
107+
"source": [
108+
"fibonacci = [1, 1, 2, 3, 5, 8, 13, 21]\n",
109+
"\n",
110+
"for index, number in enumerate(fibonacci):\n",
111+
" print(\"The {}th fibonacci number is {}.\".format(index, number))"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {},
117+
"source": [
118+
"In more difficult problems, referring to `i+1`for counting, but using index `i`, could get confusing since \n",
119+
"they don't match. In this scenario, the `enumerate()` function gives us a little more flexibility since\n",
120+
"we can specify any `start` that we want:"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 10,
126+
"metadata": {},
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"The 1th fibonacci number is 1.\n",
133+
"The 2th fibonacci number is 1.\n",
134+
"The 3th fibonacci number is 2.\n",
135+
"The 4th fibonacci number is 3.\n",
136+
"The 5th fibonacci number is 5.\n",
137+
"The 6th fibonacci number is 8.\n",
138+
"The 7th fibonacci number is 13.\n",
139+
"The 8th fibonacci number is 21.\n"
140+
]
141+
}
142+
],
143+
"source": [
144+
"fibonacci = [1, 1, 2, 3, 5, 8, 13, 21]\n",
145+
"\n",
146+
"for index, number in enumerate(fibonacci, start=1):\n",
147+
" print(\"The {}th fibonacci number is {}.\".format(index, number))"
148+
]
149+
}
150+
],
151+
"metadata": {
152+
"kernelspec": {
153+
"display_name": "Python 3",
154+
"language": "python",
155+
"name": "python3"
156+
},
157+
"language_info": {
158+
"codemirror_mode": {
159+
"name": "ipython",
160+
"version": 3
161+
},
162+
"file_extension": ".py",
163+
"mimetype": "text/x-python",
164+
"name": "python",
165+
"nbconvert_exporter": "python",
166+
"pygments_lexer": "ipython3",
167+
"version": "3.6.4"
168+
}
169+
},
170+
"nbformat": 4,
171+
"nbformat_minor": 2
172+
}

‎built_in_functions/enumerate.py

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /