|
| 1 | +## 1. If Statements ## |
| 2 | + |
| 3 | +# INITIAL CODE |
| 4 | +opened_file = open('AppleStore.csv') |
| 5 | +from csv import reader |
| 6 | +read_file = reader(opened_file) |
| 7 | +apps_data = list(read_file) |
| 8 | + |
| 9 | +free_apps_ratings = [] |
| 10 | +for row in apps_data[1:]: |
| 11 | + rating = float(row[7]) |
| 12 | + # Complete the code from here |
| 13 | + price = float(row[4]) |
| 14 | + |
| 15 | + if price == 0.0: |
| 16 | + free_apps_ratings.append(rating) |
| 17 | + |
| 18 | +avg_rating_free = sum(free_apps_ratings) / len(free_apps_ratings) |
| 19 | + |
| 20 | +## 2. Booleans ## |
| 21 | + |
| 22 | +a_price = 0 |
| 23 | +if a_price == 0: |
| 24 | + print('This is free') |
| 25 | + |
| 26 | +if a_price == 1: |
| 27 | + print('This is not free') |
| 28 | + |
| 29 | +## 3. The Average Rating of Non-free Apps ## |
| 30 | + |
| 31 | +# INITIAL CODE |
| 32 | +opened_file = open('AppleStore.csv') |
| 33 | +from csv import reader |
| 34 | +read_file = reader(opened_file) |
| 35 | +apps_data = list(read_file) |
| 36 | + |
| 37 | +non_free_apps_ratings = [] |
| 38 | +for row in apps_data[1:]: |
| 39 | + rating = float(row[7]) |
| 40 | + price = float(row[4]) |
| 41 | + if price != 0.0: |
| 42 | + non_free_apps_ratings.append(rating) |
| 43 | + |
| 44 | +avg_rating_non_free = sum(non_free_apps_ratings) / len(non_free_apps_ratings) |
| 45 | + |
| 46 | +## 4. The Average Rating of Gaming Apps ## |
| 47 | + |
| 48 | +opened_file = open('AppleStore.csv') |
| 49 | +from csv import reader |
| 50 | +read_file = reader(opened_file) |
| 51 | +apps_data = list(read_file) |
| 52 | + |
| 53 | +non_games_ratings = [] |
| 54 | +for row in apps_data[1:]: |
| 55 | + rating = float(row[7]) |
| 56 | + genre = row[11] |
| 57 | + |
| 58 | + if genre != 'Games': |
| 59 | + non_games_ratings.append(rating) |
| 60 | + |
| 61 | +avg_rating_non_games = sum(non_games_ratings) / len(non_games_ratings) |
| 62 | + |
| 63 | +## 5. Multiple Conditions ## |
| 64 | + |
| 65 | +# INITIAL CODE |
| 66 | +opened_file = open('AppleStore.csv') |
| 67 | +from csv import reader |
| 68 | +read_file = reader(opened_file) |
| 69 | +apps_data = list(read_file) |
| 70 | + |
| 71 | +free_games_ratings = [] |
| 72 | +for row in apps_data[1:]: |
| 73 | + rating = float(row[7]) |
| 74 | + price = float(row[4]) |
| 75 | + genre = row[11] |
| 76 | + # Complete code from here |
| 77 | + |
| 78 | + if price == 0.0 and genre == 'Games': |
| 79 | + free_games_ratings.append(rating) |
| 80 | + |
| 81 | +avg_rating_free_games = sum(free_games_ratings) / len(free_games_ratings) |
| 82 | + |
| 83 | +## 6. The or Operator ## |
| 84 | + |
| 85 | +# INITIAL CODE |
| 86 | +opened_file = open('AppleStore.csv') |
| 87 | +from csv import reader |
| 88 | +read_file = reader(opened_file) |
| 89 | +apps_data = list(read_file) |
| 90 | + |
| 91 | +games_social_ratings = [] |
| 92 | +for row in apps_data[1:]: |
| 93 | + rating = float(row[7]) |
| 94 | + genre = row[11] |
| 95 | + # Complete code from here |
| 96 | +# SOLUTION CODE |
| 97 | +games_social_ratings = [] |
| 98 | +for row in apps_data[1:]: |
| 99 | + rating = float(row[7]) |
| 100 | + genre = row[11] |
| 101 | + if genre == 'Social Networking' or genre == 'Games': |
| 102 | + games_social_ratings.append(rating) |
| 103 | + |
| 104 | +avg_games_social = sum(games_social_ratings) / len(games_social_ratings) |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +## 7. Combining Logical Operators ## |
| 109 | + |
| 110 | +opened_file = open('AppleStore.csv') |
| 111 | +from csv import reader |
| 112 | +read_file = reader(opened_file) |
| 113 | +apps_data = list(read_file) |
| 114 | + |
| 115 | +free_games_social_ratings = [] |
| 116 | +for row in apps_data[1:]: |
| 117 | + rating = float(row[7]) |
| 118 | + genre = row[11] |
| 119 | + price = float(row[4]) |
| 120 | + |
| 121 | + if (genre == 'Social Networking' or genre == 'Games') and price == 0: |
| 122 | + free_games_social_ratings.append(rating) |
| 123 | + |
| 124 | +avg_free = sum(free_games_social_ratings) / len(free_games_social_ratings) |
| 125 | + |
| 126 | +# Non-free apps (average) |
| 127 | + |
| 128 | +non_free_games_social_ratings = [] |
| 129 | +for row in apps_data[1:]: |
| 130 | + rating = float(row[7]) |
| 131 | + genre = row[11] |
| 132 | + price = float(row[4]) |
| 133 | + |
| 134 | + if (genre == 'Social Networking' or genre == 'Games') and price != 0: |
| 135 | + non_free_games_social_ratings.append(rating) |
| 136 | + |
| 137 | +avg_non_free = sum(non_free_games_social_ratings) / len(non_free_games_social_ratings) |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +## 8. Comparison Operators ## |
| 143 | + |
| 144 | +opened_file = open('AppleStore.csv') |
| 145 | +from csv import reader |
| 146 | +read_file = reader(opened_file) |
| 147 | +apps_data = list(read_file) |
| 148 | + |
| 149 | +n_apps_more_9 = 0 |
| 150 | +n_apps_less_9 = 0 |
| 151 | +N_apps = [] |
| 152 | +for row in apps_data[1:]: |
| 153 | + rating = float(row[7]) |
| 154 | + price = float(row[4]) |
| 155 | + if price > 9.0: |
| 156 | + N_apps.append(rating) |
| 157 | + n_apps_more_9 = n_apps_more_9 + 1 |
| 158 | + |
| 159 | + if price <= 9.0: |
| 160 | + n_apps_less_9 = n_apps_less_9 + 1 |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +avg_rating = sum(N_apps) / len(N_apps) |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +## 9. The else Clause ## |
| 169 | + |
| 170 | +# INITIAL CODE |
| 171 | +opened_file = open('AppleStore.csv') |
| 172 | +from csv import reader |
| 173 | +read_file = reader(opened_file) |
| 174 | +apps_data = list(read_file) |
| 175 | + |
| 176 | +for app in apps_data[1:]: |
| 177 | + price = float(app[4]) |
| 178 | + # Complete code from here |
| 179 | + if price == 0.0: |
| 180 | + app.append('free') |
| 181 | + else: |
| 182 | + app.append('non-free') |
| 183 | + |
| 184 | +apps_data[0].append('free_or_not') |
| 185 | + |
| 186 | + |
| 187 | +print(apps_data[:6]) |
| 188 | + |
| 189 | + |
| 190 | +## 10. The elif Clause ## |
| 191 | + |
| 192 | +# INITIAL CODE |
| 193 | +opened_file = open('AppleStore.csv') |
| 194 | +from csv import reader |
| 195 | +read_file = reader(opened_file) |
| 196 | +apps_data = list(read_file) |
| 197 | + |
| 198 | +for app in apps_data[1:]: |
| 199 | + price = float(app[4]) |
| 200 | + # Complete code from here |
| 201 | + |
| 202 | + if price == 0: |
| 203 | + app.append('free') |
| 204 | + elif price > 0 and price < 20: |
| 205 | + app.append('affordable') |
| 206 | + elif price >= 20 and price < 50: |
| 207 | + app.append('expensive') |
| 208 | + elif price >= 50: |
| 209 | + app.append('very expensive') |
| 210 | + |
| 211 | +apps_data[0].append('price_label') |
| 212 | + |
| 213 | + |
0 commit comments