-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythoncode.html
1676 lines (1195 loc) · 51.5 KB
/
pythoncode.html
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
<!--
Title: Python Code
by: Zane Dax
Date: May 17, 2021
Notes: This is organized old Python code from various projects and learnings dating back to 2016.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">
<title> Python Code Portfolio </title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{
background-color: #0b172e;
color: chartreuse;
font-family: 'Roboto Mono', monospace;
}
h1{
text-align: center;
}
h2{
margin-left: 8%;
}
p{
padding-left: 8%;
}
header{
margin: 10px;
color: aqua;
font-size: medium;
}
.collapsible {
font-family: 'Roboto Mono', monospace;
background-color: rgb(37, 51, 106);
color: chartreuse; /*#0b172e; */
cursor: pointer;
padding: 19px;
/* width: 100%; */
border: none;
text-align: left;
outline: none;
font-size: 15px;
margin: 10px;
margin-left: 80px;
width: 50%;
}
.active, .collapsible:hover {
background-color: #555;
}
.content {
padding: 0 18px;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: #0b172e;
}
</style>
</head>
<body>
<header>
<a href="./friend_bot/friend2.html"> friend chatbot </a>
</header>
<div class="container">
<h1> Python Portfolio </h1>
</div>
<h2> Collapsibles</h2>
<p> A Collapsible:</p>
<button class="collapsible"> Animated </button>
<div class="content">
<section>
<h2> Animation Dated © 2016 </h2>
<pre>
------------------
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 30 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')
WHITE = (255, 255, 255)
catImg = pygame.image.load('cat.png')
catx = 10
caty = 10
direction = 'right'
while True: # the main game loop
DISPLAYSURF.fill(WHITE)
if direction == 'right':
catx += 5
if catx == 280:
direction = 'down'
elif direction == 'down':
caty += 5
if caty == 220:
direction = 'left'
elif direction == 'left':
catx -= 5
if catx == 10:
direction = 'up'
elif direction == 'up':
caty -= 5
if caty == 10:
direction = 'right'
DISPLAYSURF.blit(catImg, (catx, caty))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)
</pre>
</section>
</div>
<button class="collapsible"> Text to Speech </button>
<div class="content">
<section>
<pre>
from .engine import pyttsx
from . import driver
#except Expception as e
engine = pyttsx.init()
engine.say(" good afternoon ")
engine.runAndWait()
</pre>
</section>
</div>
<button class="collapsible">Text to Speech (Mac) </button>
<div class="content">
<section>
<pre>
import os
os.system("echo 'hello world'")
os.system("say hello there, i am invisible text but you can hear me. \
I am your computer that is talking to you using the text that you typed in your awesome python code. \
the weather is super wonderful and is fine for my processors. I am your computer friend ")
</pre>
</section>
</div>
<button class="collapsible"> Class Object </button>
<div class="content">
<section>
<pre>
class Shape:
"""docstring for Shape"""
def __init__(self, x, y):
#super(Shape, self).__init__()
# a variable inside a class is an ATTRIBUTE
self.x = x
self.y = y
self.description = " this shape has not been described yet"
self.author = " no one is author of shape yet"
#---- a function() inside a class is called a METHOD
def area(self):
return self.x * self.y
def perimeter(self):
return 2 * self.x + 2 * self.y
def desribe(self, text):
self.description = text
def authorName(self, text):
self.author = text
def scaleSize(self, scale):
self.x = self.x * scale
self.y = self.y * scale
#--- class instance
rectangle = Shape(100,45)
#--- class instance
fat_rectangle = Shape(130,120)
print (" rectangle area : ",rectangle.area())
print (" rectangle perimeter : ",rectangle.perimeter())
print (" rectangle scale size: ",rectangle.scaleSize(0.5))
#------------------------------- CLASS INHERITANCE
''' create a new Class based on ther 'parent' class which allows parent method modifications
or have the same name as an attribute or method in parent Class '''
'''
class Square(Shape):
def __init__(self,x):
self.x = x
self.y = y
class DoubleSquare(Square):
def __init__(self,y):
self.x = 2 * y
self.y = y
def perimeter(self):
return 2 * self.x + 3 * self.y
</pre>
</section>
</div>
<button class="collapsible"> Number Conversion </button>
<div class="content">
<section>
<pre>
number= int(input(" enter a int: "))
print(" -- in hex: ", hex(number))
print(" -- in binary: ", bin(number))
print(" -- complex number: ", complex(number))
# complex number
n = 5
z = complex(n)
print(" complex # ",z)
print(" -- Octal: ", oct(number))
print("\n chr(87) = ", chr(87))
print(" ord('a') = ", ord('a'))
print(" divmod(15,2) = ", divmod(15,2))
</pre>
</section>
</div>
<button class="collapsible"> String Formatting </button>
<div class="content">
<section>
<pre>
#------------------------------------------------------------------ WORD SLICING
words =" list of words of rambling testing words for slicing"
print(" words sliced: ", words[::2], " << slices every other letter")
#----------------------------------------------- FORMATTING
print(" formatting: {0} spam".format('spicy'))
import string
wordy = "formatting method synthax is fun "
print(wordy.capitalize())
print(wordy.upper())
print(" wordy.isalpha() = ",wordy.isalpha())
print(" wordy.isnumeric() = ",wordy.isnumeric())
num_list = [1,2,3,4,5,6,7]
print(num_list)
print(num_list.reverse())
print(num_list.count(2))
#------------------------------------------ string methods
s = "this is a string"
print(s.capitalize() ) # no value in (), capitalizes first letter
print(s.upper() ) # all caps
print( s.replace('this','cheese') )
</pre>
</section>
</div>
<button class="collapsible"> While Loop </button>
<div class="content">
<section>
<pre>
# While loop
i = 2
while i < 22:
print(i)
if i == 17:
print(" -- break time ")
break
i += 1
</pre>
</section>
</div>
<button class="collapsible"> Credit Card Balance </button>
<div class="content">
<section>
<pre>
#---
creditCard_balance = 0
balance = 0
penalty = 0
#---
print("\n--------- interest rate on credit ---------\n")
INTEREST_RATE = float(input(" * Enter current interest rate (%): "))
interest_ =(INTEREST_RATE/100)
creditCard_balance= float(input(" * Enter card balance: "))
payment= float(input(" * Enter payment: "))
balance = (creditCard_balance - payment)
new_balance = (penalty+balance)
print(" - - - - - - - - -")
if (balance > 0):
penalty = (balance * interest_)
print(" -- ( interest rate = ", interest_,") -- ")
print(" balance = $", balance)
print(" new interest = ", penalty)
print("..... NEW balance is $", new_balance, ".....")
</pre>
</section>
</div>
<button class="collapsible"> Payroll Program </button>
<div class="content">
<section>
<pre>
#--------------------------- work hours & overtime
pay_rate =0
hours=0
print("\n------ $ ------ work wages & overtime ------- $ ---\n")
hours = float(input(" Enter # of work hours: "))
pay_rate = float(input(" Enter hourly wage ($) : "))
print(" - - - - - - - - - - \n")
if (hours > 40.0):
#--- OVERTIME -----
over_hours = (hours - 40.0)
OVERTIME = (1.5 * over_hours * pay_rate)
#---- REGULAR HOURS & PAY
regular_hours = (40.0 * pay_rate)
full_wage = (OVERTIME + regular_hours)
#---- savings of 20% shown
print(" [ 20% saving = $",savings_)
# overtime pay alone calculated & shown
print(" overtime $ = ", OVERTIME)
# overtime calculated with regular pay shown
print(" wages = $", full_wage)
else:
# less than 40 hours worked
regular_hours = (40.0 * pay_rate)
print(" regular wages = $", regular_hours,"\n")
#---- savings of 20% shown
savings_ = (regular_hours*.20)
print("\t [20% saving = $",savings_,"]")
#--- show money after saving 20%
pay_minus_savings = (regular_hours - savings_)
print("\t .... post saving = $",pay_minus_savings)
monthly_income = (regular_hours * 2)
yearly_salary = (monthly_income * 12)
print("\t -----------------------------")
print("\t | ..... monthly $",monthly_income," |")
print("\t | ..... yearly $", yearly_salary," |")
print("\t -----------------------------")
print("\n ")
</pre>
</section>
</div>
<button class="collapsible"> Leap Year Program </button>
<div class="content">
<section>
<pre>
# LEAP YEAR
year1 = int(input(" Enter 1 year: "))
if (year1 % 4) ==0:
if (year1 % 100) == 0:
if (year1 %400) ==0:
print(".... ",year1," = leap")
else:
print(" ",year1,"not a leap year")
else:
print("____ ",year1," = leap")
else:
print(" ", year1," is not leap - ")
</pre>
</section>
</div>
<button class="collapsible"> Get Calendar </button>
<div class="content">
<section>
<pre>
import calendar
year = 0
month = 0
year = int(input(" Enter year: "))
month = int(input(" Enter month # : "))
print(" --------------------")
print(calendar.month(year,month))
print(" --------------------")
</pre>
</section>
</div>
<button class="collapsible"> Swap Variables </button>
<div class="content">
<section>
<pre>
# SWAP VARIABLES
print("\n ----- variable swap ----- ")
x = input(" Enter value for x: ")
y = input(" Enter value for y: ")
# - temp & swap
temp = x
x = y
y = temp
print(" ... the value for x ",x, "swapped")
print(" ... the value for y ",y, "swapped")
</pre>
</section>
</div>
<button class="collapsible"> Odd or Even Number </button>
<div class="content">
<section>
<pre>
# ODD OR EVEN NUMBER CHECK
print("\n ----- odd or even ------")
number = int(input(" enter a integer: "))
if (number %2) ==0:
print(" ",number," is even ")
else:
print(" ",number, " is odd ")
</pre>
</section>
</div>
<button class="collapsible"> Is It Prime ?</button>
<div class="content">
<section>
<pre>
# IS PRIME
print("\n ------ IS PRIME OR NOT ------")
num = int(input(" enter integer: "))
if num > 1:
for i in range(2, num):
if (num % i) ==0:
print(" ",num," is not a prime number")
print(" ... ",i," * ", num//i, " = ",num)
break
else:
print(" ",num, " IS PRIME ")
else:
print(num," not prime ")
</pre>
</section>
</div>
<button class="collapsible"> Find Largest Number </button>
<div class="content">
<section>
<pre>
# FIND LARGEST NUMBER
print("\n ----- FIND LARGEST NUMBER -------")
num1 = float(input(" enter 1st number: "))
num2 = float(input(" enter 2nd number: "))
num3 = float(input(" enter 3rd number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print(" ... the largest number of (",num1,num2,num3,") is ",largest)
</pre>
</section>
</div>
<button class="collapsible"> Find Lowest Common Multiple </button>
<div class="content">
<section>
<pre>
# FIND LOWEST COMMON MULTIPLE
def L_C_M(x,y):
# choose greater number
if x > y:
greater = x
else:
greater = y
while (True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater +=1
return lcm
print("\n ---- LOWEST COMMON MULTIPLE ------")
num1 = int(input(" enter 1st number: "))
num2 = int(input(" enter 2nd number: "))
print(" ---- ")
print(" ... the LCM of",num1,"&",num2, "is = ", L_C_M(num1,num2))
</pre>
</section>
</div>
<button class="collapsible"> Highest Common Factor </button>
<div class="content">
<section>
<pre>
# HIGHEST COMMON FACTOR
def H_C_F(x,y):
# choose smallest number
if x > y:
smallest = y
else:
smallest = x
# loop
for i in range(1, smallest+1):
if((x % i ==0) and (y % i ==0)):
hcf = i
return hcf
print("\n ----- HIGHEST COMMON FACTOR ------")
num1 = int(input(" enter 1st num: "))
num2 = int(input(" enter 2nd num: "))
print("\t .... "," HCF = ", H_C_F(num1,num2))
</pre>
</section>
</div>
<button class="collapsible"> Fibonacci Sequence </button>
<div class="content">
<section>
<pre>
# FIBONACCI SEQUENCE
print("\n - - - - Fibonacci Sequence - - - -")
n_terms = int(input(" how many terms? : "))
num1 =0
num2 =1
count =0
# check number of terms
if n_terms <= 0:
print(" invalid number, positive integer only")
elif n_terms ==1:
print(" Fibo seq up to ",n_terms, num1)
else:
print(" Fibo seq to ", n_terms)
while count < n_terms:
print(" ",num1, end=',')
nth = num1 + num2
num1 = num2
num2 = nth
count +=1
print("\n")
</pre>
</section>
</div>
<button class="collapsible"> Armstrong Number </button>
<div class="content">
<section>
<pre>
# ARMSTRONG NUMBER
print("\n * * * * Armstrong Number * * * *")
num = int(input(" Enter a 3 digit number: "))
sum_ = 0
# finds the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum_ += digit ** 3
temp //= 10
if num == sum_:
print(" ",num,"is an Armstrong #")
else:
print(" ... ",num," not Armstrong number")
</pre>
</section>
</div>
<button class="collapsible"> Simple Calculator </button>
<div class="content">
<section>
<pre>
# SIMPLE CALCULATOR
#---------------- math functions
def add(x,y):
return x + y
def subtract(x,y):
return x - y
def multiply(x,y):
return x * y
def divide(x,y):
return x / y
#-----------------
#----------------- display
print("\n Select operation: ")
print(" 1. Add")
print(" 2. Subtract")
print(" 3. Multiply")
print(" 4. Divide")
#-------------------- user choice
choice = int(input(" Select one: 1/2/3/4 :"))
num1 = int(input(" Enter 1st number: "))
num2 = int(input(" Enter 2nd number: "))
if (choice ==1):
print(" ...",num1,"+",num2,"=",add(num1,num2))
elif (choice ==2):
print(" ...",num1,"-",num2,"=", subtract(num1,num2))
elif (choice ==3):
print(" ...",num1,"*",num2,"=", multiply(num1,num2))
elif (choice ==4):
print(" ...",num1,"/",num2,"=", divde(num1,num2))
else:
print(" invalud selection")
</pre>
</section>
</div>
<button class="collapsible"> Card Shuffle </button>
<div class="content">
<section>
<pre>
# CARD SHUFFLE
import itertools, random
print("\n ----------- deck shuffle ----------")
# make a deck of cards
deck = list(itertools.product(range(1,14), ['Spades','Hearts','Diamonds','Clubs']))
# shuffle the cards
random.shuffle(deck)
# draw 5 cards
print(" you got: ")
for i in range(5):
print(" ",deck[i][0],"of", deck[i][1])
print("------------------------------------ \n")
</pre>
</section>
</div>
<button class="collapsible"> Word Sort </button>
<div class="content">
<section>
<pre>
# WORD SORT in alphabetical order
usr_string = input(" enter a string: ")
# breakdown string into list of words
words = usr_string.split()
# then sort the words using sort()
words.sort()
print(" words sorted: ")
for word in words:
print(word, end=" ")
print("\n")
</pre>
</section>
</div>
<button class="collapsible"> Quadratic Equation </button>
<div class="content">
<section>
<pre>
# QUADRATIC EQUATION
import cmath
a = float(input(" enter value for a: "))
b = float(input(" enter value for b: "))
c = float(input(" enter value for c: "))
# calculate
d = (b ** 2) - (4 * a * c)
# solutions
solution1 = (-b - cmath.sqrt(d))/(2*a)
solution2 = (-b + cmath.sqrt(d))/(2*a)
print(" solution: ",solution1,"&", solution2)
</pre>
</section>
</div>
<button class="collapsible"> Recursive Search Graph </button>
<div class="content">
<section>
<pre>
#---- depth first search = recursive alogorithm
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start)
for next in graph[start] - visited:
dfs(graph, next, visited)
return visited
graph = {'0': set(['1', '2']),
'1': set(['0', '3', '4']),
'2': set(['0']),
'3': set(['1']),
'4': set(['2', '3'])}
dfs(graph, '0')
</pre>
</section>
</div>
<button class="collapsible"> Recursion function </button>
<div class="content">
<section>
<pre>
# recursion is a function that calls other functions
def main():
loop_number = int(input(" How many times for the loop? : "))
count =1
recursion(loop_number, count)
def recursion(loop_number,count):
if loop_number > 0:
print(" this is loop iteration ", count)
recursion(loop_number -1,count +1)
else:
print(" ---- the loop is now complete ----")
main()
</pre>
</section>
</div>
<button class="collapsible"> Nested For Loops (triangle & stars) </button>
<div class="content">
<section>
<pre>
# NESTED FOR LOOP
##----------- number triangle
for i in range(1,11):
for j in range(11 - i):
print(" ", end = " ")
for j in range(1,i):
print(j, end = " ")
for i in range(i, 0, -1):
print(i,end= " ")
print("\n")
#----------- stars
for i in range(1,11):
for j in range(11 - i):
print(" ", end = " ")
for j in range(1,i):
print("*", end = " ")
for i in range(i, 0, -1):
print("*",end= " ")
print("\n")
</pre>
</section>
</div>
<button class="collapsible"> Menu function </button>
<div class="content">
<section>
<pre>
# the menu function:
def menu(list, question):
for entry in list:
print(1 + list.index(entry),)
print(") " + entry)
return int(input(question)) - 1
items = ['plant', 'chair', 'door','trash can', 'window','box']
key_location =3
key_found = 0
loop =1
print(" here is items, 1 has the key ")
print(len(items),"things: ")
for x in items:
print (x)
print(" the door is locked, find key")
</pre>
</section>
</div>
<button class="collapsible"> Squaring numbers, list comprehension </button>
<div class="content">
<section>
<pre>
# squaring list of numbers, list comprehension
nums = [0,1,2,3,4]
square_it =[]
for x in nums:
square_it.append(x**2)
print square_it
# simpler code for above
nums = [2,3,4,5]
square_it = [x**2 for x in nums]
print square_it
</pre>
</section>
</div>
<button class="collapsible"> NumPy Arrays & Matrices </button>
<div class="content">
<section>
<pre>
#----------------------------------------------------------------- NUMPY
# NUMPY science computing for arrays
# numpy array is grid of values of same type non-negative integers
# dimension number = rank of array
# tuple of integers = shape of array
import numpy as np
array1 = np.array([1,2,3]) # rank 1 array
print(type(array1) )
print(array1.shape ) # prints out (3,)