Repository navigation
Expand file tree
/
Copy pathModule05_Lecture_Code.py
More file actions
232 lines (169 loc) · 6.98 KB
/
Copy pathModule05_Lecture_Code.py
File metadata and controls
232 lines (169 loc) · 6.98 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
'''
Module 5 Lecture Code
All about Functions
'''
# global variable
my_global = 5 # this variable can be called from any function in the module, unlike a local variable declared inside a function which can only be called from that function. Also, we cannot modify a global variable from inside a function unless we use the global keyword.**
# constants in Python are declared using all capital letters. Technically, Python does not have constants, but the convention is to declare a variable as a constant by using all capital letters.
PI = 3.14159 #
PHRASE = "Hello, World!" # This is a constant. The value of a constant cannot be changed once it is declared.
counter = 0
def random_num_generator():
rand_number = random.randint(0, 100) #generates a random number between 0 and 100
print(rand_number)
result = pow(2, 3)
print(result + my_global)
def playing_with_constants():
#global PI # NEVER EVER EVER DO THIS. This is a bad practice. It is better to declare the constant as a global variable at the top of the module.
#PI = 2.14
result = 0
print(result)
print(PI)
print(PHRASE)
print(PI)
print(9 * PI)
def local_constant():
#global PI
PI = 2.14
print(PI)
print(PHRASE)
PI = 3.14
print(PI)
def increment():
global counter
counter += 1
print(counter)
#This function accepts a single argument in its parameter field and adds that number with a declared integer
def addition(num_1):
#a = 4
x = 5
total = num_1 + x - my_global # The global variable my_global is used in the function now.
#multiplication(total)
return total
def subtraction():
a = 6
b = 10
total = a - b - my_global # The global variable my_global is used in the function now.
print(total)
def multiplication(c):
#a = 6
#b = 10
#total = a * b * c
d = int(input("Enter a number: "))
return c * d
def division():
a = 10
b = 6
total = a // b # The double forward slash is used to perform integer division. The result of the division is an integer. If you want the result of the division to be a float, you can use a single forward slash.
print(total)
def compute(received_data):
count = 0
#received_data.strip(" ")
#print(received_data)
for char in received_data:
if (not char.isspace()):
count += 1
length = len(received_data)
print(length)
return count
def sendMultipleBack(food, number):
first = food
second = number
return first, second
def gui():
operation = int(input("Enter 1. Addition 2. Subtraction 3. Multiplication 4. Division "))
num_of_characters = 0
if(operation == 1):
num = int(input("Enter a number from 1 - 10: "))
add_return = addition(4, num, 7)
print(add_return)
if(operation == 2):
subtraction()
if(operation == 3):
multiplication()
if(operation == 4):
division()
if(operation == 5):
fav_book = input("What is your favorite book? ")
num_of_characters = compute(fav_book)
print(f"The number of characters in the title of you book is: {num_of_characters}")
if(operation == 6):
result = sendMultipleBack("Orange", 13)
print(result)
print(*result)
# The forwared slash in the parameter field makes the parameters positional only
def positional_arguments(a, b, c):
x = a + 4
y = b + 5
z = c
j = a + x
print(x, y, z)
# The asterisk in the parameter field makes the parameters keyword only
def keyword_arguments(*, a, b, c):
print(a, b, c)
# only the values after the asterisk are keyword only
def keyword_in_middle(a, *, b, c):
print(a, b, c)
# passing a function as an argument to another function
def function_as_argument(func, *, x, y):
return func(x, y)
# chaging the order of the parameters in the function call
def changed_order(a, b, c):
print(a, b, c)
# The double asterisk in the parameter field makes the parameters keyword only
def keyword_arguments(**kwargs):
print(kwargs) # kwargs is a dictionary that holds the keyword arguments passed to the function. We will learn more about dictionaries in a later module.
# assigning values to the parameters in the function definition
def default_arguments(a = 1, b = 2, c = 3):
print(a, b, c)
change_global_without_keyword()
def change_global():
global my_global # The global keyword is used to modify the value of a global variable from inside a function.
my_global = 10
print(my_global)
def change_global_without_keyword():
my_global = 14
print(my_global)
def try_to_change_constant():
PI = 2.7 # This will work because Python does not support true constants.
print(PI)
if __name__ == "__main__":
#x = int(input("Enter a number: "))
#y = int(input("Enter another number: "))
#result = addition(x, y)
#def gui():
# print("hello world") Can do this, but alternate method is preferred.
#product = multiplication(result)
#print(product)
gui()
#playing_with_constants()
local_constant()
user_value = input("Enter a letter: ")
t = 99
positional_arguments(1, user_value, 3)
positional_arguments(1)
positional_arguments(1,2,3,4)
keyword_arguments(a = 1, b = 2, c = 3) # keyword arguments must be used when calling the function. A keyword argument is a name-value pair that you pass to a function.
#compute(7)
changed_order(c = 3, a = 1, b = 2)
keyword_arguments(name = "John", age = 25, city = "New York")
total = function_as_argument(addition, x=5, y=6)
print(total)
# changed the order of the arguments in the function call
default_arguments(5, 6, 7) #The default values are overridden by the values passed in the function call
keyword_in_middle(1, b = 2, c = 3)# The value of a is positional only, while the values of b and c are keyword only
#calling a function from print
print(addition(4, 5))
#calling a function from another function
addition(multiplication(5), 7) # The result of the multiplication function is passed as an argument to the first positional argument in the addition function. The numbe 7 is passed as the second positional argument in the addition function.
#The use of end parameter in the print function
global_result = change_global_without_keyword()
print(global_result)
try_to_change_constant()
change_global()
#try_to_change_constant()
# calling the random.randint function from an f-string
import random
print(f"The random number is: {random.randint(1, 10)}")
print("Hello", end = " ") # The end parameter is used to specify the character that will be printed at the end of the string. By default, the end parameter is set to "\n" which means that a new line will be printed at the end of the string. In this case, we are changing the end parameter to a space character.
increment()