Python and it’s basics, Study Guides, Projects, Research of Data Analysis & Statistical Methods

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

2021/2022

Available from 09/28/2022

haris-ahmed-inamdar
haris-ahmed-inamdar 🇮🇳

1 document

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Assignment
-
Python
Basics
Practice
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 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.
Guideline s
1. Make sure to run all the code cells, otherwise you may get errors like NameError for undened 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 Slack. 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.
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.
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 butt on 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.
# Install the library
!pip install jovian --upgrade --quiet
# Import it
import jovian
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python and it’s basics and more Study Guides, Projects, Research Data Analysis & Statistical Methods in PDF only on Docsity!

Assignment - Python Basics Practice

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 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

  1. Make sure to run all the code cells, otherwise you may get errors like NameError for undened 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 Slack. 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.

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.

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 prole, 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='dsmlbootcamp-python-practice'

# Capture and upload a snapshot

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 dened 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.

Hint: Use the list indexing notation [].

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.

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

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+.

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

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 prot 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

# 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 to True.

is_tweet_happy

True

Q13: Determine the number of tweets in the dataset that can be classied 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 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 classied as sad.

# store the final answer in this variable

number_of_sad_tweets = 0

# perform the calculations here

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.

# 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)

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?