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:
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
NameError
for undefined variables.jovian.commit
at regular intervals.Important Links:
Make submissions here: https://jovian.ml/learn/data-analysis-with-python-zero-to-pandas/assignment/assignment-1-python-basics-practice
Get help on the community forum: https://jovian.ml/forum/t/assignment-1-python-practice/7761 . You can get help with errors or ask for hints, but please don't ask for or share the full working answer code on the forum.
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 "adityashukla-chess/python-practice-assignment" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/adityashukla-chess/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.
Q1: Assign your name to the variable name
.
name = "Aditya"
Q2: Assign your age (real or fake) to the variable age
.
age = 15
Q3: Assign a boolean value to the variable has_android_phone
.
has_android_phone = False
You can check the values of these variables by running the next cell.
name, age, has_android_phone
('Aditya', 15, False)
Q4: Create a dictionary person
with keys "Name"
, "Age"
, "HasAndroidPhone"
and values using the variables defined above.
person = dict(nam="Aditya",age= 16, Hasandroid=False)
Let's use the person
dictionary to print a nice message.
person
{'nam': 'Aditya', 'age': 16, 'Hasandroid': False}
print("{} is aged {}, and owns an {}.".format(
person["nam"],
person["age"],
"Android phone" if person["Hasandroid"] else "iPhone"
))
Aditya is aged 16, and owns an iPhone.
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'>"
for key,value in person.items():
a= type (value)
print ('''The key "{}" has the value "{}" of the "type {}"'''.format(key,value,a))
The key "nam" has the value "Aditya" of the "type <class 'str'>"
The key "age" has the value "16" of the "type <class 'int'>"
The key "Hasandroid" has the value "False" of the "type <class 'bool'>"
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)
[jovian] Updating notebook "adityashukla-chess/python-practice-assignment" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/adityashukla-chess/python-practice-assignment
Q5: Create a list containing the following 3 elements:
my_list = ["yellow",0,bool(0)]
Let's see what the list looks like:
my_list
['yellow', 0, False]
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])
My favorite color is yellow
print('I have {} pet(s).'.format(my_list[1]))
I have 0 pet(s).
if my_list[2]:
print("I have previous programming experience")
else:
print("I do not have previous programming experience")
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(7)
Let's see if the number shows up in the list.
my_list
['yellow', 0, False, 7]
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.pop(0)
'yellow'
my_list
[0, False, 7]
Q9: Complete the print
statement below to display the number of elements in my_list
.
print("The list has {} elements.".format(len(my_list)))
The list has 3 elements.
Well done, you're making good progress! Save your work before continuing
jovian.commit(project=project_name,environment=None)
[jovian] Updating notebook "adityashukla-chess/python-practice-assignment" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/adityashukla-chess/python-practice-assignment
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
for i in range (18,535):
if i%7==0:
print("{} is divisible by 7".format (i))
sum_of_numbers+=i
sum_of_numbers
21 is divisible by 7
28 is divisible by 7
35 is divisible by 7
42 is divisible by 7
49 is divisible by 7
56 is divisible by 7
63 is divisible by 7
70 is divisible by 7
77 is divisible by 7
84 is divisible by 7
91 is divisible by 7
98 is divisible by 7
105 is divisible by 7
112 is divisible by 7
119 is divisible by 7
126 is divisible by 7
133 is divisible by 7
140 is divisible by 7
147 is divisible by 7
154 is divisible by 7
161 is divisible by 7
168 is divisible by 7
175 is divisible by 7
182 is divisible by 7
189 is divisible by 7
196 is divisible by 7
203 is divisible by 7
210 is divisible by 7
217 is divisible by 7
224 is divisible by 7
231 is divisible by 7
238 is divisible by 7
245 is divisible by 7
252 is divisible by 7
259 is divisible by 7
266 is divisible by 7
273 is divisible by 7
280 is divisible by 7
287 is divisible by 7
294 is divisible by 7
301 is divisible by 7
308 is divisible by 7
315 is divisible by 7
322 is divisible by 7
329 is divisible by 7
336 is divisible by 7
343 is divisible by 7
350 is divisible by 7
357 is divisible by 7
364 is divisible by 7
371 is divisible by 7
378 is divisible by 7
385 is divisible by 7
392 is divisible by 7
399 is divisible by 7
406 is divisible by 7
413 is divisible by 7
420 is divisible by 7
427 is divisible by 7
434 is divisible by 7
441 is divisible by 7
448 is divisible by 7
455 is divisible by 7
462 is divisible by 7
469 is divisible by 7
476 is divisible by 7
483 is divisible by 7
490 is divisible by 7
497 is divisible by 7
504 is divisible by 7
511 is divisible by 7
518 is divisible by 7
525 is divisible by 7
532 is divisible by 7
20461
print('The sum of all the numbers divisible by 7 between 18 and 534 is', sum_of_numbers)
The sum of all the numbers divisible by 7 between 18 and 534 is 20461
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)
[jovian] Updating notebook "adityashukla-chess/python-practice-assignment" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/adityashukla-chess/python-practice-assignment
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 = 200*29-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
???
# this is optional
if ???:
print("The company makes an overall profit of {} dollars".format(???))
else:
print("The company makes an overall loss of {} dollars".format(???))
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)
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 = len (tweets)
print (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]
sample_tweet
'Wow, what a great day today!! #sunshine'
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
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
toTrue
.
is_tweet_happy
True
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
for tweet in tweets:
for word in happy_words:
if word in tweet:
number_of_happy_tweets+=1
print("Number of happy tweets:", number_of_happy_tweets)
Number of happy tweets: 6
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 = number_of_happy_tweets/10
print("The fraction of happy tweets is:", happy_fraction)
The fraction of happy tweets is: 0.6
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
for tweet in tweets:
for word in sad_words:
if word in tweet:
number_of_sad_tweets+=1
print("Number of sad tweets:", number_of_sad_tweets)
Number of sad tweets: 2
Q16: What fraction of the total number of tweets are sad?
sad_fraction = number_of_sad_tweets/10
print("The fraction of sad tweets is:", sad_fraction)
The fraction of sad tweets is: 0.2
The rest of this problem is optional. Let's save your work before continuing.
jovian.commit(project=project_name,environment=None)
[jovian] Updating notebook "adityashukla-chess/python-practice-assignment" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/adityashukla-chess/python-practice-assignment
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 = happy_fraction - sad_fraction
print("The sentiment score for the given tweets is", sentiment_score)
The sentiment score for the given tweets is 0.39999999999999997
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 sentiment_score>0:
print("The overall sentiment is happy")
elif sentiment_score<0:
print("The overall sentiment is sad")
else:
print("The overall sentiment is neutral")
The overall sentiment is happy
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
non_neutral_words =sad_words+ happy_words
number_of_nonneutral_tweets = 0
for tweet in tweets:
for word in non_neutral_words:
if word in tweet:
number_of_nonneutral_tweets+=1
neutral_tweets=number_of_tweets-number_of_nonneutral_tweets
print(neutral_tweets)
2
neutral_fraction = neutral_tweets/10
print('The fraction of neutral tweets is', neutral_fraction)
The fraction of neutral tweets is 0.2
Ponder upon these questions and try some experiments to hone your skills further:
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.
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)