1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
{
"metadata": {
"name": "Chapter 5"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "<h1>Chapter 5: Arrays ans Strings<h1>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.1, Page Number: 82<h3>\n "
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Loads sample with the numbers 0 to 9'''\n\n#Variable declaration\nsample = range(10) #Declares an array [0,1,2,3,4,5,6,7,8,9]\n\n#Displays the array\nfor t in range(10):\n print sample[t],\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "0 1 2 3 4 5 6 7 8 9\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.2, Page Number: 83<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Maximum and Minimum value from a list'''\n\nimport random\n\n#Variable declaration\nlist = [] #List of integers\n\nfor i in range(10):\n list.append(random.randint(0,1000)) #randomely assigning integers\n \n#Finding the minimum value\nmin_value = list[0]\n\nfor i in range(10):\n if min_value>list[i]:\n min_value=list[i]\nprint \"minimum value: \",min_value #Result:Minimum value\n\n#Finding maximum value\nmax_value = list[0]\n\nfor i in range(10):\n if max_value<list[i]:\n max_value=list[i]\nprint \"maximum value: \",max_value #Result:Maximum value\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "minimum value: 192\nmaximum value: 684\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.3, Page Number: 85<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Bubble Sort'''\n\nimport random\n\n#Variable declaration\nnums = []\nsize = 10\n\n#Initializing list with random numbers\nfor t in range(size):\n nums.append(random.randint(0,1000))\n\n#Displays original array\nprint \"Original array is: \"\nprint nums\n\n#Bubble Sort\nfor a in range(size):\n for b in xrange(size-1,a,-1):\n if nums[b-1]>nums[b]:\n t=nums[b-1]\n nums[b-1]=nums[b]\n nums[b] = t\n\n#Display sorted array\nprint \"Sorted array is: \"\nprint nums\n\n \n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Original array is: \n[388, 661, 218, 595, 167, 46, 704, 140, 559, 428]\nSorted array is: \n[46, 140, 167, 218, 388, 428, 559, 595, 661, 704]\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.4, Page Number: 87<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Example for a string; Implementing cin and cout'''\n\n#Variable decleration\nstr = \"Hello\"\n\n#Result\nprint \"Here is your string:\",\nprint str\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Here is your string: Hello\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.5, Page Number: 88<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Example for a string; Implementing gets for input'''\n\nprint \"Enter a string: \"\n\n#user input\nstr = \"Hello\"\n\n#Result\nprint \"Here is your string:\",\nprint str\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Enter a string: \nHere is your string: Hello\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.6, Page Number: 89<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Implementing strcpy'''\n\n#Variable decleration\ns=str\nstr = s #copying s into str\n\n#Result\nprint str\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Hello\n"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.7, Page Number: 89<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Example for concatenation'''\n\n#Variable decleration\ns1 = \"Hello\"\ns2 = \" There\"\n\n#Concatenation\ns1+=s2\n\n#Result\nprint s1\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Hello There\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.8, Page Number: 90<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n#Example for string compare: Password verification\n\n#Function for password verification\ndef password():\n print \"Enter password: \" #User input for password\n s= \"pass\"\n if s==\"password\":\n return True\n else:\n print \"Invalid password.\"\n return False\n\n#Result\nif password() :\n print \"Logged On.\"\nelse:\n print \"Access denied\"\n \n \n \n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Enter password: \nInvalid password.\nAccess denied\n"
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.9, Page Number: 91<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Takes user input till strings match'''\n\nwhile True:\n s=raw_input(\"Enter a string: \") #User input of string;\n if s==\"quit\": #Sting comparison\n break\n \n \n\n \n \n",
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter a string: ddc\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter a string: hello\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter a string: quit\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.10, Page Number: 91<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''String length; Implementing strlen'''\n\n#Variable declaration\nstr = \"Hello\"\n\n#Result\nprint \"Length is: \",len(str)\n \n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Length is: 5\n"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.11, Page Number: 92<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Print a string backwards.'''\n\n#Taking a user input string\nstr = \"Hello World\"\n\n#Reversing a String\nrev = str[::-1]\n\n#Result\nprint rev\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "dlroW olleH\n"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.12, Page Number: 92<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\n'''Illustrating string functions'''\n\n#Variable declaration\ns1 = \"Hello\"\ns2 = \"there\"\n\n#Printing lengths\nprint \"lengths: \",len(s1),' ',len(s2)\n\n#Comparing\nif(s1==s2):\n print \"The strings are equal\"\nelse:\n print \"not equal\"\n\n#Concatenation\ns1+=s2\nprint s1\n\n\n#Copying\ns1=s2\n\n#Result\nprint s1,\"and\",s2,\" are now the same\"\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "lengths: 5 5\nnot equal\nHellothere\nthere and there are now the same\n"
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.13, Page Number: 93<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Convert a string to upper case'''\n\nimport string\n\n#Variable Initialization\nstr= \"this is a test\"\n\nstr=string.upper(str)\n\n#Result\nprint str\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "THIS IS A TEST\n"
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.14, Page Number: 94<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Load a two-dimensional array with values 1-12'''\n\n#Variable Initialization\narr=[] #The 2-D list\nnew=[] #The nested list\n\n#Initializing the 2-D array\nfor i in range(3):\n new=[]\n for j in range(4):\n new.append(i*4+j+1)\n arr.append(new)\n\n#Result\nfor i in range(3):\n for j in range(4):\n print arr[i][j],\n print",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1 2 3 4\n5 6 7 8\n9 10 11 12\n"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.15, Page Number: 98<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Looks up a no. in an array n prints the corresponding square'''\n\n#Variabe Initialzation\nsqrs= [[1,1],[2,4],[3,9],[4,16],[5,25],\n [6,36],[7,49],[8,64],[9,81],[10,100]] #Array storing the squares\ni=6 #User input of number whose square is to be looked up\n\n#Search for the number\nfor j in range(10):\n if sqrs[j][0]==i:\n break\n \n#Result\nprint \"The square of \",i,\" is \", sqrs[j][1]",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "The square of 6 is 36\n"
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.16, Page Number: 99<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Demonstating how local strings are initialized each time they are called'''\n\ndef f1():\n s=\"this is a test\" #initial s\n print s\n s=\"CHANGED\" #s is now changed \n print s\n\n#Calling the function twice\nf1()\nf1()",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "this is a test\nCHANGED\nthis is a test\nCHANGED\n"
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.17, Page Number: 101<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Enter and display strings'''\n\n#Variable decleration\ntext=[]\nstr=['eat','play','work'] #user input of strings\np=len(str)\n\nfor t in range(p):\n print t,\":\"\n text.append(str[t]) #Here, user input taken from the list\n \n#Result; redisplay the strings\nfor i in range(p):\n print text[i]\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "0 :\n1 :\n2 :\neat\nplay\nwork\n"
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 5.18, Page Number: 103<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''A simple employee database program'''\nimport random \n\n#Variable decleration\nname=[] #this list holds employee names\nwage=[] #their phone numbers\nphone=[] #hours worked per week\nhours=[] #wage\nnum=0 #User choice\n\n#Menu \ndef menu():\n global num #All options are chosen one by one\n print \"0.Quit.\"\n print \"1.Enter information\"\n print \"2.Report information\"\n print \"Choose one: \"\n num=int(input())\n return num #Return users selction\n\n#Enter information\ndef enter():\n for i in range(10):\n n=raw_input(\"Enter your name: \")\n name.append(n)\n phone.append(int(input(\"Enter your phone number\")))\n hours.append(int(input(\"Enter number of hours worked: \")))\n wage.append(int(input(\"Enter wage: \")))\n\n#Display report\ndef report():\n p=len(name)\n for i in range(p):\n print name[i],' ',phone[i]\n print \"Pay for the week: \",wage[i]*hours[i]\n\n\nwhile True:\n ch=menu() #get selection\n if ch==0:\n break\n elif ch==1:\n enter()\n elif ch==2:\n report()\n else:\n print \"Try again.\"\n if ch==0:\n break\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "0.Quit.\n1.Enter information\n2.Report information\nChoose one: \n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "1\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Anny\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number987654321\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 10\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 50\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Billy\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number9456783652\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 10\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 50\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Catherene\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number9476836578\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 10\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 50\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Dolly\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number9831356748\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 15\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 50\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Emily\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number9576843721\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 15\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 40\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Jack\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number9485738567\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 10\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 45\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Kevin\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number9345678923\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 10\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 45\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Lily\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number9345672831\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 10\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 45\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Monica\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number9475867483\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 10\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 50\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name: Irene\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your phone number5674356776\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter number of hours worked: 10\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter wage: 20\n"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "0.Quit.\n1.Enter information\n2.Report information\nChoose one: \n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "2\n"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "Anny 987654321\nPay for the week: 500\nBilly 9456783652\nPay for the week: 500\nCatherene 9476836578\nPay for the week: 500\nDolly 9831356748\nPay for the week: 750\nEmily 9576843721\nPay for the week: 600\nJack 9485738567\nPay for the week: 450\nKevin 9345678923\nPay for the week: 450\nLily 9345672831\nPay for the week: 450\nMonica 9475867483\nPay for the week: 500\nIrene 5674356776\nPay for the week: 200\n0.Quit.\n1.Enter information\n2.Report information\nChoose one: \n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "0\n"
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|