TUGAS 3 (BIG DATA - conditional statements)

 conditional statements

Pada kesempatan kali ini, kita akan membahas beberapa konsep penting dalam pemrograman yang sering digunakan dalam analisis data dan pengambilan keputusan. Memahami istilah-istilah ini akan membantu Anda dalam menyelesaikan berbagai tantangan pemrograman yang akan kita hadapi. Mari kita lihat masing-masing istilah tersebut dan bagaimana mereka berperan dalam konteks pemrograman.

1. If Conditional

If Conditional adalah sebuah struktur dalam pemrograman yang digunakan untuk menjalankan suatu blok kode hanya jika suatu kondisi tertentu terpenuhi. Struktur ini memungkinkan program untuk membuat keputusan berdasarkan kondisi yang dievaluasi sebagai benar (True) atau salah (False). Jika kondisi dalam pernyataan "if" bernilai True, maka kode di dalam blok tersebut akan dieksekusi; jika False, maka kode tersebut akan dilewati.

2. Boolean

Boolean adalah tipe data yang hanya memiliki dua nilai: True (benar) dan False (salah). Tipe data ini sering digunakan dalam logika pemrograman untuk membuat keputusan. Dalam konteks pemrograman, Boolean sering digunakan dalam kondisi "if" dan dalam operasi logika seperti AND, OR, dan NOT.

3. The Average Rating of Non-Free Apps

The Average Rating of Non-Free Apps merujuk pada rata-rata penilaian yang diberikan kepada aplikasi yang tidak gratis (berbayar). Untuk menghitung rata-rata ini, kita mengumpulkan semua nilai rating dari aplikasi non-free dan membaginya dengan jumlah aplikasi tersebut. Ini memberikan gambaran tentang kualitas umum aplikasi berbayar dalam suatu dataset.

4. The Average Rating of Gaming Apps

The Average Rating of Gaming Apps adalah rata-rata penilaian untuk aplikasi yang termasuk dalam kategori permainan (gaming). Sama seperti dengan aplikasi non-free, kita menghitung rata-rata ini dengan menjumlahkan semua nilai rating dari aplikasi yang dikategorikan sebagai permainan dan membaginya dengan jumlah aplikasi tersebut. Ini membantu kita memahami bagaimana pengguna menilai kualitas aplikasi game.

5. Multiple Conditions

Multiple Conditions merujuk pada penggunaan lebih dari satu kondisi dalam suatu pernyataan "if". Dalam pemrograman, kita dapat menggabungkan beberapa kondisi dengan menggunakan operator logika seperti AND dan OR untuk membuat keputusan yang lebih kompleks. Dengan ini, kita dapat mengevaluasi beberapa kriteria sekaligus sebelum mengambil tindakan tertentu.

6. Combining Logical Operator

Combining Logical Operator adalah proses menggabungkan beberapa operator logika, seperti AND dan OR, untuk membentuk kondisi yang lebih kompleks. Hal ini memungkinkan pemrogram untuk memeriksa berbagai kondisi secara bersamaan. Misalnya, kita bisa memeriksa apakah suatu aplikasi adalah gratis dan juga merupakan aplikasi game dalam satu pernyataan "if", sehingga membuat pengambilan keputusan lebih efisien.

7. Comparison Operation

Comparison Operation adalah serangkaian operasi yang digunakan untuk membandingkan dua nilai. Dalam pemrograman, kita menggunakan operator perbandingan seperti == (sama dengan), != (tidak sama dengan), > (lebih besar dari), < (lebih kecil dari), >= (lebih besar atau sama dengan), dan <= (lebih kecil atau sama dengan). Hasil dari operasi ini adalah nilai Boolean (True atau False) yang dapat digunakan dalam pernyataan "if" untuk menentukan alur eksekusi kode.

Berikut adalah contoh soal tentang materi diatas:

1. If Conditional

Instructions

Complete the code in the editor to find the average rating for free apps.<br>
Inside the for loop:<br>
Assign the price of an app as a float to a variable named price. The price
is the fifth element in each row (don't forget that the index starts at 0).<br>
If price == 0.0, append the value stored in rating to the free_apps_ratings
list using the list_name.append() command (note the free_apps_ratings is
already defined in the code editor). Be careful with indentation.<br>
Outside the for loop body, compute the average rating of free apps.
Assign the result to a variable named avg_rating_free. The ratings are stored in the free_apps_ratings list.

JAWABAN
free_apps_ratings = []
for row in apps_data[1:]:
    price = float(row[4])
    rating = float(row[7])
    if price == 0.0:
        free_apps_ratings.append(rating)
if len(free_apps_ratings) > 0:
    avg_rating_free = sum(free_apps_ratings) / len(free_apps_ratings)
else:
    avg_rating_free = 0  
print("Average rating for free apps:", avg_rating_free)

2. Booleans

Instructions

In the code editor, we've already initialized the variable a_price with a value of 0. Transcribe the following sentences into code by making use of if statements:<br>
If a_price is equal to 0, then print the string 'This is free' (remember to use the == operator for equality).<br>
If a_price is equal to 1, then print the string 'This is not free'.

JAWABAN
a_price = 0
if a_price == 0:
    print('This is free')
if a_price == 1:
    print('This is not free')

3. The Average Rating of Non-free Apps

Instructions

Modify the existing code in the editor on the right to compute the average rating of non-free apps.<br>

Change the name of the empty list from free_apps_ratings to non_free_apps_ratings (the list we defined
before the for loop).<br>
Change the condition if price == 0.0 to account for the fact that we now want to isolate only the ratings
of non-free apps.<br>
Change free_apps_ratings.append(rating) to make sure the ratings are appended to the new list non_free_apps_ratings.<br>
Compute the average value by summing up the values in non_free_apps_ratings
and dividing by the length of this list. Assign the result to avg_rating_non_free.<br>

Optional exercise: Inspect the value of avg_rating_non_free and compare
the average with that of free apps (the average rating of free apps is approximately
3.38 — we computed it in the first screen). Can we use the average values to say
that free apps are better than non-free apps, or vice versa?

JAWABAN
non_free_apps_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])  
    price = float(row[4])  
    if price != 0.0:  
        non_free_apps_ratings.append(rating)
avg_rating_non_free = sum(non_free_apps_ratings) / len(non_free_apps_ratings)
print(avg_rating_non_free)

4. The Average Rating of Gaming Apps

Instructions

Following the same techniques we used in the diagram above, compute the average rating of non-gaming apps.<br>
Initialize an empty list named non_games_ratings.<br>
Loop through the apps_data list of lists (make sure you don't include the header row). For each iteration of the loop:<br>
Assign the rating of the app as a float to a variable named rating (the index number of the rating column is 7).<br>
Assign the genre of the app to a variable named genre (index number 11).<br>
If the genre is not equal to 'Games', append the rating to the non_games_ratings list.<br>
Compute the average rating of non-gaming apps, and assign the result to a
variable named avg_rating_non_games.<br>

Optional exercise: Compare the average rating of gaming apps (3.69)
with that of non-gaming apps. Why do you think we see this difference?

JAWABAN
non_games_ratings = []

for row in apps_data[1:]:
    rating = float(row[7])
    genre = row[11]    

    if genre != 'Games':  
        non_games_ratings.append(rating)

avg_rating_non_games = sum(non_games_ratings) / len(non_games_ratings)

print(avg_rating_non_games)

5. Multiple Conditions

Instructions

Complete the code in the editor to compute the average rating of free gaming
apps.<br>
Inside the for loop, append the rating to the free_games_ratings list if
the price is equal to 0.0 and the genre is equal to 'Games'.<br>
Outside the for loop, compute the average rating of free gaming apps.
Assign the result to a variable named avg_rating_free_games.

JAWABAN
free_games_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])  
    price = float(row[5])  
    genre = row[11]      
    if price == 0.0 and genre == 'Games':  
        free_games_ratings.append(rating)
avg_rating_free_games = sum(free_games_ratings) / len(free_games_ratings)
print(avg_rating_free_games)

6. The Or Operator

Instructions

Complete the code in the editor to compute the average rating of the apps
whose genre is either "Social Networking" or "Games."<br>
Inside the for loop, append the rating to the games_social_ratings
list if the genre is either 'Social Networking' or 'Games'.<br>
Outside the for loop, compute the average rating of the apps whose
genre is either "Social Networking" or "Games," and assign the result
to a variable named avg_games_social.

JAWABAN
games_social_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])  
    genre = row[11]        
    if genre == 'Social Networking' or genre == 'Games':
        games_social_ratings.append(rating)
avg_games_social = sum(games_social_ratings) / len(games_social_ratings)
print(avg_games_social)

7. Combining Logical Operator

Instructions

Compute the average rating of non-free apps whose genre is either "Social Networking" or "Games."<br>
Assign the result to a variable named avg_non_free.<br>
We'll try to solve this exercise without any guidance. You may feel a bit stumped at first, but we've
practiced the steps needed to solve this kind of exercise several times. Essentially, the code is
almost identical to what we used to extract the ratings for free gaming or social networking apps.

JAWABAN
non_free_games_social_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])  
    genre = row[11]        
    price = float(row[4])    
    if (genre == 'Social Networking' or genre == 'Games') and price != 0.0:
        non_free_games_social_ratings.append(rating)
if non_free_games_social_ratings:  
    avg_non_free = sum(non_free_games_social_ratings) / len(non_free_games_social_ratings)
else:
    avg_non_free = 0  
print(avg_non_free)

8. Comparation Operators

Instructions

Compute the average rating of the apps that have a price greater than 9.<br>

Using a for loop, isolate the ratings of all the apps that have a price greater
than 9. When you iterate over apps_data, make sure you don't include the header row.<br>
Find the average value of these ratings and assign the result to a variable
named avg_rating.<br>
Find out how many apps have a price greater than 9 and assign the result to a variable
named n_apps_more_9. You can use the list of ratings from the previous question
to find the answer.<br>
Find out how many apps have a price less than or equal to 9 and assign the result
to a variable named n_apps_less_9. The list of ratings from the first
question can help you find a quick answer.

JAWABAN
sum_ratings = 0
n_apps_more_9 = 0  
for row in apps_data[1:]:
    price = float(row[4])  
    rating = float(row[7])  
   
    if price > 9:
        sum_ratings += rating
        n_apps_more_9 += 1
if n_apps_more_9 > 0:  
    avg_rating = sum_ratings / n_apps_more_9
else:
    avg_rating = 0  
n_apps_less_9 = len(apps_data) - 1 - n_apps_more_9  
print(f"Average rating of apps with price greater than 9: {avg_rating}")
print(f"Number of apps with price greater than 9: {n_apps_more_9}")
print(f"Number of apps with price less than or equal to 9: {n_apps_less_9}")

9. The Else Clause

Instructions

Complete the code in the editor to label each app as "free" or "non-free" depending on its price.<br>
Inside the for loop:<br>
If the price of the app is 0.0, then label the app as "free" by appending the string 'free'
to the current iteration variable.<br>
Else, label the app "non-free" by appending the string 'non-free' to the current iteration variable.
Make sure you don't write 'non_free' instead of 'non-free'.<br>
By adding labels to the end of each row, we basically created a new column. Name this column "free_or_not"
by appending the string 'free_or_not' to the first row of the apps_data data set.
Make sure this is done outside the for loop.<br>
Print the header row and the first five rows to see some of the changes we made.

JAWABAN

apps_data[0].append('free_or_not')  
for app in apps_data[1:]:
    price = float(app[4])
    if price == 0.0:
        app.append('free')
    else:
        app.append('non-free')  
for row in apps_data[:6]:  
    print(row)

10. The Elif Clause

Instructions

Complete the code in the editor to label each app as "free," "affordable," "expensive," or "very expensive." Inside the loop:<br>
If the price of the app is 0, label the app as "free" by appending the string 'free' to the current iteration variable.<br>
If the price of the app is greater than 0 and less than 20, label the app as "affordable". For efficiency purposes, use an elif clause.<br>
If the price of the app is greater or equal to 20 and less than 50, label the app as "expensive". For efficiency purposes, use an elif clause.<br>
If the price of the app is greater or equal to 50, label the app as "very expensive". For efficiency purposes, use an elif clause.<br>
Name the newly created column "price_label" by appending the string 'price_label' to the first row of the apps_data data set.<br>
Inspect the header row and the first five rows to see some of the changes you made.

JAWABAN

apps_data[0].append('price_label')
for app in apps_data[1:]:
    price = float(app[4])
    if price == 0.0:
        app.append('free')
    elif price > 0.0 and price < 20:
        app.append('affordable')  
    elif price >= 20 and price < 50:
        app.append('expensive')  
    elif price >= 50:
        app.append('very expensive')  
for row in apps_data[:6]:
    print(row)



Komentar

Postingan populer dari blog ini

Local File Inclusion (LFI) & Remote File Inclusion (RFI) - Sabilla Ardani Putri

TIDAK TERPAKAI - Footprinting dan Reconnaissance menggunakan Whois, Web History, Google Dork, GHDB

Manajemen Data Master - Sabilla Ardani Putri