






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document gives you basic understanding of coding with that language of python using cloud platform such as binder and jupyter notebook
Typology: Study Guides, Projects, Research
1 / 10
This page cannot be seen from the preview
Don't miss anything!







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 nd 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
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.
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 above, select the Run Locally option, and follow the instructions.
Saving your work: You can save a snapshot of the assignment to your Jovian pro le, so that you can access it later and continue your work. Keep saving your work by running jovian.commit from time to time.
!pip install jovian --upgrade --quiet
import jovian
project_name='dsmlbootcamp-python-practice'
jovian.commit(project=project_name, privacy='secret', evironment=None)
[jovian] Updating notebook "hai-advisoryservices/dsmlbootcamp-python-practice" on https://jovian.ai [jovian] Committed successfully! https://jovian.ai/hai-advisoryservices/dsmlbootcamp- python-practice
'https://jovian.ai/hai-advisoryservices/dsmlbootcamp-python-practice'
Problem 1 - Variables and Data Types
Q1: Assign your name to the variable namename.
name = "Haris"
Q2: Assign your age (real or fake) to the variable ageage.
age = 32
Q3: Assign a boolean value to the variable has_android_phonehas_android_phone.
has_android_phone = False
You can check the values of these variables by running the next cell.
name, age, has_android_phone
('Haris', 32, False)
Q4: Create a dictionary personperson with keys "Name""Name" , "Age""Age" , "HasAndroidPhone""HasAndroidPhone" and values using the variables de ned above.
person = {name:'Haris', age:'32', has_android_phone:False}
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[has_android_phone] else "iPhone" ))
Q6: Complete the following printprint and ifif statements by accessing the appropriate elements from
my_listmy_list.
print('My favorite color is',my_list[0])
My favorite color is black
print('I have {} pet(s).'.format(my_list[1]))
I have 0 pet(s).
if my_list[2] == True: 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(5)
Let's see if the number shows up in the list.
my_list
['black', '0', False, 5]
Q8: Remove the rst element of the list, using the appropriate list method.
my_list.pop(0)
'black'
my_list
['0', False, 5]
Q9: Complete the printprint statement below to display the number of elements in my_listmy_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)
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+53221+28+35+...+525+.
sum_of_numbers = 0
for i in range(18,534): if i %7==0: sum_of_numbers+=i
The sum of all the numbers divisible by 7 between 18 and 534 is 20461
If you are not able to gure 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 y 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 pro t 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 = price_of_ticket * number_of_passengers
print('The company makes of a profit of {} dollars'.format(profit))
The company makes of a profit of 5800 dollars
print('The sum of all the numbers divisible by 7 between 18 and 534 is', sum_of_numbers
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.
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
for word in happy_words:
if word in sample_tweet:
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 to True.
is_tweet_happy
True
Q13: Determine the number of tweets in the dataset that can be classi ed as happy.
number_of_happy_tweets = 0
for tweet in tweets: for word in happy_words:
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'be
if word in tweet: number_of_happy_tweets+= print(number_of_happy_tweets)
6
print("Number of happy tweets:", number_of_happy_tweets)
Number of happy tweets: 6
If you are not able to gure 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..
happy_fraction = 6/
print("The fraction of happy tweets is:", happy_fraction)
The fraction of happy tweets is: 0.
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 classi ed as sad.
number_of_sad_tweets = 0
for tweet in tweets: for word in sad_words: if word in tweet: number_of_sad_tweets+=
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 = 2/
print("The fraction of sad tweets is:", sad_fraction)
The fraction of sad tweets is: 0.
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.
number_of_neutral_tweets = 0
neutral_fraction = ???
print('The fraction of neutral tweets is', neutral_fraction)
Let's save our work one nal time.
jovian.commit(project=project_name,environment=None)
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?