1
+ {
2
+ "cells" : [
3
+ {
4
+ "cell_type" : " markdown" ,
5
+ "metadata" : {
6
+ "id" : " CE6ac5aIsb5K"
7
+ },
8
+ "source" : [
9
+ " # How to Swap Variables Source Code\n " ,
10
+ " by Atharva Aher for the [How to Swap Variables](https://therenegadecoder.com/code/how-to-swap-variables-in-python/) article"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type" : " markdown" ,
15
+ "metadata" : {
16
+ "id" : " yitmP7ONsq6K"
17
+ },
18
+ "source" : [
19
+ " ## Solutions"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type" : " markdown" ,
24
+ "metadata" : {
25
+ "id" : " DdrKy6u5sxw6"
26
+ },
27
+ "source" : [
28
+ " Here, we'll look at all the solutions from the original article."
29
+ ]
30
+ },
31
+ {
32
+ "cell_type" : " code" ,
33
+ "execution_count" : 7 ,
34
+ "metadata" : {
35
+ "colab" : {
36
+ "base_uri" : " https://localhost:8080/"
37
+ },
38
+ "id" : " JNcqYrx9sWS5" ,
39
+ "outputId" : " bd09165c-5b43-4ce7-a2b9-8bf4e8145b0f"
40
+ },
41
+ "outputs" : [
42
+ {
43
+ "output_type" : " stream" ,
44
+ "name" : " stdout" ,
45
+ "text" : [
46
+ " palette_1: barta\n " ,
47
+ " palette_2: foie\n "
48
+ ]
49
+ }
50
+ ],
51
+ "source" : [
52
+ " # Swap Elements with a Temporary Variable\n " ,
53
+ " palette_1 = \" foie\"\n " ,
54
+ " palette_2 = \" barta\"\n " ,
55
+ " \n " ,
56
+ " temp = palette_1\n " ,
57
+ " palette_1 = palette_2\n " ,
58
+ " palette_2 =temp\n " ,
59
+ " \n " ,
60
+ " print('palette_1:', palette_1)\n " ,
61
+ " print('palette_2:', palette_2)"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type" : " code" ,
66
+ "execution_count" : 8 ,
67
+ "metadata" : {
68
+ "colab" : {
69
+ "base_uri" : " https://localhost:8080/"
70
+ },
71
+ "id" : " mvS-Z2UYtE-T" ,
72
+ "outputId" : " 7ba8b187-69f5-4fd2-8723-589a2c54f197"
73
+ },
74
+ "outputs" : [
75
+ {
76
+ "output_type" : " stream" ,
77
+ "name" : " stdout" ,
78
+ "text" : [
79
+ " palette_1: barta\n " ,
80
+ " palette_2: foie\n "
81
+ ]
82
+ }
83
+ ],
84
+ "source" : [
85
+ " # Swap Elements with Iterable Unpacking\n " ,
86
+ " palette_1 = \" foie\"\n " ,
87
+ " palette_2 = \" barta\"\n " ,
88
+ " \n " ,
89
+ " palette_1, palette_2 = palette_2, palette_1\n " ,
90
+ " \n " ,
91
+ " print('palette_1:', palette_1)\n " ,
92
+ " print('palette_2:', palette_2)"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type" : " code" ,
97
+ "execution_count" : 9 ,
98
+ "metadata" : {
99
+ "colab" : {
100
+ "base_uri" : " https://localhost:8080/"
101
+ },
102
+ "id" : " 1B57ydaftMrB" ,
103
+ "outputId" : " c39a9e31-ffd9-43c6-8cdf-df45234da7ff"
104
+ },
105
+ "outputs" : [
106
+ {
107
+ "output_type" : " stream" ,
108
+ "name" : " stdout" ,
109
+ "text" : [
110
+ " palette_1: barta\n " ,
111
+ " palette_2: foie\n "
112
+ ]
113
+ }
114
+ ],
115
+ "source" : [
116
+ " palette_1 = \" foie\"\n " ,
117
+ " palette_2 = \" barta\"\n " ,
118
+ " \n " ,
119
+ " temp = palette_2, palette_1 # creates a tuple from the two palettes\n " ,
120
+ " palette_1, palette_2 = temp # unpacks the tuple into the two palettes\n " ,
121
+ " \n " ,
122
+ " print('palette_1:', palette_1)\n " ,
123
+ " print('palette_2:', palette_2)"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type" : " markdown" ,
128
+ "metadata" : {
129
+ "id" : " ECfsN4ZTtSES"
130
+ },
131
+ "source" : [
132
+ " ## Performance"
133
+ ]
134
+ },
135
+ {
136
+ "cell_type" : " markdown" ,
137
+ "metadata" : {
138
+ "id" : " NtKAq18ptWwK"
139
+ },
140
+ "source" : [
141
+ " Here, we'll look at the performance code from the original article."
142
+ ]
143
+ },
144
+ {
145
+ "cell_type" : " code" ,
146
+ "execution_count" : 10 ,
147
+ "metadata" : {
148
+ "id" : " IGEB5JsctTzL"
149
+ },
150
+ "outputs" : [],
151
+ "source" : [
152
+ " # Import performance testing library\n " ,
153
+ " import timeit\n " ,
154
+ " \n " ,
155
+ " # Generate test strings\n " ,
156
+ " setup = \"\"\"\n " ,
157
+ " palette_1 = \" foie\"\n " ,
158
+ " palette_2 = \" barta\"\n " ,
159
+ " \"\"\"\n " ,
160
+ " temp = \"\"\"\n " ,
161
+ " _ = palette_1\n " ,
162
+ " palette_1 = palette_2\n " ,
163
+ " palette_2 = _\n " ,
164
+ " \"\"\"\n " ,
165
+ " unpack = \"\"\"\n " ,
166
+ " palette_1, palette_2 = palette_2, palette_1\n " ,
167
+ " \"\"\" "
168
+ ]
169
+ },
170
+ {
171
+ "cell_type" : " code" ,
172
+ "execution_count" : 11 ,
173
+ "metadata" : {
174
+ "colab" : {
175
+ "base_uri" : " https://localhost:8080/"
176
+ },
177
+ "id" : " c5F_PEXDttQz" ,
178
+ "outputId" : " e1d5ecf7-3d73-4d0c-9079-04eb4b97b979"
179
+ },
180
+ "outputs" : [
181
+ {
182
+ "output_type" : " execute_result" ,
183
+ "data" : {
184
+ "text/plain" : [
185
+ " 0.018495057999984965"
186
+ ]
187
+ },
188
+ "metadata" : {},
189
+ "execution_count" : 11
190
+ }
191
+ ],
192
+ "source" : [
193
+ " min(timeit.repeat(setup=setup, stmt=temp))"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type" : " code" ,
198
+ "execution_count" : 12 ,
199
+ "metadata" : {
200
+ "colab" : {
201
+ "base_uri" : " https://localhost:8080/"
202
+ },
203
+ "id" : " zhNyN7w3twLb" ,
204
+ "outputId" : " 1a63233c-e2bf-4d90-8edc-d98d2917f87f"
205
+ },
206
+ "outputs" : [
207
+ {
208
+ "output_type" : " execute_result" ,
209
+ "data" : {
210
+ "text/plain" : [
211
+ " 0.016565159000037966"
212
+ ]
213
+ },
214
+ "metadata" : {},
215
+ "execution_count" : 12
216
+ }
217
+ ],
218
+ "source" : [
219
+ " min(timeit.repeat(setup=setup, stmt=unpack))"
220
+ ]
221
+ }
222
+ ],
223
+ "metadata" : {
224
+ "colab" : {
225
+ "name" : " how-to-write-a-loop.ipynb" ,
226
+ "provenance" : []
227
+ },
228
+ "kernelspec" : {
229
+ "display_name" : " Python 3" ,
230
+ "language" : " python" ,
231
+ "name" : " python3"
232
+ },
233
+ "language_info" : {
234
+ "codemirror_mode" : {
235
+ "name" : " ipython" ,
236
+ "version" : 3
237
+ },
238
+ "file_extension" : " .py" ,
239
+ "mimetype" : " text/x-python" ,
240
+ "name" : " python" ,
241
+ "nbconvert_exporter" : " python" ,
242
+ "pygments_lexer" : " ipython3" ,
243
+ "version" : " 3.7.6"
244
+ }
245
+ },
246
+ "nbformat" : 4 ,
247
+ "nbformat_minor" : 0
248
+ }
0 commit comments