Lompat ke konten Lompat ke sidebar Lompat ke footer

Lists For Loops - Jupyter Notebook

Instructions

1. Store the second row ('Instagram', 0.0, 'USD', 2161558, 4.5) as a list in a variable named row_2.
Store the third row ('Clash of Clans', 0.0, 'USD', 2130805, 4.5) as a list in a variable named row_3.






2. In the code editor, you can already see the lists for the first three rows.

The fourth element in each list describes the number of ratings an app has received. Retrieve this fourth element from each list, and then find the average value of the retrieved numbers.

Assign the fourth element from the list row_1 to a variable named ratings_1. Don't forget that the indexing starts at 0.
Assign the fourth element from the list row_2 to a variable named ratings_2.
Assign the fourth element from the list row_3 to a variable named ratings_3.
Add the three numbers retrieved together and save the sum to a variable named total.
Divide the sum (now saved in the variable total) by 3 to get the average number of ratings for the first three rows. Assign the result to a variable named average.









3. The last element in each list shows the average rating of each application.

Retrieve the ratings for the first three rows, and then find the average value of all the ratings retrieved.

Assign the last element from the list row_1 to a variable named rating_1. Try to take advantage of negative indexing.
Assign the last element from the list row_2 to a variable named rating_2.
Assign the last element from the list row_3 to a variable named rating_3.
Add the three ratings together and save the sum to a variable named total_rating.
Divide the total by 3 to get the average rating. Assign the result to a variable named average_rating.









4. For Facebook, Instagram, and Pandora — Music & Radio, isolate the rating data in separate lists. Each list should contain the name of the app, the rating count, and the user rating. Don't forget that indexing starts at 0.

For Facebook, assign the list to a variable named fb_rating_data.
For Instagram, assign the list to a variable named insta_rating_data.
For Pandora — Music & Radio, assign the list to a variable named pandora_rating_data.
Compute the average user rating for Facebook, Instagram, and Pandora — Music & Radio using the data you stored in fb_rating_data, insta_rating_data, and pandora_rating_data.

You'll need to add the ratings together first, and then divide the total by the number of ratings. Assign the result to a variable named avg_rating. As a side note, we could calculate the average rating here a little bit better using the weighted mean — we'll learn about the weighted mean in the statistics courses.






5. Select the first four elements from row_1 using a list slicing syntax shortcut. Assign the output to a variable named first_4_fb.
Select the last three elements from row_1 using a list slicing syntax shortcut. Assign the output to a variable named last_3_fb.
From row_5, select the list slice ['USD', 1126879] using a list slicing syntax shortcut. Assign the output to a variable named pandora_3_4.













6. In the code editor, we've already stored the five rows as lists in separate variables. Group together the five lists in a list of lists. Assign the resulting list of lists to a variable named app_data_set.
Compute the average rating of the apps by retrieving the right data points from the app_data_set list of lists.
The rating is the last element of each row. You'll need to sum up the ratings and then divide by the number of ratings.
Assign the result to a variable named avg_rating.











7. Open the AppleStore.csv file and store it as list of lists.

Open the file using the open() command. Save the output to a variable named opened_file.
Read in the opened file using the reader() command (we've already imported reader() for you from the csv module). Save the output to a variable named read_file.
Transform the read-in file to a list of lists using the list() command. Save the list of lists to a variable named apps_data. Explore apps_data. You could:
Print its length using the len() command
Print the first row (the row describing column names)
Print the second and the third row (try to use list slicing here)







8. Use the new technique we've learned to print all the rows in the app_data_set list of lists. Essentially, you'll need to translate this pattern into Python syntax: for each list in the app_data_set variable, print that list.

Don't forget about indentation.






9. Compute the average app rating for the apps stored in the app_data_set variable.

Initialize a variable named rating_sum with a value of zero outside the loop body.
Loop (iterate) over the app_data_set list of lists. For each of the five iterations of the loop (for each row in app_data_set):
Extract the rating of the app and store it to a variable named rating. The rating is the last element of each row.
Add the value stored in rating to the current value of the rating_sum.
Outside the loop body, divide the rating sum (stored in rating_sum) by the number of ratings to get an average value. Store the result in a variable named avg_rating.











10. Compute the average app rating for all the 7,197 apps stored in the data set.

Initialize a variable named rating_sum with a value of zero.
Loop through the apps_data[1:] list of lists (make sure you don't include the header row). For each of the 7,197 iterations of the loop (for each row in apps_data[1:]):
Extract the rating of the app and store it to a variable named rating (the rating has the index number 7). Make sure you convert the rating value from a string to a float using the float() command.
Add the value stored in rating to the current value of the rating_sum.
Divide the rating sum (stored in rating_sum) by the number of ratings to get an average value. Store the result in a variable named avg_rating.







11. Using the new technique we've learned, compute the average app rating for all of the 7,197 apps stored in our data set.

Initialize an empty list named all_ratings.
Loop through the apps_data[1:] list of lists (make sure you don't include the header row). For each of the 7,197 iterations of the loop:
Extract the rating of the app and store it to a variable named rating (the rating has the index number 7). Make sure you convert the rating value from a string to a float.
Append the value stored in rating to the list all_ratings.
Compute the sum of all ratings using the sum() command.
Divide the sum of all ratings by the number of ratings, and assign the result to a variable named avg_rating.









Posting Komentar untuk "Lists For Loops - Jupyter Notebook"