Week 2 class demo
import random
random.seed(42)
random.randrange(1,11)
2
random.randint(1,10)
1
colors = ['red', 'green', 'blue', 'fuzzy']
random.choice(colors)
'blue'
options = ['a', 'b', 'c', 'd', 'e', 'f']
options.append('g')
print(options)
['a', 'b', 'c', 'd', 'e', 'f', 'g']
random.shuffle(options)
options
['e', 'a', 'c', 'd', 'f', 'b']
random.shuffle(options)
print(options)
None
options
['e', 'a', 'c', 'd', 'f', 'b']
options
'd'
text = "
text = """India (official name: the Republic of India;[19] Hindi: Bhārat Gaṇarājya) is a country in South Asia. It is the seventh-largest country by area, the second-most populous country, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the southwest, and the Bay of Bengal on the southeast, it shares land borders with Pakistan to the west;[d] China, Nepal, and Bhutan to the north; and Bangladesh and Myanmar to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; its Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
Modern humans arrived on the Indian subcontinent from Africa no later than 55,000 years ago.[20] Their long occupation, initially in varying forms of isolation as hunter-gatherers, has made the region highly diverse, second only to Africa in human genetic diversity.[21] Settled life emerged on the subcontinent in the western margins of the Indus river basin 9,000 years ago, evolving gradually into the Indus Valley Civilisation of the third millennium BCE.[22] By 1200 BCE, an archaic form of Sanskrit, an Indo-European language, had diffused into India from the northwest, unfolding as the language of the Rigveda, and recording the dawning of Hinduism in India.[23] The Dravidian languages of India were supplanted in the northern regions.[24] By 400 BCE, stratification and exclusion by caste had emerged within Hinduism,[25] and Buddhism and Jainism had arisen, proclaiming social orders unlinked to heredity.[26] Early political consolidations gave rise to the loose-knit Maurya and Gupta Empires based in the Ganges Basin.[27] Their collective era was suffused with wide-ranging creativity,[28] but also marked by the declining status of women,[29] and the incorporation of untouchability into an organized system of belief.[e][30] In south India, the Middle kingdoms exported Dravidian-languages scripts and religious cultures to the kingdoms of southeast Asia.[31]
In the early medieval era, Christianity, Islam, Judaism and Zoroastrianism put down roots on India's southern and western coasts.[32] Armies from Central Asia intermittently overran India's plains,[33] eventually establishing the Delhi sultanate, and drawing northern India into the cosmopolitan networks of medieval Islam.[34] In the 15th century, the Vijayanagara Empire created a long-lasting composite Hindu culture in south India.[35] In the Punjab, Sikhism emerged, rejecting institutionalized religion.[36] The Mughal empire, in 1526, ushered in two centuries of relative peace,[37] leaving a legacy of luminous architecture.[f][38] Gradually expanding rule of the British East India Company followed, turning India into a colonial economy, but also consolidating its sovereignty.[39] British Crown rule began in 1858. The rights promised to Indians were granted slowly,[40] but technological changes were introduced, and ideas of education, modernity and the public life took root.[41] A pioneering and influential nationalist movement emerged,[42] which was noted for nonviolent resistance and led India to its independence in 1947."""
words = text.lower().split()
shuffledwords = words
random.shuffle(shuffledwords)
stop = int(len(shuffledwords) * .1)
text.lower().split()[:stop]
['india',
'(official',
'name:',
'the',
'republic',
'of',
'india;[19]',
'hindi:',
'bhārat',
'gaṇarājya)',
'is',
'a',
'country',
'in',
'south',
'asia.',
'it',
'is',
'the',
'seventh-largest',
'country',
'by',
'area,',
'the',
'second-most',
'populous',
'country,',
'and',
'the',
'most',
'populous',
'democracy',
'in',
'the',
'world.',
'bounded',
'by',
'the',
'indian',
'ocean',
'on',
'the',
'south,',
'the',
'arabian',
'sea']
shuffledwords[:stop]
['era',
'the',
'the',
'bce.[22]',
'river',
'isolation',
'in',
'as',
'their',
'coasts.[32]',
'sri',
'suffused',
'indus',
'the',
'india',
'to',
'in',
'and',
'christianity,',
'india',
'but',
'religious',
'diffused',
'were',
'the',
'archaic',
'and',
'indian',
'to',
'north;',
'jainism',
'root.[41]',
'human',
'india',
'by',
'caste',
'southeast',
'valley',
'the',
'vicinity',
'british',
'an',
'had',
'rule',
'colonial',
'independence']
base = 0
while True:
if base == 1000:
break
else:
base += 1
print(base)
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
status = True
base = 0
while status:
check = input("Do you want to continue? Say no to stop.")
if check.lower() == 'no':
print("bye!")
break
else:
print("Let's hit it again!")
Do you want to continue? Say no to stop.no
bye!
status = True
base = 0
while status:
check = input("Give me a number, say no to stop.")
if check.lower() == 'no':
print("bye!")
break
elif check.isnumeric():
base += int(check)
else:
print("Let's hit it again!")
print(base)
Give me a number, say no to stop.hello
Let's hit it again!
Give me a number, say no to stop.10
Give me a number, say no to stop.10
Give me a number, say no to stop.20
Give me a number, say no to stop.21
Give me a number, say no to stop.no
bye!
61
import random
total = 0
count = 0
while total <= 100:
total += random.randint(1,10)
count += 1
# print(total)
print("the final total is", total, "and it took", count, "trials to get there")
print("the mean number generated was", total/count)
the final total is 103 and it took 18 trials to get there
the mean number generated was 5.722222222222222
test = {'a': [1, 2]}
test['a'].append(10)
test['b'] = [10,20]
test
{'a': [1, 2, 10], 'b': [10, 20]}
test['b'] = [100, 200]
test
{'a': [1, 2, 10], 'b': [100, 200]}
text = "India (official name: the Republic of India;[19] Hindi: Bhārat Gaṇarājya) is a country in South Asia. It is the seventh-largest country by area, the second-most populous country, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the southwest, and the Bay of Bengal on the southeast, it shares land borders with Pakistan to the west;[d] China, Nepal, and Bhutan to the north; and Bangladesh and Myanmar to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; its Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia."
words = text.lower().split()
counts = {}
for word in words:
if word not in counts:
counts[word] = 1
else:
counts[word] += 1
counts
{'india': 2,
'(official': 1,
'name:': 1,
'the': 17,
'republic': 1,
'of': 3,
'india;[19]': 1,
'hindi:': 1,
'bhārat': 1,
'gaṇarājya)': 1,
'is': 3,
'a': 2,
'country': 2,
'in': 4,
'south': 1,
'asia.': 1,
'it': 2,
'seventh-largest': 1,
'by': 2,
'area,': 1,
'second-most': 1,
'populous': 2,
'country,': 1,
'and': 8,
'most': 1,
'democracy': 1,
'world.': 1,
'bounded': 1,
'indian': 2,
'ocean': 1,
'on': 3,
'south,': 1,
'arabian': 1,
'sea': 1,
'southwest,': 1,
'bay': 1,
'bengal': 1,
'southeast,': 1,
'shares': 1,
'land': 1,
'borders': 1,
'with': 2,
'pakistan': 1,
'to': 3,
'west;[d]': 1,
'china,': 1,
'nepal,': 1,
'bhutan': 1,
'north;': 1,
'bangladesh': 1,
'myanmar': 1,
'east.': 1,
'ocean,': 1,
'vicinity': 1,
'sri': 1,
'lanka': 1,
'maldives;': 1,
'its': 1,
'andaman': 1,
'nicobar': 1,
'islands': 1,
'share': 1,
'maritime': 1,
'border': 1,
'thailand': 1,
'indonesia.': 1}
wordcats = {'<=3': [], '4-6': [], '7+': []}
for word in words:
l = len(word)
if l <=3:
wordcats['<=3'].append(word)
elif l <=6:
wordcats['4-6'].append(word)
else:
wordcats['7+'].append(word)
print(wordcats)
{'<=3': ['the', 'of', 'is', 'a', 'in', 'it', 'is', 'the', 'by', 'the', 'and', 'the', 'in', 'the', 'by', 'the', 'on', 'the', 'the', 'sea', 'on', 'the', 'and', 'the', 'bay', 'of', 'on', 'the', 'it', 'to', 'the', 'and', 'to', 'the', 'and', 'and', 'to', 'the', 'in', 'the', 'is', 'in', 'the', 'of', 'sri', 'and', 'the', 'its', 'and', 'a', 'and'], '4-6': ['india', 'name:', 'hindi:', 'bhārat', 'south', 'asia.', 'area,', 'most', 'world.', 'indian', 'ocean', 'south,', 'bengal', 'shares', 'land', 'with', 'china,', 'nepal,', 'bhutan', 'north;', 'east.', 'indian', 'ocean,', 'india', 'lanka', 'share', 'border', 'with'], '7+': ['(official', 'republic', 'india;[19]', 'gaṇarājya)', 'country', 'seventh-largest', 'country', 'second-most', 'populous', 'country,', 'populous', 'democracy', 'bounded', 'arabian', 'southwest,', 'southeast,', 'borders', 'pakistan', 'west;[d]', 'bangladesh', 'myanmar', 'vicinity', 'maldives;', 'andaman', 'nicobar', 'islands', 'maritime', 'thailand', 'indonesia.']}
for key, value in wordcats.items():
print(key, len(value))
<=3 51
4-6 28
7+ 29
for countcat, listofwords in wordcats.items():
print(countcat, len(listofwords))
<=3 51
4-6 28
7+ 29
[return_this for this in sequence]
sen = 'the quick brown dog jumps over the lazy dog'.split()
sen
['the', 'quick', 'brown', 'dog', 'jumps', 'over', 'the', 'lazy', 'dog']
[w.title() for w in sen]
['The', 'Quick', 'Brown', 'Dog', 'Jumps', 'Over', 'The', 'Lazy', 'Dog']
[len(w) for w in sen]
[3, 5, 5, 3, 5, 4, 3, 4, 3]
result = []
for w in sen:
result.append(len(w))
result
[3, 5, 5, 3, 5, 4, 3, 4, 3]
[w for w in sen if len(w) <=3]
['the', 'dog', 'the', 'dog']
{key: value for key, value in dict.items()}
{key: len(value) for key, value in wordcats.items()}
{'<=3': 51, '4-6': 28, '7+': 29}
sen
['the', 'quick', 'brown', 'dog', 'jumps', 'over', 'the', 'lazy', 'dog']
{w: len(w) for w in sen}
{'the': 3, 'quick': 5, 'brown': 5, 'dog': 3, 'jumps': 5, 'over': 4, 'lazy': 4}
b = lambda x, y: x + y
1
1
sorted(counts, key = lambda item: counts[item], reverse = True)
['the',
'and',
'in',
'of',
'is',
'on',
'to',
'india',
'a',
'country',
'it',
'by',
'populous',
'indian',
'with',
'(official',
'name:',
'republic',
'india;[19]',
'hindi:',
'bhārat',
'gaṇarājya)',
'south',
'asia.',
'seventh-largest',
'area,',
'second-most',
'country,',
'most',
'democracy',
'world.',
'bounded',
'ocean',
'south,',
'arabian',
'sea',
'southwest,',
'bay',
'bengal',
'southeast,',
'shares',
'land',
'borders',
'pakistan',
'west;[d]',
'china,',
'nepal,',
'bhutan',
'north;',
'bangladesh',
'myanmar',
'east.',
'ocean,',
'vicinity',
'sri',
'lanka',
'maldives;',
'its',
'andaman',
'nicobar',
'islands',
'share',
'maritime',
'border',
'thailand',
'indonesia.']
def sortme(pair):
return pair[1]
sorted(counts.items(), key = sortme, reverse = True)
[('the', 17),
('and', 8),
('in', 4),
('of', 3),
('is', 3),
('on', 3),
('to', 3),
('india', 2),
('a', 2),
('country', 2),
('it', 2),
('by', 2),
('populous', 2),
('indian', 2),
('with', 2),
('(official', 1),
('name:', 1),
('republic', 1),
('india;[19]', 1),
('hindi:', 1),
('bhārat', 1),
('gaṇarājya)', 1),
('south', 1),
('asia.', 1),
('seventh-largest', 1),
('area,', 1),
('second-most', 1),
('country,', 1),
('most', 1),
('democracy', 1),
('world.', 1),
('bounded', 1),
('ocean', 1),
('south,', 1),
('arabian', 1),
('sea', 1),
('southwest,', 1),
('bay', 1),
('bengal', 1),
('southeast,', 1),
('shares', 1),
('land', 1),
('borders', 1),
('pakistan', 1),
('west;[d]', 1),
('china,', 1),
('nepal,', 1),
('bhutan', 1),
('north;', 1),
('bangladesh', 1),
('myanmar', 1),
('east.', 1),
('ocean,', 1),
('vicinity', 1),
('sri', 1),
('lanka', 1),
('maldives;', 1),
('its', 1),
('andaman', 1),
('nicobar', 1),
('islands', 1),
('share', 1),
('maritime', 1),
('border', 1),
('thailand', 1),
('indonesia.', 1)]
Last updated
Was this helpful?