-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensive Python Program Examples.py
More file actions
5573 lines (4149 loc) · 196 KB
/
Extensive Python Program Examples.py
File metadata and controls
5573 lines (4149 loc) · 196 KB
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
# Here are some extensive hands-on, in depth Python program examples.
# Created by Joseph C. Richardson, GitHub.com
# I am almost a complete Walking Human Computer Science Research Laboratory
# Machine on Two Legs. 😁
# Use three single quote marks at the beginning and
# at the end of the print() function text.
print('''print things on different lines with just one print()
function. That way you don't have create more print()
functions. All you need to do is press Enter to begin
a new line of text within your one and only print()
function. You can type as much text as you please.''')
# Use three double quote marks at the beginning and
# at the end of the print() function text.
print("""print things on different lines with just one print()
function. That way you don't have create more print()
functions. All you need to do is press Enter to begin
a new line of text within your one and only print()
function. You can type as much text as you please.""")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's use one variable to satisfy the print() function.
my_text = '''print things on different lines with just one print()
function. That way you don't have create more print()
functions. All you need to do is press Enter to begin
a new line of text within your one and only print()
function. You can type as much text as you please.'''
print(my_text)
my_text = """print things on different lines with just one print()
function. That way you don't have create more print()
functions. All you need to do is press Enter to begin
a new line of text within your one and only print()
function. You can type as much text as you please."""
print(my_text)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create two packed variables with different text paragraphs
# in them.
my_text1,my_text2 = ("""Don't be afraid of making mistakes, they are our greatest
lessons through life. When we learn from them, they become our greatest teachers
through life and we become their greatest, lifelong student...""",
"""Science is not just for smart people from a university. Science can and often
does happen for anyone. All you really need is a bit of imagination and a whole
lot of trial and error. Anyone can achieve anything they so wish. All you have to
do is believe in yourself, while you make all kinds of mistakes along the way.
Sooner or later, what doesn't work now will eventually work out, when you least
expect it. Science and discovery is all about trial and error. So don't become
discouraged when something won't work out right. Give it some time, before you
know it, you will start to see the sparks fly...""")
# use a plus '+' sign after the double '\n\n' line break
print(my_text1,'\n\n'+my_text2)
# or use plus '+' signs before and after the double '\n\n' line break
print(my_text1+'\n\n'+my_text2)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Unpack multiple values, using just one, single "=" sign.
# Note: You must use equal variables to equal values.
a,b,c=1,2,3
print(a,b,c)
# Add the values together.
print(a+b+c)
# Example 2:
a,b,c,d,e,f=1,2,3,4,5,6
print(a,b,c,d,e,f)
# Add the values together.
print(a+b+c+d+e+f)
# Example 3
name1,name2,name3='Bob','Rob','John'
print(name1,'and',name2,'went to',name3+"'s",'house for dinner.')
# You can use the 'f' format to make the above print statement
# read like this.
print(f"{name1} and {name2} went to {name3}'s house for dinner.")
# Old format example of the print statement from earlier Python versions.
print("{} and {} went to {}'s house for dinner.".format(name1,name2,name3))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's learn what Python tuples are all about with these
# Python program examples.
# print out tuple values onto the screen output
my_tuple = ('Python',"Programmer's",'Glossary','Bible')
print(my_tuple[0])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# this is a tuple() by default, without the parantheses '()'
default_tuple = 'Python',"Programmer's",'Glossary','Bible'
print(default_tuple)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# access 'my_tuple' values 1 through 4 without printing
# the value 'Python'
my_tuple = ('Python',"Programmer's",'Glossary','Bible')
print(my_tuple[1:4])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# access 'my_tuple' values 1 through 4 without printing
# the value 'Python'
my_tuple = ('Python',"Programmer's",'Glossary','Bible')
print(my_tuple[1:])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# access three of my_tuple values 0 through 3 without printing
# the value 'Bible'
my_tuple = ('Python',"Programmer's",'Glossary','Bible')
print(my_tuple[:3])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# unpack tuple or list values; both examples will work the same
# way. Refer to learning and creating lists
my_tuple = ('Python',"Programmer's",'Glossary','Bible')
(we,sure,love,Python) = my_tuple
print(we,sure,love,Python)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# unpack and assign tuple or list values with an asterisk ' * ',
# both examples will work the same way. Refer to learning
# and creating lists.
my_tuple = ('Python',"Programmer's",'Glossary','Bible')
(we,sure,*love,Python) = my_tuple
print(we,sure,love,Python)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# create a variable to join 'my_tuple1' with 'my_tuple2'
my_tuple1 = ('Python',"Programmer's",'Glossary','Bible')
my_tuple2 = ('We','sure','love','Python')
tuple_join = my_tuple1+my_tuple2
print(tuple_join)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the asterisk key ' * ' to duplicate all values in 'my_tuple' by three,
# followed by an integer count duplicator value ' 3 ', which multiplies
# the entire tuple values three times
my_tuple = ('Python',"Programmer's",'Glossary','Bible')
print(my_tuple*3) # assign any integer number value
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# place a tuple within another tuple, while each tuple set
# returns their own indexed values
my_tuple = (('Python',"Programmer's",'Glossary','Bible'),('We','love','Python'))
print(my_tuple[0])
print(my_tuple[1])
print(my_tuple[1][1])
print(my_tuple[0][0])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# create a multi-dimensional tuple
multi_dim_tuple = (
('Python',"Programmer's",'Glossary','Bible'),
('We','Love','Python!'),('Python','is','Greate!'))
print(multi_dim_tuple[0][0])
print(multi_dim_tuple[2][1])
print(multi_dim_tuple[2][2])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the 'join()' function to join tuple items together
# with a separator '//' string. The separator string can
# be any character/characters; two backslashes '//' are
# used for this Python program example.
tuple_join = ('Python',"Programmer's",'Glossary','Bible')
separater = '//'
print(separater.join(tuple_join))
print(separater.join(tuple_join[1]))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's learn what Python lists are all about with these
# Python program examples.
# print out list values onto the screen output
my_list = ['Python',"Programmer's",'Glossary','Bible']
print(my_list[0])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# change 'my_list' value 'Bible' to the value 'Book'
my_list = ['Python',"Programmer's",'Glossary','Bible']
my_list[3] = 'Book'
print(my_list[3])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# insert three new values into 'my_list' with indexing
my_list = ['Python',"Programmer's",'Glossary','Bible']
my_list[2:0] = ['Fun','Greate','Book']
print(my_list[2])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# add the value 'Book' to 'my_list' with the append() function
my_list = ['Python',"Programmer's",'Glossary','Bible']
my_list.append('Book')
print(my_list[4])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# insert a new value 'Book' into 'my_list' at index[3]
# with the 'insert()' function
my_list = ['Python',"Programmer's",'Glossary','Bible']
my_list.insert(3,'Book')
print(my_list[3])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# remove the last value at the end of 'my_list' with the
# pop() function
my_list = ['Python',"Programmer's",'Glossary','Bible']
# create a variable called popped, so you can see the
# value that was removed or popped off 'my_list', via the pop()
# function
popped = my_list.pop()
print(popped)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# remove the second value of 'my_list' with the pop() function
my_list = ['Python',"Programmer's",'Glossary','Bible']
# create a variable called popped, so you can see the
# value that was removed or popped off 'my_list', via the pop()
# function
popped = my_list.pop(1)
print(popped)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# remove a value from 'my_list' with the remove() function
my_list = ['Python',"Programmer's",'Glossary','Bible']
my_list.remove("Programmer's")
print(my_list[1])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# empty all values in 'my_list' with the clear() function,
# without deleting the 'my_list' variable
my_list = ['Python',"Programmer's",'Glossary','Bible']
my_list.clear()
print(my_list)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# delete the entire 'my_list' variable and values, via
# invoking the 'del' prefix. Note: when you type and
# execute/run this Python program example, the program
# will cause and error and crash the program. This is
# simply because 'my_list' doesn't exist anymore
my_list = ['Python',"Programmer's",'Glossary','Bible']
del my_list
print(my_list)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# delete the 'my_list' value at index[1], via invoking the 'del'
# prefix
my_list = ['Python',"Programmer's",'Glossary','Bible']
del my_list[1]
print(my_list[1])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# copy all the values in 'my_list' to the items_list with the
# copy() function, then check the two lists to view them
# on the screen output at program execution time
my_list = ['Python',"Programmer's",'Glossary','Bible']
items_list = my_list.copy()
print(items_list)
print(my_list)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# create a variable to join 'my_list1' with 'my_list2'
my_list1 = ['Python',"Programmer's",'Glossary','Bible']
my_list2 = ['We','sure','love','Python']
list_join = my_list1+my_list2
print(list_join)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# join two lists together with the extend() function, then
# check 'my_list1' to view them on the screen output at
# execution time
my_list1 = ['Python',"Programmer's",'Glossary','Bible']
my_list2 = ['We','Sure','Love','Python','Programming']
my_list1.extend(my_list2)
print(my_list1)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# unpack list or tuple values; both examples will work the same
# way. Refer to learning and creating tuples
my_list = ['Python',"Programmer's",'Glossary','Bible']
(we,sure,love,Python) = my_list
print(we,sure,love,Python)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# unpack and assign list or tuple values with an asterisk ' * ',
# both examples will work the same way. Refer to learning
# and creating tuples
my_list = ['Python',"Programmer's",'Glossary','Bible']
(we,sure,*love,Python) = my_list
print(we,sure,love,Python)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the sorrt() function to sort 'my_list' values in
# alphabetical order
my_list = ['Python',"Programmer's",'Glossary','Bible']
my_list.sort()
print(my_list)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the sorted() function to temporarily sort 'my_list'
# values without sorting the actual 'my_list' values
my_list = ['Python',"Programmer's",'Glossary','Bible']
sorted_list = sorted(my_list)
print(sorted_list)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the asterisk key ' * ' to duplicate all values in 'my_list' by three,
# followed by an integer count duplicator value ' 3 ', which multiplies
# the entire list values three times
my_list = ['Python',"Programmer's",'Glossary','Bible']
print(my_list*3) # assign any integer number value
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# place a list within another list, while each list set
# returns their own indexed values
my_list = [['Python',"Programmer's",'Glossary','Bible'],['We','love','Python']]
print(my_list[0])
print(my_list[1])
print(my_list[1][1])
print(my_list[0][0])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# List Comprehension in Python
# Like always, I research when I want to learn something new, or new to me.
# Since the years I've played around with Python programming, I never ever
# explored list comprehension at all. This is like killing two birds with one stone's
# throw. I'm only experimenting with this tonight; I kept on ignoring it. But it
# shortens for loops down, when looping through a list, using a for loop right
# inside the list[ ] brackets. Believe it or not, I'm also new to this list comprehension
# thing. So you are not alone on this one. But try these out and tinker with them.
# I did that here. I just play and tinker and see what works and what won't work.
# This is what computer science is all about. Sometimes you just have to keep on
# learning and do constant research; you can only avoid something for so long,
# like I did with this headache. But like everything we learn; it gets easier over time.
# Happy researching. I'm also happily doing research with these below.
text_list = ['dog','cat','bird','fish','turtle']
words = [text for text in text_list]
print(words)
text_list = ['dog','cat','bird','fish','turtle']
words = [text if text == 'dog' else 'none' for text in text_list]
print(words[1])
num_list = [1,2,3,4,5]
nums = [num for num in num_list]
print(nums)
num_list = []
nums = [num for num in range(20)]
print(nums)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# create a multi-dimensional list
multi_dim_list = (
['Python',"Programmer's",'Glossary','Bible'],
['We','Love','Python!'],['Python','is','Greate!'])
print(multi_dim_list[0][0])
print(multi_dim_list[2][1])
print(multi_dim_list[2][2])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Unpacking multi-list example:
list_1,list_2,list_3=[
[0,1,2,3,4,5,6,7,8,9],
['a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'],
['"Python',"Programmer's",'Glossary','Bible"']]
print(list_1[9])
print(list_2[0])
print(list_3[0],list_3[1],list_3[2],list_3[3])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Unpacking multi-list for-loop example:
list_1,list_2,list_3=[
[0,1,2,3,4,5,6,7,8,9],
['a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'],
['"Python',"Programmer's",'Glossary','Bible"']]
for i in list_1,list_2,list_3:
print(i[0],i[1],i[2],i[3])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Things you can do with tuples and lists.
# Tuple Example:
variable_1='dog','cat','bird','guppy'
variable_2=(
'Grey Wolf','Huge Tigger',
'Macaw Parrot','Great White Shark')
variable_3='John','Rob','Ron','Bob'
variable_4='Mom','Dad','Brother','Sister'
variable_5='friend','girlfriend','boyfriend','neighbour'
for var1,var2,var3,var4,var5 in zip(variable_1,variable_2,variable_3,variable_4,variable_5):
print(f'My {var4} and my {var5} {var3} says my {var1} is really a {var2}.')
# List Example:
variable_1=['dog','cat','bird','guppy']
variable_2=[
'Grey Wolf','Huge Tigger',
'Macaw Parrot','Great White Shark']
variable_3=['John','Rob','Ron','Bob']
variable_4=['Mom','Dad','Brother','Sister']
variable_5=['friend','girlfriend','boyfriend','neighbour']
for var1,var2,var3,var4,var5 in zip(variable_1,variable_2,variable_3,variable_4,variable_5):
print(f'My {var4} and my {var5} {var3} says my {var1} is really a {var2}.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let Python help you create a list of values faster with this Python program example
empty_list = []
while True:
user_input = input("Let's create a list of values for 'empty_list': ").lower().strip()
empty_list.append(user_input)
if user_input == 'quit':
break
elif user_input == '':
empty_list.pop(0)
empty_list.pop() # removes the very last value 'quit' in the empty_list
# check your 'empty_list' values that you created
print(f"\nPython helped you create your 'empty_list' values:\n{empty_list}")
# call an 'empty_list' value with its index value range
try:
print(empty_list[0])
except IndexError:
print('index out of range:')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the 'join()' function to join list items together
# with a separator '//' string. The separator string can
# be any character/characters; two backslashes '//' are
# used for this Python program example.
list_join = ['Python',"Programmer's",'Glossary','Bible']
separater = '//'
print(separater.join(list_join))
print(separater.join(list_join[1]))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# You can use Emojis in Python. I just discovered this on my own, just to see if
# it would work. And it does work! WOW!! Use YouTube Emojis in your very own
# Python program examples. Use them for creating logical programs that if
# the user doesn't press the right key, you can program the Emojis to make
# an angry face. If the user does what he wants, them make the Emojis make
# a happy face and so on. Who knows? I just might create us a Python program
# to do such. Copy and then paste this program into your Python App and
# execute/run it and see what happens. Double click the file to open it so you
# can see the colour of the Emojis face. Save the file as Emojis and run it.
print('I am almost a complete Walking Human Computer Science \
Research Laboratory Machine on Two Legs. 😁')
input('Press Enter to exit the program')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Here are 90 emojis you can use within your Python programs.
# Please note: these were not easy to create. The emojis can't
# be copied into a list as easy as ordinary Python lists. If you
# add any of your own, make sure you don't make any mistakes.
# Python's undo button and or ctrl+z does not work; you have
# to keep undoing the mistake or the ones you don't want in the
# list, and emojis you add to the Python list. As I said, these
# weren't easy to create at all. I think 90 emojis is more than
# a great start. Don't you?
# Save the file as Emojis and then double click the Python file's
# icon to see the emojis in colour as shown in the Python program
# example. Please note: These emojis in the actual Python idle are
# green and outlined, not coloured as shown on this YouTube
# community page.
emojis_list = [
'😀','😄','😁','😆','😅','🤣','😂','🙂','🙃','😉',
'😊','😇','🥰','😍','🤩','😘','😗','😚','🥲','😋',
'😛','😜','🤪','😝','🤑','🤗','🤭','🤫','🤔','🤐',
'🤨','😐','😑','😶','😶🌫','😏','😒','🙄','😬','😮💨',
'🤥','😌','😔','😪','🤤','😴','😷','🤒','🤕','🤢',
'🤧','🥵','🥶','🥴','😵','😵💫','🤯','🤠','🥳','🥸',
'😎','🤓','🧐','😕','😟','🙁','😮','😯','😲','😳',
'🥺','😦','😧','😨','😰','😥','😢','😭','😱','😖',
'😣','😞','😓','😩','😫','🥱','😤','😡','😠','🤬']
print(emojis_list[2])
input('Press Enter to exit the program.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's learn what Python dictionaries are all about with these
# Python program examples.
# print out key values onto the screen output
my_dictionary = {
'key1':'Python','key2':"Programmer's",'key3':'Glossary','key4':'Bible'}
print(my_dictionary.get('key1'))
print(my_dictionary.get('key2'))
print(my_dictionary.get('key3'))
print(my_dictionary.get('key4'))
print(my_dictionary.get('key5'))
print(my_dictionary.get('key5','Key Not Found!')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# print out key values with a partial tuple within 'my_dictionary_list'
my_dictionary_tuple = {
'key1':'Python','key2':("Programmer's",'Glossary','Bible'),'key3':'is','key4':'Great!'}
print(my_dictionary_tuple.get('key1'))
print(my_dictionary_tuple.get('key2')[0])
print(my_dictionary_tuple.get('key2')[1])
print(my_dictionary_tuple.get('key2')[2])
print(my_dictionary_tuple.get('key3'))
print(my_dictionary_tuple.get('key4'))
print(my_dictionary_tuple.get('key1','Key Not Found!')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# print out key values with a partial list within 'my_dictionary_list'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
print(my_dictionary_list.get('key1'))
print(my_dictionary_list.get('key2')[0])
print(my_dictionary_list.get('key2')[1])
print(my_dictionary_list.get('key2')[2])
print(my_dictionary_list.get('key3'))
print(my_dictionary_list.get('key4'))
print(my_dictionary_list.get('key5','Key Not Found!')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# update a key's value to change its value in 'my_dictionary_list'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
my_dictionary_list.update({'key1':'Book'}) # key1 value was 'Python'
print(my_dictionary_list.get('key1')) # Book
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# update a key's value to change its value in 'my_dictionary_list'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
my_dictionary_list['key1'] = 'Book' # key1 value was 'Python'
print(my_dictionary_list.get('key1')) # Book
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the 'pop()' function to delete/pop 'key2' from 'my_dictionary_list'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
my_dictionary_list.pop('key2')
print(my_dictionary_list.get('key2','Sorry! Key Not Found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# return a 'poopped' dictionary{ } 'key' from 'my_dictionary_list'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
popped = my_dictionary_list.pop('key1')
print(popped)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# completely delete a dictionary{ } 'key' from 'my_dictionary_list'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
del my_dictionary_list['key1']
print(my_dictionary_list.get('key1','Sorry! Key Not Found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# clear the entire 'my_dictionary_list'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
my_dictionary_list.clear()
print(my_dictionary_list.get('key1','Sorry! Key Not Found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the 'items()' function to view the complete 'my_dictionary_list'
# with 'keys' and 'key values'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
list_items = my_dictionary_list.items()
print(list_items)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the 'items()' function to view the 'my_dictionary_list' 'keys' and 'key values'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
# invoke the 'items()' function to view the 'my_dictionary_list' 'keys' only
print(my_dictionary_list.keys())
# invoke the 'items()' function to view the 'my_dictionary_list' 'values' only
print(my_dictionary_list.values())
# invoke the 'items()' function using a for loop to view the 'my_dictionary_list'
# 'keys' and 'key values'
for keys, values in my_dictionary_list.items():
print(keys, values)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# change a key's value in 'my_dictionary_list'
my_dictionary_list = {
'key1':'Python','key2':["Programmer's",'Glossary','Bible'],'key3':'is','key4':'Great!'}
my_dictionary_list['key1'] = 'We Sure Love Python!'
print(my_dictionary_list.get('key1'))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create an animals dictionary so we can use its values.
animals={
'Dog':'Wolf',
'Cat':'Lion',
'Bird':'Eagle',
'Fish':'Shark'
}
print(animals.get('dog'))
print(animals.get('dog','Not Found!')) # optional
print(animals.get('Dog','Not Found!')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
for key,value in animals.items():
print(key)
for key,value in animals.items():
print(value)
for key,value in animals.items():
print(key,value)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create some sentences out of our animals dictionary list.
d=animals.get('Dog')
c=animals.get('Cat')
b=animals.get('Bird')
f=animals.get('Fish')
print(f'My dog is really a {d}.')
print(f'My Cat is really a {c}.')
print(f'My Bird is really a {b}.')
print(f'My Fish is really a {f}.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create some sentences out of our animals dictionary list
# using a 'for in' items() function to drastically reduce lines of
# code and code redundancy in our Python program example.
for keys,values in animals.items():
print(f'My {keys} is really a {values}.')
# Rename the key and value variables if you wish.
for my_keys,my_values in animals.items():
print(f'My {my_keys} is really a {my_values}.')
for animal_keys,animal_values in animals.items():
print(f'My {animal_keys} is really a {animal_values}.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Try this dictionary Python program example below and recap
# what happens when you type and execute/run this program.
animals={
'Dog':'Wolf',
'Cat':'Lion',
'Bird':'Eagle',
'Fish':'Shark'}
people={
'Person1':'Tom',
'Person2':'Bob',
'Person3':'Rob',
'Person4':'Ron'}
for key,value in animals.items():
print(key,value)
for key,value in people.items():
print(key,value)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create a dictionary that's also a complete list of values
# to one, single key each.
my_dictionary_list={
'key1':['Dog,','Cat','Bird','fish'],
'key2':['Tom','Bob','Rob','Ron']}
print(my_dictionary_list.get('key1')[0])
print(my_dictionary_list.get('key2')[0])
print(my_dictionary_list.get('key5','Sorry! Key Not Found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create a dictionary that uses strings as keys and values, instead of
# of actual text, like we did before. Let's create two, simple tuples; one for
# the key tuple and one for the value tuple. We can also create them with or
# without parentheses, but a '\' backslash must be implemented in place of the
# parentheses. However, the Python programming standard shows only the
# constant use of parentheses, not backslashes, as you can see in the next
# example below.
key='dog','cat','mouse','bird','fish' # tuple by default
value=(
'Grey Wolf','Huge Tigger',
'Black Rat','Macaw Parrot',
'Great White Shark') # create a tuple with '()' parentheses.
# Why use '()' parentheses when you can simply use the '\' backslash instead.
# Note: '\' is not the usual Python programming standard, but it works. Now,
# however, this only acts like a tuple by default, not a list as one would think.
# You cannot change or modify tuple values at all; they are immutable, not
# mutable like lists. Even though this works, it's not viable, especially when
# you need to create a mutable list, not an immutable tuple, as this example
# does by default. You must use either '()' parentheses for tuples, '[]' square
# brackets for lists and '{}' curly braces for dictionaries and sets alike.
key='dog','cat','mouse','bird','fish' # tuple by default
value=\
'Grey Wolf','Huge Tigger',\
'Black Rat','Macaw Parrot',\
'Great White Shark' # tuple by default
dictionary={ # dictionary
key[0]:value[0],
key[1]:value[1],
key[2]:value[2],
key[3]:value[3],
key[4]:value[4]
}
# Non formatted examples with commas ',' and plus '+' signs
for keys,values in dictionary.items():
print('My '+keys+' is really a '+values+'.')
for keys,values in dictionary.items():
print('My',keys,'is really a',values+'.')
# Old formatted example: now depreciated in Python 3 and up.
# Can still be used in Python 3, thus far.
for keys,values in dictionary.items():
print('My {} is really a {}.'.format(keys,values))
# New formatted example: Python 3 and up.
for keys,values in dictionary.items():
print(f'My {keys} is really a {values}.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# create a dictionary
print(dict(
we='Python',sure="Programmer's",love='Glassary',Python='Bible'))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# invoke the 'join()' function to join dictionary keys together
# with a separator '//' string. The separator string can
# be any character/characters; two backslashes '//' are
# used for this Python program example.
dictionary_join = {
'key1':'Python','key2':"Programmer's",'key3':'Glossary','key4':'Bible'}
separater = '//'
print(separater.join(dictionary_join))
print(separater.join(dictionary_join.get('key1')))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Python dictionaries are pretty neat for data manipulation.
# You can call keys to return values. You can also use integers
# for keys and values to completely manipulate data
# dictionary 'keys' return values. Note: the word key can be any
# name you like. Play with the text values and make sentences
# out of them
dictionary = {
'key 1':'Value 1','key 2':'Value 2','key 3':'Value 3',
'key 4':'Value 4','key 5':'Value 5','key 6':'Value 6',
'key 7':'Value 7','key 8':'Value 8'}
# use a variable if you like
key = dictionary.get('key 4')
print(key)
# or this:
key = dictionary.get('key 11','Key Not Found:') # KNF optional
print(key)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dictionary = {
'key 1':'Value 1','key 2':'Value 2','key 3':'Value 3',
'key 4':'Value 4','key 5':'Value 5','key 6':'Value 6',
'key 7':'Value 7','key 8':'Value 8'}
# loop through dictionary keys
for i in dictionary:
print(i)
# loop through the dictionary values
for i in dictionary:
print(dictionary.get(i))
# loop through dictionary keys and values
for i in dictionary:
print(i,dictionary.get(i))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# use integers as dictionary keys to return values
dictionary = {
1:'Value 1',2:'Value 2',3:'Value 3',
4:'Value 4',5:'Value 5',6:'Value 6',
7:'Value 7',8:'Value 8'}
# use a variable if you like
key = dictionary.get(4)
print(key)
# or this:
key = dictionary.get(11,'Key Not Found:') # KNF optional
print(key)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# this is pure data manipulation at its finest.
# Use integer numbers for keys instead of text strings
dictionary = {
1:'Value 1',2:'Value 2',3:'Value 3',
4:'Value 4',5:'Value 5',6:'Value 6',
7:'Value 7',8:'Value 8'}
print(dictionary.get(1+2+5))
print(dictionary.get(2*5-2))
print(dictionary.get(16/2))
print(dictionary.get(16/2+1,'Key Not Found:'))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# use integers for keys and values for total data manipulation
dictionary = {
1:100,2:200,3:300,
4:400,5:500,6:600,