-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions_ Fundamentals-315.py
212 lines (165 loc) · 4.65 KB
/
Functions_ Fundamentals-315.py
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
## 1. Functions ##
a_list = [4444, 8897, 6340, 9896, 4835, 4324, 10, 6445,
661, 1246, 1000, 7429, 1376, 8121, 647, 1280,
3993, 4881, 9500, 6701, 1199, 6251, 4432, 37]
sum_manual=0
for ele in a_list:
sum_manual+=ele
print(a_list)
## 2. Built-in Functions ##
ratings = ['4+', '4+', '4+', '9+', '12+', '12+', '17+', '17+']
content_ratings={}
for rating in ratings:
if rating in content_ratings:
content_ratings[rating] += 1
else:
content_ratings[rating] = 1
print(content_ratings)
## 3. Creating Our Own Functions ##
def square(a_number):
s_sum=a_number*a_number
return s_sum
squared_10=square(a_number=10)
squared_16=square(a_number=16)
## 4. The Structure of a Function ##
def add_10(number):
addition=number+10
return addition
add_30=add_10(number=30)
add_90=add_10(number=90)
## 5. Parameters and Arguments ##
def square(a_number):
sum=a_number*a_number
return a_number*a_number
squared_6=square(6)
squared_11=square(11)
## 6. Extract Values From Any Column ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
def extract(value):
emoty_list =[]
for row in apps_data[1:]:
index = row[value]
emoty_list.append(index)
return emoty_list
genres = extract(11)
## 7. Creating Frequency Tables ##
# CODE FROM THE PREVIOUS SCREEN
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
def extract(index):
column = []
for row in apps_data[1:]:
value = row[index]
column.append(value)
return column
genres = extract(11)
def freq_table(column):
edictionary={}
for row in column:
if row in edictionary:
edictionary[row]+=1
else:
edictionary[row]=1
return edictionary
genres_ft=freq_table(genres)
## 8. Writing a Single Function ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
def freq_table(index):
table={}
for row in apps_data[1:]:
number=row[index]
if number in table:
table[number]+=1
else:
table[number]=1
return table
ratings_ft=freq_table(7)
## 9. Reusability and Multiple Parameters ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
# INITIAL FUNCTION
def freq_table(index, data_set):
frequency_table = {}
for row in apps_data[1:]:
value = row[index]
if value in frequency_table:
frequency_table[value] += 1
else:
frequency_table[value] = 1
return frequency_table
ratings_ft=freq_table(index=7, data_set=apps_data)
## 10. Keyword and Positional Arguments ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
def freq_table(data_set, index):
frequency_table = {}
for row in data_set[1:]:
value = row[index]
if value in frequency_table:
frequency_table[value] += 1
else:
frequency_table[value] = 1
return frequency_table
content_ratings_ft=freq_table(apps_data, 10)
ratings_ft=freq_table(data_set=apps_data, index=7)
genres_ft=freq_table(index=11, data_set=apps_data)
## 11. Combining Functions ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
def extract(data_set, index):
column = []
for row in data_set[1:]:
value = row[index]
column.append(value)
return column
def find_sum(a_list):
a_sum = 0
for element in a_list:
a_sum += float(element)
return a_sum
def find_length(a_list):
length = 0
for element in a_list:
length += 1
return length
def mean(data_set, index):
column=extract(data_set, index)
mean=find_sum(column)/find_length(column)
return mean
avg_price=mean(data_set=apps_data, index=4)
## 12. Debugging Functions ##
def extract(data_set, index):
column = []
for row in data_set[1:]:
value = row[index]
column.append(value)
return column
def find_sum(a_list):
a_sum = 0
for element in a_list:
a_sum +=float(element)
return a_sum
def find_length(a_list):
length = 0
for element in a_list:
length+= 1
return length
def mean(data_set, index):
column = extract(data_set, index)
return float(find_sum(column)/find_length(column))
avg_price = float(mean(apps_data, 4))
avg_rating = mean(apps_data, 7)