Skip to content

Commit 7919325

Browse files
authored
Add files via upload
Updated Python Basics Tuple tutorial for post video.
1 parent fc44066 commit 7919325

1 file changed

Lines changed: 87 additions & 95 deletions

File tree

Python_Basics/Intro/PythonBasicsTuples.ipynb

Lines changed: 87 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets."
14+
"Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed (immutable) unlike lists (mutable). <br> Tuples use parentheses, whereas lists use square brackets."
1515
]
1616
},
1717
{
@@ -23,37 +23,55 @@
2323
},
2424
{
2525
"cell_type": "code",
26-
"execution_count": 5,
27-
"metadata": {
28-
"collapsed": true
29-
},
26+
"execution_count": 17,
27+
"metadata": {},
3028
"outputs": [],
3129
"source": [
3230
"# empty tuple written as two parentheses containing nothing\n",
33-
"tup1 = (); "
31+
"tup1 = (); \n",
32+
"\n",
33+
"# empty list\n",
34+
"list1 = []"
3435
]
3536
},
3637
{
3738
"cell_type": "code",
38-
"execution_count": 6,
39-
"metadata": {
40-
"collapsed": true
41-
},
42-
"outputs": [],
39+
"execution_count": 84,
40+
"metadata": {},
41+
"outputs": [
42+
{
43+
"data": {
44+
"text/plain": [
45+
"tuple"
46+
]
47+
},
48+
"execution_count": 84,
49+
"metadata": {},
50+
"output_type": "execute_result"
51+
}
52+
],
4353
"source": [
4454
"# To write a tuple containing a single value you have to include a comma, even though there is only one value\n",
45-
"tup1 = (50,);"
55+
"tup1 = 50, ;\n",
56+
"type(tup1)"
4657
]
4758
},
4859
{
4960
"cell_type": "code",
50-
"execution_count": 22,
51-
"metadata": {
52-
"collapsed": true
53-
},
54-
"outputs": [],
61+
"execution_count": 85,
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"<type 'tuple'>\n"
69+
]
70+
}
71+
],
5572
"source": [
5673
"tup1 = 'Please', 'Subscribe';\n",
74+
"print(type(tup1))\n",
5775
"tup2 = ('pretty please', );\n",
5876
"tup3 = \"a\", \"b\", \"c\", \"d\";"
5977
]
@@ -84,18 +102,16 @@
84102
},
85103
{
86104
"cell_type": "code",
87-
"execution_count": 24,
88-
"metadata": {
89-
"collapsed": false
90-
},
105+
"execution_count": 75,
106+
"metadata": {},
91107
"outputs": [
92108
{
93109
"data": {
94110
"text/plain": [
95111
"('a', 'b', 'c', 'd')"
96112
]
97113
},
98-
"execution_count": 24,
114+
"execution_count": 75,
99115
"metadata": {},
100116
"output_type": "execute_result"
101117
}
@@ -107,9 +123,7 @@
107123
{
108124
"cell_type": "code",
109125
"execution_count": 25,
110-
"metadata": {
111-
"collapsed": false
112-
},
126+
"metadata": {},
113127
"outputs": [
114128
{
115129
"data": {
@@ -130,18 +144,16 @@
130144
},
131145
{
132146
"cell_type": "code",
133-
"execution_count": 26,
134-
"metadata": {
135-
"collapsed": false
136-
},
147+
"execution_count": 12,
148+
"metadata": {},
137149
"outputs": [
138150
{
139151
"data": {
140152
"text/plain": [
141153
"('a', 'b', 'c')"
142154
]
143155
},
144-
"execution_count": 26,
156+
"execution_count": 12,
145157
"metadata": {},
146158
"output_type": "execute_result"
147159
}
@@ -153,18 +165,16 @@
153165
},
154166
{
155167
"cell_type": "code",
156-
"execution_count": 27,
157-
"metadata": {
158-
"collapsed": false
159-
},
168+
"execution_count": 13,
169+
"metadata": {},
160170
"outputs": [
161171
{
162172
"data": {
163173
"text/plain": [
164174
"('b', 'c', 'd')"
165175
]
166176
},
167-
"execution_count": 27,
177+
"execution_count": 13,
168178
"metadata": {},
169179
"output_type": "execute_result"
170180
}
@@ -177,9 +187,7 @@
177187
{
178188
"cell_type": "code",
179189
"execution_count": 35,
180-
"metadata": {
181-
"collapsed": false
182-
},
190+
"metadata": {},
183191
"outputs": [
184192
{
185193
"data": {
@@ -213,18 +221,16 @@
213221
},
214222
{
215223
"cell_type": "code",
216-
"execution_count": 36,
217-
"metadata": {
218-
"collapsed": false
219-
},
224+
"execution_count": 86,
225+
"metadata": {},
220226
"outputs": [
221227
{
222228
"data": {
223229
"text/plain": [
224230
"('Please', 'Subscribe')"
225231
]
226232
},
227-
"execution_count": 36,
233+
"execution_count": 86,
228234
"metadata": {},
229235
"output_type": "execute_result"
230236
}
@@ -235,18 +241,16 @@
235241
},
236242
{
237243
"cell_type": "code",
238-
"execution_count": 37,
239-
"metadata": {
240-
"collapsed": false
241-
},
244+
"execution_count": 87,
245+
"metadata": {},
242246
"outputs": [
243247
{
244248
"data": {
245249
"text/plain": [
246250
"('pretty please',)"
247251
]
248252
},
249-
"execution_count": 37,
253+
"execution_count": 87,
250254
"metadata": {},
251255
"output_type": "execute_result"
252256
}
@@ -257,10 +261,8 @@
257261
},
258262
{
259263
"cell_type": "code",
260-
"execution_count": 38,
261-
"metadata": {
262-
"collapsed": false
263-
},
264+
"execution_count": 88,
265+
"metadata": {},
264266
"outputs": [
265267
{
266268
"name": "stdout",
@@ -296,28 +298,24 @@
296298
"cell_type": "markdown",
297299
"metadata": {},
298300
"source": [
299-
"tuples are useful for sequence unpacking <br>\n",
300-
"if you some data you know you dont want to change, use tuples <br>\n",
301-
"generated faster and iterated faster than a python list"
301+
"tuples are useful for sequence unpacking <br>"
302302
]
303303
},
304304
{
305305
"cell_type": "code",
306-
"execution_count": 41,
307-
"metadata": {
308-
"collapsed": false
309-
},
306+
"execution_count": 92,
307+
"metadata": {},
310308
"outputs": [
311309
{
312310
"name": "stdout",
313311
"output_type": "stream",
314312
"text": [
315-
"Value of x , y : 7 10\n"
313+
"('Value of x , y : ', 7, 10)\n"
316314
]
317315
}
318316
],
319317
"source": [
320-
"x, y = 7, 10;\n",
318+
"x, y = (7, 10);\n",
321319
"print(\"Value of x , y : \", x, y)"
322320
]
323321
},
@@ -337,25 +335,23 @@
337335
},
338336
{
339337
"cell_type": "code",
340-
"execution_count": 43,
341-
"metadata": {
342-
"collapsed": false
343-
},
338+
"execution_count": 47,
339+
"metadata": {},
344340
"outputs": [
345341
{
346342
"name": "stdout",
347343
"output_type": "stream",
348344
"text": [
349-
"0 steve\n",
350-
"1 rachel\n",
351-
"2 michael\n",
352-
"3 adam\n",
353-
"4 monica\n"
345+
"(0, 'steve')\n",
346+
"(1, 'rachel')\n",
347+
"(2, 'michael')\n",
348+
"(3, 'adam')\n",
349+
"(4, 'monica')\n"
354350
]
355351
}
356352
],
357353
"source": [
358-
"friends = ['steve', 'rachel', 'michael', 'adam', 'monica']\n",
354+
"friends = ('steve', 'rachel', 'michael', 'adam', 'monica')\n",
359355
"for index, friend in enumerate(friends):\n",
360356
" print(index,friend)"
361357
]
@@ -371,15 +367,13 @@
371367
"cell_type": "markdown",
372368
"metadata": {},
373369
"source": [
374-
"Fibonacci sequence is an integer sequence characterized by the fact that every number after the first two is the sum of the two preceding one. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1 (<b>which is how I like to code it</b>), or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two."
370+
"Fibonacci sequence is an integer sequence characterized by the fact that every number after the first two is the sum of the two preceding ones. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1 (<b>which is how I like to code it</b>), or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two."
375371
]
376372
},
377373
{
378374
"cell_type": "code",
379375
"execution_count": 2,
380-
"metadata": {
381-
"collapsed": false
382-
},
376+
"metadata": {},
383377
"outputs": [
384378
{
385379
"name": "stdout",
@@ -402,33 +396,31 @@
402396
},
403397
{
404398
"cell_type": "code",
405-
"execution_count": 3,
406-
"metadata": {
407-
"collapsed": false
408-
},
399+
"execution_count": 24,
400+
"metadata": {},
409401
"outputs": [
410402
{
411403
"name": "stdout",
412404
"output_type": "stream",
413405
"text": [
414-
"1\n",
415-
"1\n",
416-
"2\n",
417-
"3\n",
418-
"5\n",
419-
"8\n",
420-
"13\n",
421-
"21\n",
422-
"34\n",
423-
"55\n"
406+
"('Fib(a): ', 1, 'b is: ', 1)\n",
407+
"('Fib(a): ', 1, 'b is: ', 2)\n",
408+
"('Fib(a): ', 2, 'b is: ', 3)\n",
409+
"('Fib(a): ', 3, 'b is: ', 5)\n",
410+
"('Fib(a): ', 5, 'b is: ', 8)\n",
411+
"('Fib(a): ', 8, 'b is: ', 13)\n",
412+
"('Fib(a): ', 13, 'b is: ', 21)\n",
413+
"('Fib(a): ', 21, 'b is: ', 34)\n",
414+
"('Fib(a): ', 34, 'b is: ', 55)\n",
415+
"('Fib(a): ', 55, 'b is: ', 89)\n"
424416
]
425417
}
426418
],
427419
"source": [
428420
"# Note, there are better ways to code this which I will go over in later videos\n",
429421
"a,b = 1,1\n",
430422
"for i in range(10):\n",
431-
" print(a)\n",
423+
" print(\"Fib(a): \", a, \"b is: \", b)\n",
432424
" a,b = b,a+b "
433425
]
434426
},
@@ -450,21 +442,21 @@
450442
"metadata": {
451443
"anaconda-cloud": {},
452444
"kernelspec": {
453-
"display_name": "Python [conda env:py36]",
445+
"display_name": "Python 2",
454446
"language": "python",
455-
"name": "conda-env-py36-py"
447+
"name": "python2"
456448
},
457449
"language_info": {
458450
"codemirror_mode": {
459451
"name": "ipython",
460-
"version": 3
452+
"version": 2
461453
},
462454
"file_extension": ".py",
463455
"mimetype": "text/x-python",
464456
"name": "python",
465457
"nbconvert_exporter": "python",
466-
"pygments_lexer": "ipython3",
467-
"version": "3.6.1"
458+
"pygments_lexer": "ipython2",
459+
"version": "2.7.13"
468460
}
469461
},
470462
"nbformat": 4,

0 commit comments

Comments
 (0)