Skip to content

Commit 486610f

Browse files
committed
Create a List of Tuples with Numbers and Their Cubes
1 parent 3ecdbcf commit 486610f

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"vscode": {
7+
"languageId": "plaintext"
8+
}
9+
},
10+
"source": [
11+
"## Using List Comprehension"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"[(1, 1), (2, 8), (3, 27), (4, 64), (5, 125)]\n"
24+
]
25+
}
26+
],
27+
"source": [
28+
"a: list[int] = [1,2,3,4,5]\n",
29+
"res: list[tuple[int, int]] = [(n,n**3) for n in a]\n",
30+
"print(res)"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"## using map()"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 3,
43+
"metadata": {},
44+
"outputs": [
45+
{
46+
"name": "stdout",
47+
"output_type": "stream",
48+
"text": [
49+
"[(1, 1), (2, 8), (3, 27), (4, 64), (5, 125)]\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"a: list[int] = [1,2,3,4,5]\n",
55+
"res: list[tuple[int, int]] = list(map(lambda n: (n,n**3), a))\n",
56+
"print(res)"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"## Using a for Loop with append()"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 8,
69+
"metadata": {},
70+
"outputs": [
71+
{
72+
"name": "stdout",
73+
"output_type": "stream",
74+
"text": [
75+
"[(1, 1), (2, 8), (3, 27), (4, 64), (5, 125)]\n"
76+
]
77+
}
78+
],
79+
"source": [
80+
"a: list[int] = [1,2,3,4,5]\n",
81+
"res: list[tuple[int,int]] = []\n",
82+
"for n in a:\n",
83+
" res.append((n,n**3))\n",
84+
"print(res)"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"metadata": {},
90+
"source": [
91+
"## Using a Generator and list()"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 9,
97+
"metadata": {},
98+
"outputs": [
99+
{
100+
"name": "stdout",
101+
"output_type": "stream",
102+
"text": [
103+
"[(1, 1), (2, 8), (3, 27), (4, 64), (5, 125)]\n"
104+
]
105+
}
106+
],
107+
"source": [
108+
"a: list[int] = [1,2,3,4,5]\n",
109+
"res: list[tuple[int, int]] = list((n, n**3) for n in a)\n",
110+
"print(res)"
111+
]
112+
}
113+
],
114+
"metadata": {
115+
"kernelspec": {
116+
"display_name": "Python 3",
117+
"language": "python",
118+
"name": "python3"
119+
},
120+
"language_info": {
121+
"codemirror_mode": {
122+
"name": "ipython",
123+
"version": 3
124+
},
125+
"file_extension": ".py",
126+
"mimetype": "text/x-python",
127+
"name": "python",
128+
"nbconvert_exporter": "python",
129+
"pygments_lexer": "ipython3",
130+
"version": "3.12.9"
131+
}
132+
},
133+
"nbformat": 4,
134+
"nbformat_minor": 2
135+
}

0 commit comments

Comments
 (0)