adilsyed0011/python-practice-assignment - Jovian
Learn data science and machine learning by building real-world projects on Jovian

Assignment 1 - Python Basics Practice

This assignment is a part of the course "Data Analysis with Python: Zero to Pandas"

In this assignment, you'll get to practice some of the concepts and skills covered in the following notebooks:

  1. First Steps with Python and Jupyter
  2. A Quick Tour of Variables and Data Types
  3. Branching using Conditional Statements and Loops

As you go through this notebook, you will find the symbol ??? in certain places. To complete this assignment, you must replace all the ??? with appropriate values, expressions or statements to ensure that the notebook runs properly end-to-end.

Guidelines

  1. Make sure to run all the code cells, otherwise you may get errors like NameError for undefined variables.
  2. Do not change variable names, delete cells or disturb other existing code. It may cause problems during evaluation.
  3. In some cases, you may need to add some code cells or new statements before or after the line of code containing the ???.
  4. Since you'll be using a free online service for code execution, save your work by running jovian.commit at regular intervals.
  5. Questions marked (Optional) will not be considered for evaluation, and can be skipped. They are for your learning.
  6. If you are stuck, you can ask for help on the course forum. Post errors, ask for hints and help others, but please don't share the full solution answer code to give others a chance to write the code themselves.
  7. After submission your code will be tested with some hidden test cases. Make sure to test your code exhaustively to cover all edge cases.

Important Links:

How to Run the Code and Save Your Work

Option 1: Running using free online resources (1-click, recommended): Click the Run button at the top of this page and select Run on Binder. You can also select "Run on Colab" or "Run on Kaggle", but you'll need to create an account on Google Colab or Kaggle to use these platforms.

Option 2: Running on your computer locally: To run the code on your computer locally, you'll need to set up Python & Conda, download the notebook and install the required libraries. Click the Run button at the top of this page, select the Run Locally option, and follow the instructions.

Saving your work: You can save a snapshot of the assignment to your Jovian profile, so that you can access it later and continue your work. Keep saving your work by running jovian.commit from time to time.

# Install the library
!pip install jovian --upgrade --quiet
# Import it
import jovian
project_name='python-practice-assignment'
# Capture and upload a snapshot
jovian.commit(project=project_name, privacy='secret', evironment=None)
[jovian] Updating notebook "adilsyed0011/python-practice-assignment" on https://jovian.ai [jovian] Committed successfully! https://jovian.ai/adilsyed0011/python-practice-assignment

You'll be asked to provide an API Key, to securely upload the notebook to your Jovian.ml account. You can get the API key from your Jovian.ml profile page after logging in / signing up. See the docs for details: https://jovian.ml/docs/user-guide/upload.html . The privacy of your assignment notebook is set to Secret, so that you can the evlauators can access it, but it will not shown on your public profile to other users.

Problem 1 - Variables and Data Types

Q1: Assign your name to the variable name.

name = 'adil'

Q2: Assign your age (real or fake) to the variable age.

age = 20

Q3: Assign a boolean value to the variable has_android_phone.

has_android_phone = True

You can check the values of these variables by running the next cell.

name
'adil'

Q4: Create a dictionary person with keys "Name", "Age", "HasAndroidPhone" and values using the variables defined above.

#person = {'Name':'adil','Age':20,'HasAndroidPhone':True}
person=dict(Name="adil",Age=20,HasAndroidPhone=True)
person
{'Name': 'adil', 'Age': 20, 'HasAndroidPhone': True}

Let's use the person dictionary to print a nice message.

print("{} is aged {}, and owns an {}.".format(
    person["Name"], 
    person["Age"], 
    "Android phone" if person["HasAndroidPhone"] else "NOMOBILE"
))
adil is aged 20, and owns an Android phone.

Q4b (Optional): Use a for loop to display the type of each value stored against each key in person.

Here's the expected output for the key "Name":

The key "Name" has the value "Derek" of the type "<class 'str'>"
# this is optional
for key in person.keys():
    print("the key {} has the value {} of type {}".format(key,person[key],type(key)))
the key Name has the value adil of type <class 'str'> the key Age has the value 20 of type <class 'str'> the key HasAndroidPhone has the value True of type <class 'str'>

Now that you've solved one problem, it would be a good idea to record a snapshot of your notebook.

jovian.commit(project=project_name,environment=None)

Problem 2 - Working with Lists

Q5: Create a list containing the following 3 elements:

  • your favorite color
  • the number of pets you have
  • a boolean value describing whether you have previous programming experience
my_list = ['maroon',12,True]

Let's see what the list looks like:

my_list
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_36/376848641.py in <module> ----> 1 my_list NameError: name 'my_list' is not defined

Q6: Complete the following print and if statements by accessing the appropriate elements from my_list.

Hint: Use the list indexing notation [].

print('My favorite color is', my_list[0])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_36/1643188729.py in <module> ----> 1 print('My favorite color is', my_list[0]) NameError: name 'my_list' is not defined
print('I have {} pet(s).'.format(???))
if ???:
    print("I have previous programming experience")
else:
    print("I do not have previous programming experience")

Q7: Add your favorite single digit number to the end of the list using the appropriate list method.

my_list.append(17)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_36/1267718384.py in <module> ----> 1 my_list.append(17) NameError: name 'my_list' is not defined

Let's see if the number shows up in the list.

my_list

Q8: Remove the first element of the list, using the appropriate list method.

Hint: Check out methods of list here: https://www.w3schools.com/python/python_ref_list.asp

my_list.???
my_list

Q9: Complete the print statement below to display the number of elements in my_list.

print("The list has {} elements.".format(???))

Well done, you're making good progress! Save your work before continuing

jovian.commit(project=project_name,environment=None)

Problem 3 - Conditions and loops

Q10: Calculate and display the sum of all the numbers divisible by 7 between 18 and 534 i.e. 21+28+35+...+525+532.

Hint: One way to do this is to loop over a range using for and use an if statement inside it.

# store the final answer in this variable
sum_of_numbers = 0

# perform the calculation here
???
print('The sum of all the numbers divisible by 7 between 18 and 534 is', sum_of_numbers)

If you are not able to figure out the solution to this problem, you can ask for hints on the community forum: https://jovian.ml/forum/t/assignment-1-python-practice/7761 . Remember to save your work before moving forward.

jovian.commit(project=project_name,environment=None)

Problem 4 - Flying to the Bahamas

Q11: A travel company wants to fly a plane to the Bahamas. Flying the plane costs 5000 dollars. So far, 29 people have signed up for the trip. If the company charges 200 dollars per ticket, what is the profit made by the company?

Fill in values or arithmetic expressions for the variables below.

cost_of_flying_plane = 5000
number_of_passengers = 29
price_of_ticket = 200
profit = (29*200)-5000
print('The company makes of a profit of {} dollars'.format(profit))
The company makes of a profit of 800 dollars

Q11b (Optional): Out of the 29 people who took the flight, only 12 buy tickets to return from the Bahamas on the same plane. If the flying the plane back also costs 5000 dollars, and does the company make an overall profit or loss? The company charges the same fee of 200 dollars per ticket for the return flight.

Use an if statement to display the result.

# this is optional
loss=(12*200)-5000
profit+=loss
profit
-1800
# this is optional
if profit>0:
    print("The company makes an overall profit of {} dollars".format(profit))
else:
    print("The company makes an overall loss of {} dollars".format(profit))
The company makes an overall loss of -1800 dollars

Great work so far! Want to take a break? Remember to save and upload your notebook to record your progress.

jovian.commit(project=project_name,environment=None)

Problem 5 - Twitter Sentiment Analysis

Are your ready to perform some Data Analysis with Python? In this problem, we'll analyze some fictional tweets and find out whether the overall sentiment of Twitter users is happy or sad. This is a simplified version of an important real world problem called sentiment analysis.

Before we begin, we need a list of tweets to analyze. We're picking a small number of tweets here, but the exact same analysis can also be done for thousands, or even millions of tweets. The collection of data that we perform analysis on is often called a dataset.

tweets = [
    "Wow, what a great day today!! #sunshine",
    "I feel sad about the things going on around us. #covid19",
    "I'm really excited to learn Python with @JovianML #zerotopandas",
    "This is a really nice song. #linkinpark",
    "The python programming language is useful for data science",
    "Why do bad things happen to me?",
    "Apple announces the release of the new iPhone 12. Fans are excited.",
    "Spent my day with family!! #happy",
    "Check out my blog post on common string operations in Python. #zerotopandas",
    "Freecodecamp has great coding tutorials. #skillup"
]

Let's begin by answering a very simple but important question about our dataset.

Q12: How many tweets does the dataset contain?

number_of_tweets = 10

Let's create two lists of words: happy_words and sad_words. We will use these to check if a tweet is happy or sad.

happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']

To identify whether a tweet is happy, we can simply check if contains any of the words from happy_words. Here's an example:

sample_tweet = tweets[0]
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_36/3264440851.py in <module> ----> 1 sample_tweet = tweets[0] NameError: name 'tweets' is not defined
sample_tweet
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_36/482269483.py in <module> ----> 1 sample_tweet NameError: name 'sample_tweet' is not defined
is_tweet_happy = False

# Get a word from happy_words
for word in happy_words:
    # Check if the tweet contains the word
    if word in sample_tweet:
        # Word found! Mark the tweet as happy
        is_tweet_happy = True
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_36/2430347479.py in <module> 4 for word in happy_words: 5 # Check if the tweet contains the word ----> 6 if word in sample_tweet: 7 # Word found! Mark the tweet as happy 8 is_tweet_happy = True NameError: name 'sample_tweet' is not defined

Do you understand what we're doing above?

For each word in the list of happy words, we check if is a part of the selected tweet. If the word is indded a part of the tweet, we set the variable is_tweet_happy to True.

is_tweet_happy

Q13: Determine the number of tweets in the dataset that can be classified as happy.

Hint: You'll need to use a loop inside another loop to do this. Use the code from the example shown above.

# store the final answer in this variable
number_of_happy_tweets = 0

# perform the calculations here
for i in happy_words:
    if i in tweets:
        number_of_happy_tweets+=1
print("Number of happy tweets:", number_of_happy_tweets)
Number of happy tweets: 0

If you are not able to figure out the solution to this problem, you can ask for hints on the community forum: https://jovian.ml/forum/t/assignment-1-python-practice/7761 . Also try adding print statements inside your loops to inspect variables and make sure your logic is correct.

Q14: What fraction of the total number of tweets are happy?

For example, if 2 out of 10 tweets are happy, then the answer is 2/10 i.e. 0.2.

happy_fraction = ???
print("The fraction of happy tweets is:", happy_fraction)

To identify whether a tweet is sad, we can simply check if contains any of the words from sad_words.

Q15: Determine the number of tweets in the dataset that can be classified as sad.

# store the final answer in this variable
number_of_sad_tweets = 0

# perform the calculations here
???
print("Number of sad tweets:", number_of_sad_tweets)

Q16: What fraction of the total number of tweets are sad?

sad_fraction = ???
print("The fraction of sad tweets is:", sad_fraction)

The rest of this problem is optional. Let's save your work before continuing.

jovian.commit(project=project_name,environment=None)

Great work, even with some basic analysis, we already know a lot about the sentiment of the tweets given to us. Let us now define a metric called "sentiment score", to summarize the overall sentiment of the tweets.

Q16b (Optional): Calculate the sentiment score, which is defined as the difference betweek the fraction of happy tweets and the fraction of sad tweets.

sentiment_score = ???
print("The sentiment score for the given tweets is", sentiment_score)

In a real world scenario, we could calculate & record the sentiment score for all the tweets sent out every day. This information can be used to plot a graph and study the trends in the changing sentiment of the world. The following graph was creating using the Python data visualization library matplotlib, which we'll cover later in the course.

What does the sentiment score represent? Based on the value of the sentiment score, can you identify if the overall sentiment of the dataset is happy or sad?

Q16c (Optional): Display whether the overall sentiment of the given dataset of tweets is happy or sad, using the sentiment score.

if ???:
    print("The overall sentiment is happy")
else:
    print("The overall sentiment is sad")

Finally, it's also important to track how many tweets are neutral i.e. neither happy nor sad. If a large fraction of tweets are marked neutral, maybe we need to improve our lists of happy and sad words.

Q16d (Optional): What is the fraction of tweets that are neutral i.e. neither happy nor sad.

# store the final answer in this variable
number_of_neutral_tweets = 0

# perform the calculation here
???
neutral_fraction = ???
print('The fraction of neutral tweets is', neutral_fraction)

Ponder upon these questions and try some experiments to hone your skills further:

  • What are the limitations of our approach? When will it go wrong or give incorrect results?
  • How can we improve our approach to address the limitations?
  • What are some other questions you would like to ask, given a list of tweets?
  • Try collecting some real tweets from your Twitter timeline and repeat this analysis. Do the results make sense?

IMPORTANT NOTE: If you want to try out these experiments, please create a new notebook using the "New Notebook" button on your Jovian.ml profile, to avoid making unintended changes to your assignment submission notebook.

Submission

Congratulations on making it this far! You've reached the end of this assignment, and you just completed your first data analysis problem. It's time to record one final version of your notebook for submission.

Make a submission here by filling the submission form: https://jovian.ml/learn/data-analysis-with-python-zero-to-pandas/assignment/assignment-1-python-basics-practice

jovian.commit(project=project_name,environment=None)