Skip to content

Instantly share code, notes, and snippets.

@BolajiAyodeji
Created August 25, 2021 07:55
Show Gist options
  • Save BolajiAyodeji/c0b9cef7f82c9a1254ed9784e9942834 to your computer and use it in GitHub Desktop.
Save BolajiAyodeji/c0b9cef7f82c9a1254ed9784e9942834 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 1 - Python Basics Practice\n",
"\n",
"*This assignment is a part of the course [\"Data Analysis with Python: Zero to Pandas\"](https://jovian.ml/learn/data-analysis-with-python-zero-to-pandas)*\n",
"\n",
"In this assignment, you'll get to practice some of the concepts and skills covered in the following notebooks:\n",
"\n",
"1. [First Steps with Python and Jupyter](https://jovian.ml/aakashns/first-steps-with-python)\n",
"2. [A Quick Tour of Variables and Data Types](https://jovian.ml/aakashns/python-variables-and-data-types)\n",
"3. [Branching using Conditional Statements and Loops](https://jovian.ml/aakashns/python-branching-and-loops)\n",
"\n",
"\n",
"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. \n",
"\n",
"**Guidelines**\n",
"\n",
"1. Make sure to run all the code cells, otherwise you may get errors like `NameError` for undefined variables.\n",
"2. Do not change variable names, delete cells or disturb other existing code. It may cause problems during evaluation.\n",
"3. In some cases, you may need to add some code cells or new statements before or after the line of code containing the **???**. \n",
"4. Since you'll be using a free online service for code execution, save your work by running `jovian.commit` at regular intervals.\n",
"5. Questions marked **(Optional)** will not be considered for evaluation, and can be skipped. They are for your learning.\n",
"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.\n",
"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.\n",
"\n",
"\n",
"Important Links:\n",
"\n",
"* Make submissions here: https://jovian.ml/learn/data-analysis-with-python-zero-to-pandas/assignment/assignment-1-python-basics-practice\n",
"\n",
"* 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.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### How to Run the Code and Save Your Work\n",
"\n",
"**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](https://colab.research.google.com) or [Kaggle](https://kaggle.com) to use these platforms.\n",
"\n",
"\n",
"**Option 2: Running on your computer locally**: To run the code on your computer locally, you'll need to set up [Python](https://www.python.org) & [Conda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/), 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.\n",
"\n",
"**Saving your work**: You can save a snapshot of the assignment to your [Jovian](https://jovian.ai) profile, so that you can access it later and continue your work. Keep saving your work by running `jovian.commit` from time to time."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Install the library\n",
"!pip install jovian --upgrade --quiet"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Import it\n",
"import jovian"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"project_name='bolaji-python-practice-assignment'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"window.require && require([\"base/js/namespace\"],function(Jupyter){Jupyter.notebook.save_checkpoint()})"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jovian] Updating notebook \"bolajiayodeji/bolaji-python-practice-assignment\" on https://jovian.ai\u001b[0m\n",
"[jovian] Committed successfully! https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Capture and upload a snapshot\n",
"jovian.commit(project=project_name, privacy='secret', evironment=None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 1 - Variables and Data Types\n",
"\n",
"**Q1: Assign your name to the variable `name`.**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"name = \"Bolaji Ayodeji\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q2: Assign your age (real or fake) to the variable `age`.**"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"age = 67"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q3: Assign a boolean value to the variable `has_android_phone`.**"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"has_android_phone = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can check the values of these variables by running the next cell."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Bolaji Ayodeji', 67, True)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"name, age, has_android_phone"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q4: Create a dictionary `person` with keys `\"Name\"`, `\"Age\"`, `\"HasAndroidPhone\"` and values using the variables defined above.**"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"person = {\"Name\": name, \"Age\": age, \"HasAndroidPhone\": has_android_phone}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's use the `person` dictionary to print a nice message."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bolaji Ayodeji is aged 67, and owns an Android phone.\n"
]
}
],
"source": [
"print(\"{} is aged {}, and owns an {}.\".format(\n",
" person[\"Name\"], \n",
" person[\"Age\"], \n",
" \"Android phone\" if person[\"HasAndroidPhone\"] else \"iPhone\"\n",
"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q4b (Optional): Use a `for` loop to display the `type` of each value stored against each key in `person`.**\n",
"\n",
"Here's the expected output for the key `\"Name\"`: \n",
"\n",
"```\n",
"The key \"Name\" has the value \"Derek\" of the type \"<class 'str'>\"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The key Name has a value Bolaji Ayodeji of the type <class 'str'>\n",
"The key Age has a value 67 of the type <class 'str'>\n",
"The key HasAndroidPhone has a value True of the type <class 'str'>\n"
]
}
],
"source": [
"# this is optional\n",
"\n",
"for x in person:\n",
" print(\"The key\", x, \"has a value\", person.get(x), \"of the type\", type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that you've solved one problem, it would be a good idea to record a snapshot of your notebook."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"window.require && require([\"base/js/namespace\"],function(Jupyter){Jupyter.notebook.save_checkpoint()})"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jovian] Updating notebook \"bolajiayodeji/bolaji-python-practice-assignment\" on https://jovian.ai\u001b[0m\n",
"[jovian] Committed successfully! https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jovian.commit(project=project_name,environment=None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 2 - Working with Lists\n",
"\n",
"**Q5: Create a list containing the following 3 elements:**\n",
"\n",
"* your favorite color\n",
"* the number of pets you have\n",
"* a boolean value describing whether you have previous programming experience\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"my_list = [\"blue\", 0, True]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see what the list looks like:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['blue', 0, True]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q6: Complete the following `print` and `if` statements by accessing the appropriate elements from `my_list`.**\n",
"\n",
"*Hint*: Use the list indexing notation `[]`."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"My favorite color is blue\n"
]
}
],
"source": [
"print('My favorite color is', my_list[0])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I have 0 pet(s).\n"
]
}
],
"source": [
"print('I have {} pet(s).'.format(my_list[1]))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I have previous programming experience\n"
]
}
],
"source": [
"if my_list[2]:\n",
" print(\"I have previous programming experience\")\n",
"else:\n",
" print(\"I do not have previous programming experience\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q7: Add your favorite single digit number to the end of the list using the appropriate list method.**"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"my_list.append(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see if the number shows up in the list."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['blue', 0, True, 3]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q8: Remove the first element of the list, using the appropriate list method.**\n",
"\n",
"*Hint*: Check out methods of list here: https://www.w3schools.com/python/python_ref_list.asp"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list.pop(0)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, True, 3]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9: Complete the `print` statement below to display the number of elements in `my_list`.**"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The list has 3 elements.\n"
]
}
],
"source": [
"print(\"The list has {} elements.\".format(len(my_list)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Well done, you're making good progress! Save your work before continuing"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"window.require && require([\"base/js/namespace\"],function(Jupyter){Jupyter.notebook.save_checkpoint()})"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jovian] Updating notebook \"bolajiayodeji/bolaji-python-practice-assignment\" on https://jovian.ai\u001b[0m\n",
"[jovian] Committed successfully! https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jovian.commit(project=project_name,environment=None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 3 - Conditions and loops\n",
"\n",
"**Q10: Calculate and display the sum of all the numbers divisible by 7 between 18 and 534 i.e. `21+28+35+...+525+532`**.\n",
"\n",
"*Hint*: One way to do this is to loop over a `range` using `for` and use an `if` statement inside it."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1141\n"
]
}
],
"source": [
"# store the final answer in this variable\n",
"sum_of_numbers = 0\n",
"\n",
"sorted_list = [];\n",
"\n",
"# perform the calculation here\n",
"numbers = [11,15,78,21,28,35,525,532, 600, 1000]\n",
"\n",
"for x in numbers:\n",
" if x in range(18, 534):\n",
" if x%7 == 0:\n",
" sorted_list.append(x)\n",
" sum_of_numbers = sum(sorted_list)\n",
"print(sum_of_numbers);"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The sum of all the numbers divisible by 7 between 18 and 534 is 1141\n"
]
}
],
"source": [
"print('The sum of all the numbers divisible by 7 between 18 and 534 is', sum_of_numbers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"window.require && require([\"base/js/namespace\"],function(Jupyter){Jupyter.notebook.save_checkpoint()})"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jovian] Updating notebook \"bolajiayodeji/bolaji-python-practice-assignment\" on https://jovian.ai\u001b[0m\n",
"[jovian] Committed successfully! https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jovian.commit(project=project_name,environment=None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 4 - Flying to the Bahamas\n",
"\n",
"**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?**\n",
"\n",
"Fill in values or arithmetic expressions for the variables below."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"cost_of_flying_plane = 5000"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"number_of_passengers = 29"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"price_of_ticket = 200"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"profit = number_of_passengers*price_of_ticket"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The company makes of a profit of 5800 dollars\n"
]
}
],
"source": [
"print('The company makes of a profit of {} dollars'.format(profit))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**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.**\n",
"\n",
"Use an `if` statement to display the result."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"# this is optional\n",
"number_of_return_passengers = 12\n",
"\n",
"profit1 = number_of_passengers*price_of_ticket\n",
"profit2 = number_of_return_passengers*price_of_ticket\n",
"\n",
"profit = profit1 + profit2"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The company makes an overall profit of 8200 dollars\n"
]
}
],
"source": [
"# this is optional\n",
"if profit > 5000:\n",
" print(\"The company makes an overall profit of {} dollars\".format(profit))\n",
"else:\n",
" print(\"The company makes an overall loss of {} dollars\".format(profit))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Great work so far! Want to take a break? Remember to save and upload your notebook to record your progress."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"window.require && require([\"base/js/namespace\"],function(Jupyter){Jupyter.notebook.save_checkpoint()})"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jovian] Updating notebook \"bolajiayodeji/bolaji-python-practice-assignment\" on https://jovian.ai\u001b[0m\n",
"[jovian] Committed successfully! https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment'"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jovian.commit(project=project_name,environment=None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 5 - Twitter Sentiment Analysis\n",
"\n",
"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*.\n",
"\n",
"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*."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"tweets = [\n",
" \"Wow, what a great day today!! #sunshine\",\n",
" \"I feel sad about the things going on around us. #covid19\",\n",
" \"I'm really excited to learn Python with @JovianML #zerotopandas\",\n",
" \"This is a really nice song. #linkinpark\",\n",
" \"The python programming language is useful for data science\",\n",
" \"Why do bad things happen to me?\",\n",
" \"Apple announces the release of the new iPhone 12. Fans are excited.\",\n",
" \"Spent my day with family!! #happy\",\n",
" \"Check out my blog post on common string operations in Python. #zerotopandas\",\n",
" \"Freecodecamp has great coding tutorials. #skillup\"\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's begin by answering a very simple but important question about our dataset.\n",
"\n",
"**Q12: How many tweets does the dataset contain?**"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"number_of_tweets = len(tweets)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To identify whether a tweet is happy, we can simply check if contains any of the words from `happy_words`. Here's an example:"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"sample_tweet = tweets[0]"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Wow, what a great day today!! #sunshine'"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sample_tweet"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"is_tweet_happy = False\n",
"\n",
"# Get a word from happy_words\n",
"for word in happy_words:\n",
" # Check if the tweet contains the word\n",
" if word in sample_tweet:\n",
" # Word found! Mark the tweet as happy\n",
" is_tweet_happy = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Do you understand what we're doing above? \n",
"\n",
"> 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`. "
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_tweet_happy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q13: Determine the number of tweets in the dataset that can be classified as happy.**\n",
"\n",
"*Hint*: You'll need to use a loop inside another loop to do this. Use the code from the example shown above."
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"# store the final answer in this variable\n",
"number_of_happy_tweets = 0\n",
"happy_tweets = []\n",
"\n",
"# perform the calculations here\n",
"for tweet in tweets:\n",
" for word in happy_words:\n",
" if word in tweet:\n",
" happy_tweets.append(tweet)\n",
"number_of_happy_tweets = len(happy_tweets)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of happy tweets: 6\n"
]
}
],
"source": [
"print(\"Number of happy tweets:\", number_of_happy_tweets)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q14: What fraction of the total number of tweets are happy?**\n",
"\n",
"For example, if 2 out of 10 tweets are happy, then the answer is `2/10` i.e. `0.2`."
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"happy_fraction = number_of_happy_tweets/number_of_tweets"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The fraction of happy tweets is: 0.6\n"
]
}
],
"source": [
"print(\"The fraction of happy tweets is:\", happy_fraction)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To identify whether a tweet is sad, we can simply check if contains any of the words from `sad_words`.\n",
"\n",
"**Q15: Determine the number of tweets in the dataset that can be classified as sad.**"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"# store the final answer in this variable\n",
"number_of_sad_tweets = 0\n",
"sad_tweets = []\n",
"\n",
"# perform the calculations here\n",
"for tweet in tweets:\n",
" for word in sad_words:\n",
" if word in tweet:\n",
" sad_tweets.append(tweet)\n",
"number_of_sad_tweets = len(sad_tweets)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of sad tweets: 2\n"
]
}
],
"source": [
"print(\"Number of sad tweets:\", number_of_sad_tweets)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q16: What fraction of the total number of tweets are sad?**"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"sad_fraction = number_of_sad_tweets/number_of_tweets"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The fraction of sad tweets is: 0.2\n"
]
}
],
"source": [
"print(\"The fraction of sad tweets is:\", sad_fraction)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The rest of this problem is optional. Let's save your work before continuing."
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"window.require && require([\"base/js/namespace\"],function(Jupyter){Jupyter.notebook.save_checkpoint()})"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jovian] Updating notebook \"bolajiayodeji/bolaji-python-practice-assignment\" on https://jovian.ai\u001b[0m\n",
"[jovian] Committed successfully! https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment'"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jovian.commit(project=project_name,environment=None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"**Q16b (Optional): Calculate the sentiment score, which is defined as the difference betweek the fraction of happy tweets and the fraction of sad tweets.**"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"sentiment_score = happy_fraction - sad_fraction"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The sentiment score for the given tweets is 0.39999999999999997\n"
]
}
],
"source": [
"print(\"The sentiment score for the given tweets is\", sentiment_score)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"<img src=\"https://i.imgur.com/6CCIwCb.png\" style=\"width:400px\">\n",
"\n",
"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?\n",
"\n",
"**Q16c (Optional): Display whether the overall sentiment of the given dataset of tweets is happy or sad, using the sentiment score.**"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The overall sentiment is happy\n"
]
}
],
"source": [
"if sentiment_score>0:\n",
" print(\"The overall sentiment is happy\")\n",
"else:\n",
" print(\"The overall sentiment is sad\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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. \n",
"\n",
"**Q16d (Optional): What is the fraction of tweets that are neutral i.e. neither happy nor sad.**"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"# store the final answer in this variable\n",
"number_of_neutral_tweets = 0\n",
"\n",
"# perform the calculations here\n",
"number_of_neutral_tweets = len(tweets) - (number_of_happy_tweets - number_of_sad_tweets)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"neutral_fraction = number_of_neutral_tweets/number_of_tweets"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The fraction of neutral tweets is 0.6\n"
]
}
],
"source": [
"print('The fraction of neutral tweets is', neutral_fraction)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ponder upon these questions and try some experiments to hone your skills further:\n",
"\n",
"* What are the limitations of our approach? When will it go wrong or give incorrect results?\n",
"* How can we improve our approach to address the limitations?\n",
"* What are some other questions you would like to ask, given a list of tweets?\n",
"* Try collecting some real tweets from your Twitter timeline and repeat this analysis. Do the results make sense?\n",
"\n",
"**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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Submission \n",
"\n",
"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.\n",
"\n",
"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"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"application/javascript": [
"window.require && require([\"base/js/namespace\"],function(Jupyter){Jupyter.notebook.save_checkpoint()})"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jovian] Updating notebook \"bolajiayodeji/bolaji-python-practice-assignment\" on https://jovian.ai\u001b[0m\n",
"[jovian] Committed successfully! https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'https://jovian.ai/bolajiayodeji/bolaji-python-practice-assignment'"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jovian.commit(project=project_name,environment=None)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment