Conditional Statements - BIG DATA
Certainly! Here's some material about Python conditional statements to help you understand the topic better:
- Conditional Statements in Python
1. The `if` Statement
The `if` statement is used to test a condition. If the condition is true, the code inside the `if` block is executed. If the condition is false, the code is skipped.
```python
if condition:
# Code to execute when the condition is true
```
Example:
```python
x = 10
if x > 5:
print("x is greater than 5")
```
2. The `elif` Statement
The `elif` (short for "else if") statement is used when you want to test multiple conditions. You can have multiple `elif` blocks to check for different conditions in order.
```python
if condition1:
# Code to execute when condition1 is true
elif condition2:
# Code to execute when condition2 is true
```
Example:
```python
x = 5
if x > 10:
print("x is greater than 10")
elif x < 5:
print("x is less than 5")
```
3. The `else` Statement
The `else` statement is used to specify a block of code to be executed if the condition in the `if` statement is false.
```python
if condition:
# Code to execute when the condition is true
else:
# Code to execute when the condition is false
```
Example:
```python
x = 7
if x > 10:
print("x is greater than 10")
else:
print("x is not greater than 10")
```
4. Nested Conditional Statements
You can nest conditional statements, meaning you can place one conditional statement inside another. This allows for more complex decision-making in your code.
Example:
```python
x = 7
if x > 5:
if x < 10:
print("x is between 5 and
10")
else:
print("x is not between 5
and 10")
else:
print("x is not greater than 5")
```
5. Logical Operators
Python provides logical operators such as `and`, `or`, and `not` that allow you to combine conditions to create more complex tests.
Example:
```python
x = 7
if x > 5 and x < 10:
print("x is between 5 and 10")
```
Conditional statements are fundamental to programming and are essential for controlling the flow of your code. They allow you to create dynamic and responsive programs that can adapt to different situations.
example:
1- If Conditional
Instructions
Complete the code in the editor to find the average rating
for free apps.
Inside the for loop:
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).
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.
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.
In [22]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
free_apps_ratings = []
for row in apps_data[1:]:
rating = float(row[8])
price = row[4]
if price ==
'0.0' or price == '0' or price == 'USD':
price =
0.0
free_apps_ratings.append(rating)
avg_rating_free = sum(free_apps_ratings) / len(free_apps_ratings)
print("Average rating for free apps:", avg_rating_free)
Average rating for free apps: 3.526955675976101
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:
If a_price is equal to 0, then print the string 'This is
free' (remember to use the == operator for equality).
If a_price is equal to 1, then print the string 'This is not free'.
In [13]:
a_price = 0
if a_price == 0:
print('This is
free')
elif a_price == 1:
print('This is not
free')
This is 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.
Change the name of the empty list from free_apps_ratings to
non_free_apps_ratings (the list we defined before the for loop).
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.
Change free_apps_ratings.append(rating) to make sure the ratings are appended
to the new list non_free_apps_ratings.
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.
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?
In [15]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
non_free_apps_ratings = []
for row in apps_data[1:]:
price = row[4] # The price is the fifth element in each
row
rating = float(row[7]) # The rating is the eighth element in each
row
# Check if the
price can be converted to a float
if price.replace('.',
'', 1).isdigit():
price = float(price)
else:
continue # Skip this row
# Only include
non-free apps (price is not equal to 0.0)
if price !=
0.0:
non_free_apps_ratings.append(rating)
# Check if there are non-free apps in the dataset
if len(non_free_apps_ratings) > 0:
# Calculate the
average rating of non-free apps
avg_rating_non_free
= sum(non_free_apps_ratings) / len(non_free_apps_ratings)
print("Average
rating of non-free apps:", avg_rating_non_free)
else:
print("There
are no non-free apps in the dataset.")
There are no non-free apps in the dataset.
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.
Initialize an empty list named non_games_ratings.
Loop through the apps_data list of lists (make sure you don't include the
header row). For each iteration of the loop:
Assign the rating of the app as a float to a variable named rating (the index
number of the rating column is 7).
Assign the genre of the app to a variable named genre (index number 11).
If the genre is not equal to 'Games', append the rating to the
non_games_ratings list.
Compute the average rating of non-gaming apps, and assign the result to a
variable named avg_rating_non_games.
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?
In [16]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
non_games_ratings = []
for row in apps_data[1:]:
rating = float(row[7]) # The rating is the eighth element in each
row
genre = row[11] # The genre is the twelfth element in each
row
if genre !=
'Games':
non_games_ratings.append(rating)
# Calculate the average rating of non-gaming apps
avg_rating_non_games = sum(non_games_ratings) /
len(non_games_ratings)
# Print the result
print("Average rating of non-gaming apps:", avg_rating_non_games)
Average rating of non-gaming apps: 460.3739057940809
5- Multiple Conditions
Instructions
Complete the code in the editor to compute the average
rating of free gaming apps.
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'.
Outside the for loop, compute the average rating of free gaming apps.
Assign the result to a variable named avg_rating_free_games.
In [24]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
free_games_ratings = []
for row in apps_data[1:]:
rating = float(row[7]) # The rating is the eighth element in each
row
price = row[4] # The price is the fifth element in each
row
genre = row[11] # The genre is the twelfth element in each
row
# Check if the
app is both free and a game
if price ==
'0' and genre == 'Games':
free_games_ratings.append(rating)
# Check if there are free gaming apps in the dataset
if len(free_games_ratings) > 0:
# Calculate the
average rating of free gaming apps
avg_rating_free_games
= sum(free_games_ratings) / len(free_games_ratings)
print("Average
rating of free gaming apps:", avg_rating_free_games)
else:
print("There
are no free gaming apps in the dataset.")
There are no free gaming apps in the dataset.
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."
Inside the for loop, append the rating to the
games_social_ratings list if the genre is either 'Social Networking' or
'Games'.
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.
In [26]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
games_social_ratings = []
for row in apps_data[1:]:
rating = float(row[7]) # The rating is the eighth element in each
row
genre = row[11] # The genre is the twelfth element in each
row
# Check if the
app's genre is either 'Social Networking' or 'Games'
if genre ==
'Social Networking' or genre == 'Games':
games_social_ratings.append(rating)
# Check if there are apps in the 'Social Networking' or
'Games' genre
if len(games_social_ratings) > 0:
# Calculate the
average rating of apps in the 'Social Networking' or 'Games' genre
avg_games_social =
sum(games_social_ratings) / len(games_social_ratings)
print("Average
rating of 'Social Networking' or 'Games' apps:", avg_games_social)
else:
print("There
are no apps in the 'Social Networking' or 'Games' genre in the dataset.")
There are no apps in the 'Social Networking' or 'Games'
genre in the dataset.
7- Combining Logical Operator
Instructions
Compute the average rating of non-free apps whose genre is
either "Social Networking" or "Games."
Assign the result to a variable named avg_non_free.
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.
In [28]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
free_games_social_ratings = []
non_free_games_social_ratings = []
for row in apps_data[1:]:
rating = float(row[8])
genre = row[12]
price = float(row[5])
if (genre ==
'Social Networking' or genre == 'Games') and price ==
0:
free_games_social_ratings.append(rating)
if (genre ==
'Social Networking' or genre == 'Games') and price !=
0:
non_free_games_social_ratings.append(rating)
avg_free = sum(free_games_social_ratings) / len(free_games_social_ratings)
# Non-free apps (average)
avg_non_free = sum(non_free_games_social_ratings) /
len(non_free_games_social_ratings)
# Print the result
print("Average rating of non-free 'Social Networking'
or 'Games' apps:", avg_non_free)
Average rating of non-free 'Social Networking' or 'Games'
apps: 3.8904235727440146
8- Comparation Operators
Instructions
Compute the average rating of the apps that have a price
greater than 9.
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.
Find the average value of these ratings and assign the result to a variable
named avg_rating.
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.
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.
In [30]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
ratings_above_9 = []
n_apps_more_9 = 0
n_apps_less_9 = 0
for row in apps_data[1:]:
price = row[4] # The price is the fifth element in each
row
rating = float(row[7]) # The rating is the eighth element in each
row
# Check if the
price can be converted to a float
if price.replace('.',
'', 1).isdigit():
price =
float(price)
else:
continue # Skip this row
if price >
9:
ratings_above_9.append(rating)
n_apps_more_9 +=
1
else:
n_apps_less_9 +=
1
# Check if there are any apps with prices greater than 9
if n_apps_more_9 > 0:
avg_rating =
sum(ratings_above_9) / len(ratings_above_9)
else:
avg_rating =
0.0 # Set to 0 if there are no apps
with prices greater than 9
# Print the results
print("Average rating of apps with price > 9:",
avg_rating)
print("Number of apps with price > 9:", n_apps_more_9)
print("Number of apps with price <= 9:", n_apps_less_9)
Average rating of apps with price > 9: 0.0
Number of apps with price > 9: 0
Number of apps with price <= 9: 0
9- The Else Clause
Instructions
Complete the code in the editor to label each app as
"free" or "non-free" depending on its price.
Inside the for loop:
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.
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'.
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.
Print the header row and the first five rows to see some of the changes we
made.
In [32]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
# Iterate through the apps_data list
for app in apps_data[1:]:
price = app[4] # The price is the fifth element in each
row
# Check if the
price can be converted to a float (is a valid number)
if price.replace('.',
'', 1).isdigit():
price =
float(price)
if price
== 0.0:
app.append('free')
else:
app.append('non-free')
else:
app.append('non-free') # If price cannot be converted, assume
it's non-free
# Add the "free_or_not" label to the header row
apps_data[0].append('free_or_not')
# Print the header row
print(apps_data[0])
# Print the first five rows to see the changes
for row in apps_data[1:6]:
print(row)
['', 'id', 'track_name', 'size_bytes', 'currency', 'price',
'rating_count_tot', 'rating_count_ver', 'user_rating', 'user_rating_ver',
'ver', 'cont_rating', 'prime_genre', 'sup_devices.num', 'ipadSc_urls.num',
'lang.num', 'vpp_lic', 'free_or_not']
['1', '281656475', 'PAC-MAN Premium', '100788224', 'USD',
'3.99', '21292', '26', '4', '4.5', '6.3.5', '4+', 'Games', '38', '5', '10',
'1', 'non-free']
['2', '281796108', 'Evernote - stay organized', '158578688',
'USD', '0', '161065', '26', '4', '3.5', '8.2.2', '4+', 'Productivity', '37',
'5', '23', '1', 'non-free']
['3', '281940292', 'WeatherBug - Local Weather, Radar, Maps,
Alerts', '100524032', 'USD', '0', '188583', '2822', '3.5', '4.5', '5.0.0',
'4+', 'Weather', '37', '5', '3', '1', 'non-free']
['4', '282614216', 'eBay: Best App to Buy, Sell, Save!
Online Shopping', '128512000', 'USD', '0', '262241', '649', '4', '4.5',
'5.10.0', '12+', 'Shopping', '37', '5', '9', '1', 'non-free']
['5', '282935706', 'Bible', '92774400', 'USD', '0',
'985920', '5320', '4.5', '5', '7.5.1', '4+', 'Reference', '37', '5', '45', '1',
'non-free']
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:
If the price of the app is 0, label the app as
"free" by appending the string 'free' to the current iteration
variable.
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.
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.
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.
Name the newly created column "price_label" by appending the string
'price_label' to the first row of the apps_data data set.
Inspect the header row and the first five rows to see some
of the changes you made.
In [34]:
opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
# Iterate through the apps_data list
for app in apps_data[1:]:
price = app[4] # The price is the fifth element in each
row
# Check if the
price can be converted to a float (is a valid number)
if price.replace('.',
'', 1).isdigit():
price =
float(price)
if price
== 0:
app.append('free')
elif 0 <
price < 20:
app.append('affordable')
elif 20
<= price < 50:
app.append('expensive')
else:
app.append('very
expensive')
else:
app.append('very
expensive') # If price cannot be
converted, assume it's 'very expensive'
# Add the "price_label" label to the header row
apps_data[0].append('price_label')
# Print the header row
print(apps_data[0])
# Print the first five rows to see the changes
for row in apps_data[1:6]:
print(row)
['', 'id', 'track_name', 'size_bytes', 'currency', 'price',
'rating_count_tot', 'rating_count_ver', 'user_rating', 'user_rating_ver',
'ver', 'cont_rating', 'prime_genre', 'sup_devices.num', 'ipadSc_urls.num',
'lang.num', 'vpp_lic', 'price_label']
['1', '281656475', 'PAC-MAN Premium', '100788224', 'USD',
'3.99', '21292', '26', '4', '4.5', '6.3.5', '4+', 'Games', '38', '5', '10',
'1', 'very expensive']
['2', '281796108', 'Evernote - stay organized', '158578688',
'USD', '0', '161065', '26', '4', '3.5', '8.2.2', '4+', 'Productivity', '37',
'5', '23', '1', 'very expensive']
['3', '281940292', 'WeatherBug - Local Weather, Radar, Maps,
Alerts', '100524032', 'USD', '0', '188583', '2822', '3.5', '4.5', '5.0.0',
'4+', 'Weather', '37', '5', '3', '1', 'very expensive']
['4', '282614216', 'eBay: Best App to Buy, Sell, Save!
Online Shopping', '128512000', 'USD', '0', '262241', '649', '4', '4.5',
'5.10.0', '12+', 'Shopping', '37', '5', '9', '1', 'very expensive']
['5', '282935706', 'Bible', '92774400', 'USD', '0',
'985920', '5320', '4.5', '5', '7.5.1', '4+', 'Reference', '37', '5', '45', '1',
'very expensive']
Komentar
Posting Komentar