-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic_Math.py
More file actions
177 lines (123 loc) · 3.55 KB
/
Copy pathBasic_Math.py
File metadata and controls
177 lines (123 loc) · 3.55 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
# Built in print() function used to output results on terminal
# Each learning point is contained within a function that describes the code
# The # and """" syntax is used for single and multiline comments explaining the code
# Basic Math Operators
# + addition
# - subtraction
# * multiplication
# / division (with decimals/float)
# // division (without decimals/integer)
# ** Exponents
# % modulo operator - returns the remainder after division.
# Addition
def Addition():
print(4 + 5)
def Add_Variables():
First_Number = 4
Second_Number = 20
print(First_Number + Second_Number)
def Add_Strings():
print("first" + " " + "second")
def Add_Variables_Containing_Strings():
Var1 = "First string"
Var2 = "Second String"
print(Var1 + " " + Var2)
def Add_Lists():
list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2)
def Add_Values_Returned_From_Function():
def get_num():
return 5
print(get_num() + 10)
def Add_And_Reassign():
Var1 = 20
print(Var1)
Var1 += 15
print(Var1)
# Subtraction
def Subtraction():
print(20 - 7)
print(20 - 100)
def Subtract_Floats():
print(3.5 - 7.5)
def subtract_variables():
Var1 = 26
Var2 = 12
print(Var1 - Var2)
def Subtract_And_Reassign():
Var1 = 10
print(Var1)
Var1 -= 5
print(Var1)
def Chaining_Subtraction():
print(10 - 5 - 2)
def Subtract_In_For_Loop():
"""Built in Range function has 3 arguments (Start, Stop, Step)
Step means would you like to count up or down 1 at
a time or 2 or more. The stop number will not be
returned, the number before it will. The start number
is returned."""
for i in range(10, -1, -1): # in a range from 10 to -1,decrementing from 10 for each number in that range print it
print(i)
def Subtract_In_While_Loop():
"""As long as Var1 is greater than or equal to
0, print Var1, subtract 1 and reassign that difference
to Var1"""
Var1 = 10
while Var1 >= 0:
print(Var1)
Var1 -= 1
def Subtract_With_Lists():
"""Print Var1 and then for each item in List1, subtract
it from Var1, reassign the difference to Var1"""
Var1 = 100
List1 = [10, 20, 10, 3, 6]
for number in List1:
print(Var1)
Var1 -= number
# Multiplication
def Multiplication():
print(5 * 5)
def Multiply_With_Float():
print(8.9 * 7)
def Multiply_Variables():
Var1 = 3
Var2 = 7
print(Var1 * Var2)
def Multiply_And_Reassign():
Var1 = 8
print(Var1)
Var1 *= 7
print(Var1)
def Multiply_Strings():
String = "This is a string "
print(String)
String *= 3
print(String)
print("*" * 30)
def Multiply_In_Lists():
List1 = [1, 2]
print(List1 * 6)
def Multiply_In_For_Loop():
"""Note how the stop argument is 21 instead of 20. I want to see the
product of 20 x 3, but the stop argument does not print so i must use
the next number up. Also notice how i used an f-string, look at Strings.py
for info on f-strings"""
for i in range(0, 21):
print(f"{i} x 3 is {i * 3}")
def Multiply_For_Loop_With_String():
for i in range(1, 10, 1):
print("*" * i)
def Multiply_In_While_Loop():
"""I want to multiply Var1 by 2, reassign that product to Var1 and print it,
however i do not want it to go higher than 10. I use a conditional `if`
statement and a loop control keywork `break` to prevent printing
a number higher than 10"""
Var1 = 1
while Var1 < 10:
Var1 *= 2
if Var1 > 10:
break
print(Var1)
# Division