Conditional Statement

 

Conditional Statements

Certainly! Here's some material about Python conditional statements to help you understand the topic better:

Conditional Statements in Python

Conditional statements allow you to make decisions in your Python code. They help you execute specific code blocks based on whether a given condition is true or false. In Python, you primarily use the `if`, `elif`, and `else` statements to create conditional structures.

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 [4]:
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 [5]:
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 [7]:
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 [8]:
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 [9]:
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 [10]:
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 [11]:
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 [13]:
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 [14]:
['', '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 [15]:
['', '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']

Comments

Popular Posts