-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython3programs.txt
More file actions
3139 lines (1818 loc) · 61.7 KB
/
Copy pathpython3programs.txt
File metadata and controls
3139 lines (1818 loc) · 61.7 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
# python3programs.txt
#
# anne.dawson@gmail.com
#
# Last updated: Friday 17th February 2023, 10:49 PT, AD
#
# All Python programming resources by Dr Anne Dawson are here:
# https://www.annedawson.net/Resources165.html#N13
# Search for a programming topic at annedawson.net:
# https://cse.google.com/cse?cx=a0d9952fa0ee346b3#gsc.tab=0
# Please note: lines in Python code starting with a # are comments
# and are ignored by
# the Python interpreter...
# See:
# https://www.annedawson.net/PythonComments.txt
# for important information about comments.
# Any of the following example programs can be run by
# directly copying the desired program and pasting
# the code to a Python editor such as IDLE...
# For Python and Python IDLE installation and running instructions
# watch this video: https://youtu.be/3Xy221yv9A8
# For detailed instructions on how to use the Python IDLE environment
# to write a simple Python 3 program, watch this video:
# https://www.youtube.com/watch?v=lBkcDFRA958
# A video course for beginner programmers in Python 3
# composed of over 40 short videos
# can be found here:
# https://www.youtube.com/@AnneDawson/playlists
# The first Python program below (01-01.py) has only
# one executable line:
# print ("Hello World!")
# and one comment line
#################################################
# NOTE: These example programs run on Python 3. #
#################################################
#01-01.py
print ("Hello World!")
#01-02.py
thetext = input("Enter some text ")
print ("This is what you entered:")
print (thetext)
#01-03.py
# Note that \n within quote marks forces a new line to be printed
thetext = input("Enter some text\n")
print ("This is what you entered:")
print (thetext)
#01-04.py
prompt = "Enter a some text "
thetext = input(prompt)
print ("This is what you entered:")
print (thetext)
#02-01.py
total = 0.0
number1=float(input("Enter the first number: "))
total = total + number1
number2=float(input("Enter the second number: "))
total = total + number2
number3=float(input("Enter the third number: "))
total = total + number3
average = total / 3
print ("The average is " + str(average))
################################################################
# #
# 02-02.py #
# Purpose: to demonstrate storage of a floating point number #
# #
# Programmer: Anne Dawson #
# Friday 17th February 2023, 5:15 PT, AD #
# #
# See this resource to find out how the input function works: #
# https://www.annedawson.net/Python3_Input.txt #
# #
# See this resource to find out how important comments are: #
# https://www.annedawson.net/PythonComments.txt #
# #
################################################################
number1=float(input("Enter the first number: "))
number2=float(input("Enter the second number: "))
number3=float(input("Enter the third number: "))
total = number1 + number2 + number3
average = total / 3
print ("The average is: ")
print (average)
#02-03.py
total = 0.0
count = 0
while count < 3:
number=float(input("Enter a number: "))
count = count + 1
total = total + number
average = total / 3
print ("The average is " + str(average))
#03-01.py
total = 10
#03-02.py
total = 10
print (total)
#03-03.py
total = 10
print (total)
print (type (total))
#03-04.py
print (2 + 4)
print (6 - 4)
print (6 * 3)
print (6 / 3)
print (6 % 3)
print (6 // 3) # floor division: always truncates fractional remainders
print (-5)
print (3**2) # three to the power of 2
#03-05.py
print (2.0 + 4.0)
print (6.0 - 4.0)
print (6.0 * 3.0)
print (6.0 / 3.0)
print (6.0 % 3.0)
print (6.0 // 3.0) # floor division: always truncates fractional remainders
print (-5.0)
print (3.0**2.0) # three to the power of 2
#03-06.py
# mixing data types in expressions
# mixed type expressions are "converted up"
# converted up means to take the data type with the greater storage
# float has greater storage (8 bytes) than a regular int (4 bytes)
print (2 + 4.0)
print (6 - 4.0)
print (6 * 3.0)
print (6 / 3.0)
print (6 % 3.0)
print (6 // 3.0) # floor division: always truncates fractional remainders
print (-5.0)
print (3**2.0) # three to the power of 2
#03-07.py
# these are Boolean expressions which result in a value of
# true or false
# Note that Python stores true as integer 1, and false as integer 0
# but outputs 'true' or 'false' from print statements
print (7 > 10)
print (4 < 16)
print (4 == 4)
print (4 <= 4)
print (4 >= 4)
print (4 != 4)
#03-08.py
# these are string objects
print ("Hello out there")
print ('Hello')
print ("Where's the spam?")
print ('x')
#03-09.py
# these are string assignments
a = "Hello out there"
print (a)
b = 'Hello'
print (b)
c = "Where's the spam?"
print (c)
d = 'x'
print (d)
#03-10.py
a = 'Hello out there'
b = "Where's the spam?"
c = a + b
print (c)
#03-11.py
a = 'Hello out there'
b = "Where's the spam?"
c = a + b
print (c)
#d = c + 10
# you cannot concatenate a string and an integer
# you must convert the integer to a string first:
d = c + str(10)
print (d)
#03-12.py
a = "10"
b = '99'
c = a + b
print (c)
print (type(c))
c = int(c)
print (c)
print (type(c))
# 03-13.py
# How to round up a floating point number
# to the nearest integer
# Friday 17th February 2023, 5:15 PT, AD
x = 1.6
print (x)
x = round(x)
print (x)
#compare the above with
x = 1.6
x = int(x)
print (x)
# 03-14.py
# How to round a float number to 2 decimal places,
# and output that number as $ currency
# printed with a comma after the number of thousands
# i.e. print 1234.5678 as $1,234.57
# for a list of Python 3's built-in functions, see the link below:
# http://docs.python.org/py3k/library/functions.html
###########################################################################
# PLEASE NOTE: This program will work correctly for some
# input values, but has not been modified to work in all cases.
###########################################################################
# Friday 17th February 2023, 5:15 PT, AD
number = 1234.5678
print (number)
number = round(number,2)
print (number)
# the above line rounds the number to 2 decimal places
thousands = number / 1000
print (thousands)
thousands = int(thousands)
print (thousands)
remainder = number % 1000
print (remainder)
pretty_output = "$" + str(thousands) + "," + str(remainder)
print (pretty_output)
# File: 04-01.py
# Purpose: Creating a string object
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
number1 = input("Enter first number:\n")
print (number1, type(number1))
# File: 04-02.py
# Purpose: Converting one data type to another
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
number1 = input("Enter first number:\n")
print (number1, type(number1))
number1 = int(number1)
print (number1, type(number1))
# File: 04-03.py
# Purpose: Displaying an object's memory location
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
number1 = input("Enter first number:\n")
print (number1, type(number1), id(number1))
number1 = int(number1)
print (number1, type(number1), id(number1))
# File: 04-04.py
# Purpose: Examples of use of arithmetic operators
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
print (2 + 4)
print (6 - 4)
print (6 * 3)
print (6 / 3)
print (6 % 3)
print (6 // 3) # floor (integer) division: always truncates fractional remainders
print (-5)
print (3**2) # three to the power of 2
# File: 04-05.py
# Purpose: Examples of use of arithmetic operators with float values
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
print (2.0 + 4.0)
print (6.0 - 4.0)
print (6.0 * 3.0)
print (6.0 / 3.0)
print (6.0 % 3.0)
print (6.0 // 3.0) # floor (integer) division: always truncates fractional remainders
print (-5.0)
print (3.0**2.0) # three to the power of 2
# File: 04-06.py
# Purpose: Examples of use of arithmetic operators
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# mixing data types in expressions
# mixed type expressions are "converted up"
# converted up means to take the data type with the greater storage
# float has greater storage (8 bytes) than a regular int (4 bytes)
print (2 + 4.0)
print (6 - 4.0)
print (6 * 3.0)
print (6 / 3.0)
print (6 % 3.0)
print (6 // 3.0) # floor division: always truncates fractional remainders
print (-5.0)
print (3**2.0) # three to the power of 2
# File: 04-07.py
# Purpose: Examples of use of Boolean expressions
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# these are Boolean expressions which result in a value of
# true or false
# Note that Python stores true as integer 1, and false as integer 0
# but outputs 'true' or 'false' from print statements
# If you input Boolean values, you must input 1 or 0.
print (7 > 10)
print (4 < 16)
print (4 == 4)
print (4 <= 4)
print (4 >= 4)
print (4 != 4)
# File: 04-08.py
# Purpose: Displaying boolean values
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
number = 10
isPositive = (number > 0)
print (isPositive)
# File: 04-09.py
# Purpose: Combining boolean expressions with and
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
age = 25
salary = 55000
print ((age > 21) and (salary > 50000))
# File: 04-10.py
# Purpose: The if statement
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# The condition of the following if statement
# follows the word if, and ends with a colon (:)
# In this example, if x has a value equal to 'spam',
# then 'Hi spam' will be printed.
x = 'spam'
if x == 'spam':
print ('Hi spam')
else:
print ('not spam')
# Notice the indentation (spacing out) of this code.
# The statement(s) following the if condition (i.e. boolean expression)
# must be indented to the next tab stop. This means you must press
# the Tab button before typing the word print.
# Try removing the tab spaces and see what happens when you attempt to run.
# File: 04-11.py
# Purpose: The if statement with multiple statements
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# The condition of the following if statement
# follows the word if, and ends with a colon (:)
# In this example, if x has a value equal to 'spam',
# then 'Hi spam\n' will be printed followed by
# "Nice weather we're having"
# followed by 'Have a nice day!'
x = 'spam'
if x == 'spam':
print ('Hi spam\n')
print ("Nice weather we're having")
print ('Have a nice day!')
else:
print ('not spam')
# Notice the indentation (spacing out) of this code.
# The statement(s) following the if condition (i.e. boolean expression)
# must be indented to the next tab stop. This means you must press
# the Tab button before typing the word print.
# Try removing the tab spaces and see what happens when you attempt to run.
# File: 04-12.py
# Purpose: The if statement with multiple statements
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# The condition of the following if statement
# follows the word if, and ends with a colon (:)
# In this example, if x has a value equal to 'spammy',
# then 'Hi spam\n' will be printed followed by
# "Nice weather we're having"
# followed by 'Have a nice day!'
x = 'spam'
if x == 'spammy':
print ('Hi spam\n')
print ("Nice weather we're having")
print ('Have a nice day!')
else:
print ('not spam')
print ('Not having a good day?')
# Notice the indentation (spacing out) of this code.
# The statement(s) following the if condition (i.e. boolean expression)
# must be indented to the next tab stop. This means you must press
# the Tab button before typing the word print.
# Try removing the tab spaces and see what happens when you attempt to run.
# Program: 04-13.py
# Purpose: A nested if example (an if statement within another if statement)
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
score = input("Enter score: ")
score = int(score)
if score >= 80:
grade = 'A'
else:
if score >= 70:
grade = 'B'
else:
grade = 'C'
print ("\n\nGrade is: " + grade)
# Program: 04-14.py
# Purpose: A nested if example - using if/else
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
score = input("Enter score: ")
score = int(score)
if score >= 80:
grade = 'A'
else:
if score >= 70:
grade = 'B'
else:
if score >= 55:
grade = 'C'
else:
if score >= 50:
grade = 'Pass'
else:
grade = 'Fail'
print ("\n\nGrade is: " + grade)
# Program: 04-15A.py
# Purpose: A nested if example - using if/elif/else
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
score = input("Enter score: ")
score = int(score)
if score > 80 or score == 80:
grade = 'A'
elif score > 70 or score == 70:
grade = 'B'
elif score > 55 or score == 55:
grade = 'C'
elif score > 50 or score == 50:
grade = 'Pass'
else:
grade = 'Fail'
print ("\n\nGrade is: " + grade)
# Program: 04-15B.py
# Purpose: A nested if example - using if/elif/else
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
score = input("Enter score: ")
score = int(score)
if score >= 80:
grade = 'A'
elif score >= 70:
grade = 'B'
elif score >= 55:
grade = 'C'
elif score >= 50:
grade = 'Pass'
else:
grade = 'Fail'
print ("\n\nGrade is: " + grade)
# File: 04-16.py
# Purpose: Demo of DeMorgan's Laws:
# 1. a Not And is equivalent to an Or with two negated inputs
# 2. a Not Or is equivalent to an And with two negated inputs
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# Test data: 0 0, 0 1, 1 0, 1 1
# For ***any*** value of x and y, (not(x < 15 and y >= 3)) == (x >= 15 or y < 3)
# Common uses of De Morgan's rules are in digital circuit design
# where it is used to manipulate the types of logic gates.
# Also, computer programmers use them to change a complicated statement
# like IF ... AND (... OR ...) THEN ... into its opposite (and shorter) equivalent.
# http://en.wikipedia.org/wiki/De_Morgan%27s_law
# https://www.annedawson.net/DeMorgansLaws.htm
x = int(input("Enter a value for x: "))
y = int(input("Enter a value for y: "))
print ((not(x < 15 and y >= 3)))
print ((x >= 15 or y < 3))
# Program: 04-17.py
# Purpose: Decision using two conditions linked with an and or an or
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
age = input("Enter your age: ")
age = int(age)
have_own_car = input("Do you own your own car (y/n): ")
if (age > 21) and (have_own_car == 'y'):
print ("You are over 21 years old and own your own car")
if (age > 21) and (have_own_car == 'n'):
print ("You are over 21 years old and you do NOT own your own car")
if (age == 21) and (have_own_car == 'y'):
print ("You are 21 years old and you own your own car")
if (age == 21) and (have_own_car == 'n'):
print ("You are 21 years old and you DO NOT own your own car" )
if (age < 21) and (have_own_car == 'y'):
print ("You are younger than 21 and you own your own car")
if (age < 21) and (have_own_car == 'n'):
print ("You are younger than 21 and you DO NOT own your own car" )
salary = float(input("Enter your annual salary, (e.g. 50000): "))
if (salary > 50000) or (age > 21):
print ("you can join our club because you earn more than $50000 OR you are over 21 (or both)")
else:
print ("you need to be earning more than 50000 OR be over 21 (or both) to join our club")
# File: 05-01.py
# Purpose: Examples of while loops
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# You must remember to indent the statements to be repeated.
# They must be repeated to the same level.
# Use the Tab key to indent. The space bar can be used but
# its easier (less typing) to use the space bar
# Used like this, the while loop is said to be
# 'counter-controlled'. In this program, x is acting as a counter.
x = 1
while x < 5:
print ('Hi spam')
x = x + 1
print ('done')
# File: 05-02.py
# Purpose: Examples of while loops
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# Used like this, the while loop is said to be
# 'counter-controlled'. In this program, x is acting as a counter.
# You may repeat one statement or multiple statements.
x = 1
while x < 5:
print ('Hi spam')
x = x + 1
print ('I love spam')
print ('done')
print ('gone')
# File: 05-03.py
# Purpose: Examples of while loops - the infinite loop
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# An infinite loop.
# Remember that 1 (or any value other than 0) represents true.
# Press Ctrl-C to interrupt this program run.
x = 1
while x:
print ('Hi spam')
x = x + 1
print ('I love spam')
print ('Press the Ctrl key and the C key together')
print ('to interrupt this program...')
print ('done')
print ('gone')
# File: 05-04.py
# Purpose: Examples of while loops - another infinite loop
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# An infinite loop.
# Remember that 1 (or any value other than 0) represents true.
# Press Ctrl-C to interrupt this program run.
while 1:
print ('Anyone for spam? ')
print ('Press the Ctrl key and the C key together')
print ('to interrupt this program...')
print ('done')
print ('gone')
# File: 05-05.py
# Purpose: Example: use of break to end an infinite loop
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
while 1:
print ('Spam')
answer = input('Press y to end this loop')
if answer == 'y':
print ('Fries with that?')
break
print ('Have a ')
print ('nice day!')
# File: 05-06.py
# Purpose: Example: use of continue in a loop
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
while 1:
print ('Spam')
answer = input('Press y for large fries ')
if answer == 'y':
print ('Large fries with spam, mmmm, yummy ')
continue
answer = input('Had enough yet? ')
if answer == 'y':
break
print ('Have a ')
print ('nice day!')
# File: 05-07.py
# Purpose: Example: 'sentinel-controlled' while loop
# Calculates average score of a class
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
# initialization phase
totalScore = 0 # sum of scores
numberScores = 0 # number of scores entered
# processing phase
score = input( "Enter score, (Enter -9 to end): " ) # get one score
score = int( score ) # convert string to an integer
while score != -9: # -9 is used as a sentinel ( a lookout or sentry value )
totalScore = totalScore + score
numberScores = numberScores + 1
score = input( "Enter score, (Enter -9 to end): " )
score = int( score )
# termination phase
if numberScores != 0: # division by zero would be a run-time error
average = float( totalScore ) / numberScores
print ("Class average is", average)
else:
print ("No scores were entered")
# File: 05-08.py
# Purpose: Example: the counter-controlled for loop
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
for c in range (10):
print (c)
# Note: range (10) is 0 through 9
# File: 05-09.py
# Purpose: Example: the counter-controlled for loop
# Programmer: Anne Dawson
# Date: Friday 17th February 2023, 5:15 PT, AD
for c in range (5,10):
print (c)
# Note: range (5,10) is 5 through 9