Table of Content

Housekeeping

Disclaimer: This workshop is not the be-all and end-all of your journey with python. The goal is to get you up and running as soon as possible so that you feel empowered to explore further. I have some resources for you at the end of the workshop!!

This is a beta workshop. If you find any typos or have suggestions please do let me know.

Join the community of learners

Notebook Used in this Workshop

How not to teach Python - A Workshop

Who is this workshop for?

Programming is hard and inaccessible especially if one does not have the right resource to efficiently learn. Google is the place where every beginner programmer goes to look for resources on the internet and get a bunch of results to dive deep. Hey, the Internet is a cesspool and dipping hands in every possible tutorial is an inescapable journey through the rabbit hole. More often than not you might be stuck and quit programming because of not having a sense of purpose.

The goal of this workshop is to change all these uncertainties and provide an efficient way to learn python with a sense of purpose to look beyond this workshop. Here you belong to a learner community, we will help each other to move past the hurdle.

Navigating through the workshop

This workshop has 3 sections, Beginner, Intermediate and advanced.

The goal of the beginner section

  • Introduce the basic concept of Python by building a calculator

The goal of the intermediate section

  • Introduce computational thinking and some intermediate concepts while building a game rock, pepper, scissor

The goal of the advanced section

  • We will go for text analysis, particularly sentiment analysis.

Feel free to start at any point.

Beginner

Alert!! Project Based learning!! We want to know just enough to build a calculator. We will implement addition, subtraction, multiplication, and division. We will start with basic and push higher. Let's see how fancy a calculator can be. While building the calculator, we will also introduce the key concept of python.

Variable and data type

A Variable is just a fancy container that can contain anything.

Anything can be text, numbers, lists, tuples, dictionaries. Let's explore.

In [60]:
# declaring a variable
# integer if x = 5, if x = 5.0 then float
x = 5
# print variable on the screen
print(x)
# string can be added with each other. Anything that goes between single or double quote is a string even a space.
y = 'blah blah' + ' ' + 'blah'
print(y)
# empty list. We will talk about this later.
z = []
# empty tuple. We will talk about this later.
i = ()
# empty dictionary. We will talk about this later.
j = {}
5
blah blah blah
In [61]:
# type() is a built in function that can be called on any variable to check datatype.
# not a must know function tbh.
print(type(x))
print(type(y))
print(type(z))
print(type(i))
print(type(j))
<class 'int'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>

Only the variable of the same type can be added or concatenated together. That means int(integer) can not be concatenated with str(string).

In [62]:
z = x + y
print(z)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-a7554d7108d0> in <module>
----> 1 z = x + y
      2 print(z)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Protip: read the error. Understanding error is an essential skill. To solve this we can convert int to str. We can do vice versa also.

In [63]:
z = str(x) + y
print(z)
5blah blah blah

Project Calculator: addition, subtraction, multiplication, division

In [64]:
# addition
x = 5 + 2
# changing variable type does the trick
print('addition: ' + str(x))
# substraction
x = 5 - 2
print('substraction: ' + str(x))
# multiplication
x = 5 * 2
print('multiplication: ' + str(x))
# division
x = 5 / 2
print('division: ' + str(x))
addition: 7
substraction: 3
multiplication: 10
division: 2.5

Let's learn how to take user input.

In [65]:
x = input("Enter a number:")
print("You Entered: " + str(x))
Enter a number:5
You Entered: 5

now let's implement the addition of a calculator. We will take two inputs from the user and get the output.

In [66]:
print("### Addition Functionality of Calculator ###")
input1 = input("Enter a number:")
print("1st number you entered: " + str(input1))
input2 = input("Enter a number:")
print("2nd number you entered: " + str(input2))
# any input python takes from the user treat it as a string. Hence we need to convert to integer for addition.
add = int(input1) + int(input2)
print("Addition: " + str(add))
### Addition Functionality of Calculator ###
Enter a number:1
1st number you entered: 1
Enter a number:4
2nd number you entered: 4
Addition: 5

Congratulation, you have built the addition function of the calculator!! Try to build subtraction, multiplication, and division as you wish.

Loops - if elif else

So far we have built specific functions for the calculator and the user does not have to choose. What if the user wants to use either of the operations(addition, subtraction, multiplication, division)? For this, we can use loops.

There are different loops you might be familiar with for instance if, while, and for. Let's learn one by one.

In [67]:
x = 7
# if followed by a condition in this case a variable, x<5
if x<5:
  # if the condition passes, execute this print
  print("Wooho!!")
# if more than one condition in the if statement except the first condition, rest of the condition declared in elif
elif 5<x<10:
  print("Wow!!")
# if none of the above condition passes then execute the following else, here its acts like a failsafe
else:
  print("sad so sad!!")
Wow!!

calculator: Let's build the different functions of the calculator with an if loop, so that the user can choose different functions in one go.

In [ ]:
print('### Please enter two numbers for operation ###')
x = int(input("Enter a number1:"))
y = int(input("Enter a number2:"))
# multiline string with triple quote
z = int(input("""Enter a number to Choose a operation(
  1 for addition, 
  2 for substraction, 
  3 for multiplication, 
  4 for division):
  
  """
  ))
if z==1:
  print('you have chosen addition')
  print('result: ' + str(x + y))
elif z==2:
  print('you have chosen addition')
  print('result: ' + str(x - y))
elif z==3:
  print('you have chosen addition')
  print('result: ' + str(x * y))
elif z==4:
  print('you have chosen addition')
  print('result: ' + str(x / y))
else:
  print('you have chosen the wrong operation')
### Please enter two numbers for operation ###
Enter a number1:3
Enter a number2:3
Enter a number to Choose a operation(
  1 for addition, 
  2 for substraction, 
  3 for multiplication, 
  4 for division):
  
  1
you have chosen addition
result: 6

Congrats!! We have updated our calculator!! Now user can choose either of the operations in one try.

Next up while and for loops.

Loops - for

So far the users of our basic calculator can use it once and they have to execute the code again to use its function(addition, subtraction, multiplication, division). Let's fix that!! We will introduce the concept of the for loop to provide users a finite number of use of our calculator before they have to execute the code block again.

Before going for the idea of for loop, let's introduce the idea of a list that we have mentioned in the beginning. Let's see how the list works.

In [ ]:
# declaring empty list
x = []
# checcking the type of x and indeed it is a list
print(type(x))
<class 'list'>
In [ ]:
# list can hold multiple datatypes i.e. numbers aka int, float; text aka string; other list aka nested list; dictionary
# lets not complicate things just look at the example.
x = [100, 100.32, "Woohoo"]
print(x)
[100, 100.32, 'Woohoo']
In [ ]:
# indexing in python starts from 0 that means the first element of a sequence is 0 index in python
# lets access the fitst element of the list
x = [100, 100.32, "Woohoo"]
print(x[0])
100
In [ ]:
# to find the number of element we can use the built in len function
x = [100, 100.32, "Woohoo"]
print(len(x))
3
In [ ]:
# please find the last element of the list remember the index starts at 0.
In [ ]:
# alternatively without knowing the length of the list, we can simply call -1 on the list to find the last element of the list.
x = [100, 100.32, "Woohoo"]
print(x[-1])
Woohoo
In [ ]:
# lets look at how to add an element to our list.
x = [100, 100.32, "Woohoo"]
# Adding a 4th element to the list, by default the new element goes to the end of the list.
x.append("Blah Blah")
print(x)
[100, 100.32, 'Woohoo', 'Blah Blah']

This is enough for us to know about the list to proceed with building the calculator. Let's look at how to print all the elements of a list with 2 lines of code instead of calling the index of each element.

In [ ]:
x = [100, 100.32, 'Woohoo', 'Blah Blah']
for item in x:
  print(item)
100
100.32
Woohoo
Blah Blah

Let's break it down, what is happening? This is how a for loop looks in code

for variable in sequence {
 # do something
}

Remember: the variable in for loop is only available to the for loop for going over the sequence.

The sequence can be a list, dictionary, or list generated with python's built-in functions.

In our case, it is a list.

Time for introducing a built-in function range. this essentially works like a list of numbers. Let's declare a list.

x = [0,1,2,3,4]

Without doing this manually, we can accomplish the same with a built-in range function.

range(5)

And we can use a for loop to iterate over the range to build the same list.

In [ ]:
# lets build the list x = [0,1,2,3,4] with for loop and range
# lets declare an empty list
x = []
for item in range(5):
  # lets use append to add each item to the list x
  x.append(item)
print(x)
[0, 1, 2, 3, 4]

A quick question can you answer how many times

for item in range(5):

will run?

hint: it runs as many times as it adds an item to the empty list. How many items were added to our empty list?

If you guess 5 times you are correct. Let's leverage this concept to provide users a finite number of use of our calculator before they have to execute the code block again.

In [1]:
for num_of_tries in range(4):
  print('### Please enter a operation you want to perform ###')
  # multiline string with triple quote
  z = int(input("""Enter a number to Choose a operation(
    1 for addition, 
    2 for substraction, 
    3 for multiplication, 
    4 for division):
    
    """
    ))
  x = int(input("Enter a number1:"))
  y = int(input("Enter a number2:"))
  if z==1:
    print('you have chosen addition')
    print('result: ' + str(x + y))
  elif z==2:
    print('you have chosen subtraction')
    print('result: ' + str(x - y))
  elif z==3:
    print('you have chosen multiplication')
    print('result: ' + str(x * y))
  elif z==4:
    print('you have chosen division')
    print('result: ' + str(x / y))
  else:
    print('you have chosen the wrong operation')
### Please enter a operation you want to perform ###
Enter a number to Choose a operation(
    1 for addition, 
    2 for substraction, 
    3 for multiplication, 
    4 for division):
    
    1
Enter a number1:1
Enter a number2:2
you have chosen addition
result: 3
### Please enter a operation you want to perform ###
Enter a number to Choose a operation(
    1 for addition, 
    2 for substraction, 
    3 for multiplication, 
    4 for division):
    
    2
Enter a number1:1
Enter a number2:2
you have chosen subtraction
result: -1
### Please enter a operation you want to perform ###
Enter a number to Choose a operation(
    1 for addition, 
    2 for substraction, 
    3 for multiplication, 
    4 for division):
    
    3
Enter a number1:1
Enter a number2:2
you have chosen multiplication
result: 2
### Please enter a operation you want to perform ###
Enter a number to Choose a operation(
    1 for addition, 
    2 for substraction, 
    3 for multiplication, 
    4 for division):
    
    4
Enter a number1:1
Enter a number2:3
you have chosen division
result: 0.3333333333333333

Loops - While

So far the calculator that we have built is super ineffective. A user will try to use the calculator as many times as they want. We will introduce a while loop to achieve that.

The general syntax of while is pretty straightforward.

While True:
  # do something
In [ ]:
while True:
  print('### Please enter a operation you want to perform ###')
  # multiline string with triple quote
  z = int(input("""Enter a number to Choose a operation(
    1 for addition, 
    2 for substraction, 
    3 for multiplication, 
    4 for division):
    
    """
    ))
  x = int(input("Enter a number1:"))
  y = int(input("Enter a number2:"))
  if z==1:
    print('you have chosen addition')
    print('result: ' + str(x + y))
  elif z==2:
    print('you have chosen addition')
    print('result: ' + str(x - y))
  elif z==3:
    print('you have chosen addition')
    print('result: ' + str(x * y))
  elif z==4:
    print('you have chosen addition')
    print('result: ' + str(x / y))
  else:
    print('you have chosen the wrong operation')
  
  prompt = input("Wish to continue? (yes/no): ")
  if prompt == "no":
    break
### Please enter a operation you want to perform ###
Enter a number to Choose a operation(
    1 for addition, 
    2 for substraction, 
    3 for multiplication, 
    4 for division):
    
    1
Enter a number1:1
Enter a number2:1
you have chosen addition
result: 2
Wish to continue? (yes/no): no

Quiz 1

In [44]:

In [69]:

Intermediate

Computational Thinking

Before introducing further concepts. Let's talk about computational thinking. In my opinion, this is more important than concepts. It helps to break down the problem you are trying to address or the application you are trying to build in chunks.

Thinking like that makes the life of a programmer much easy and more organized. Let's learn how to think computationally with one of the most well-known games rock, paper, scissor, a two-player game.

Before getting our hands dirty and building the game, let's think about the principle of the game. In computers, most of the software we use nowadays is rule-based or simply known as algorithmic(a set of rules that accomplish a certain task).

The rules of rock paper scissors are straightforward.

  • Rock smashes scissors.
  • Paper covers rock.
  • Scissors cut paper.

Now you know the rule. Let's do some computational thinking. There are possibly 3 options for any of our players to choose from. Let's do this in python. Where can I store our options?

Remember list, an object that can store other objects like these three objects(rock, paper, scissors).

options = ["rock", "paper", "scissor"]

So, now, the player will choose either of these options. So we need user input. Ok then let's then take some user input.

user_input = input("Enter a choice (rock, paper, scissors): ")

Let's say our other player is a computer. Us vs the machine then.

The computer is not inherently intelligent. You either have to teach computers to be intelligent or to make their life easy give them a finite number of options to choose from. Voila, our options list comes in handy here.

But let's think about the problem, if the computer's choice is predictable, it is no fun for the human to play against it. So, we need to randomize. To achieve this, we can use python's random function.

computer_input = random.choice(options)

Now the computer will randomly choose one of the 3 options.

Ok, so our players have made up their minds, now what? Hey, we need to decide who wins, we are not playing judge and jury, there is no place for emotion here.

We decide our fate based on the rules. Remember the rules we mentioned for our game before like Rock smashes scissors, if rock and scissors are the two inputs then rock wins by smashing the scissor. Let's build the logic with the if else statement.

if(user_input.lower()=="rock" && computer_input.lower()=="scissors"):
   print("user wins!!")
else:
   print("Computer wins!!")

Well, there is a problem here if you did not yet recognize it. What if both the user and computer input the same. Then it's a tie! We have a take care of this scenario either. Ok, let's tackle this.

if(user_input.lower()== computer_input.lower()):
   print("its a tie!!")
elif(user_input.lower()=="rock" && computer_input.lower()=="scissors"):
   print("user wins!!")
else:
   print("Computer wins!!")

Try to think computationally and take a piece of paper to write on it the rest. You can do programming on paper also. A computer is there to validate your thinking.

Function

Let's introduce the concept of function. Instead of writing the same line of code or executing a block of code to take user input over and over. We can write a function to take user input. Using function makes our program efficient because we can reuse the existing code. Let's write a function for taking user input.

options = ["rock", "paper", "scissor"]
def user_input(options):
 while(True):
   user_input = input("Enter a choice (rock, paper, scissors): ")
   if user_input.lower() in options:
     print("You have entered", user_input)
     break
   else:
     print("Invalid input")
 return user_input

Hey, this looks complicated, isn't it? The former input command was just a single line but we have written so many lines here. The thinking behind this is we have made sure that the user will not be able to enter put anything other than the three options. The former input statement would have failed if the user did not enter any value(null) or some other value. Thinking about all these is part of computational thinking.

We have missed one important aspect, what if the user wants to quit(doesn't want to play)? Try building that within the user_input function.

In [ ]:
options = ["rock", "paper", "scissor"]
def user_input(options):
  while(True):
    user_input = input("Enter a choice (rock, paper, scissors): ")
    if user_input.lower() in options:
      print("You have entered", user_input)
      break
    else:
      print("Invalid input")
  return user_input
newinput = user_input(options)
print(newinput)
Enter a choice (rock, paper, scissors): rock
You have entered rock
rock

Hey, there are other intermediate concepts like class and inheritance. I will not be going to teach you here because I want you to learn by googling.

Learning by googling is an important skill to attain. Understanding other people's code(debugging) is the greatest leap that you can take to become an advanced programmer from a novice.

Quiz 2

In [68]:

Advance: Sentiment Analysis

We have introduced the concept of computational thinking and emphasized googling. This is an advanced section, I will show you how to undertake a project and use the power of Google search to plan and implement a project.

For this, we will do sentiment analysis on amazon reviews. Well, let's assess the requirement. First I need some way to get the review from amazon. We can always use the oldest trick in the book copy and paste!!! Let's not do that or maybe call it a Plan B, isn't it will be great if we can collect a lot of reviews by running some blocks of codes and then run sentiment analysis on it? This is our plan A.

To execute our plan what we need is an amazon scrapper. We can write the scrapper or get other people’s code to integrate within our project to make our life easier!! Let’s do that. After doing some search. I have found a promising one!!

https://www.scrapehero.com/tutorial-how-to-scrape-amazon-product-details-using-python-and-selectorlib/

Now what we need to do is to understand it and debug the code so that if we have any error, we can resolve it. Also, modify the code based on requirements.

Try to understand the code!!! Here is a headstart, website like amazon usually does not allow scrapping data in a bulk, also they have a certain parameter that needs to be passed before you even start scrapping. Parameters within the dictionary header are those parameters. The scrapper requires a configuration file that the tutorial has already built for us (selector.yml). You do not need to worry about that, I have already incorporated the file within the project. Also, we need to paste our amazon review page links within a file called url.txt. You do not need to think about that either, just copy and paste URL within the links list in code block 2.

I have given you enough headstart. Try to debug yourself. If you fail, just send me an email, join the community in slack and share the issue you encountered. Last but not least google!!!

In [ ]:
!pip3 install python-dateutil lxml requests selectorlib transformers
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: python-dateutil in /usr/local/lib/python3.8/dist-packages (2.8.2)
Requirement already satisfied: lxml in /usr/local/lib/python3.8/dist-packages (4.9.2)
Requirement already satisfied: requests in /usr/local/lib/python3.8/dist-packages (2.23.0)
Collecting selectorlib
  Downloading selectorlib-0.16.0-py2.py3-none-any.whl (5.8 kB)
Collecting transformers
  Downloading transformers-4.25.1-py3-none-any.whl (5.8 MB)
     |████████████████████████████████| 5.8 MB 30.0 MB/s 
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/dist-packages (from python-dateutil) (1.15.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/dist-packages (from requests) (2022.12.7)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.8/dist-packages (from requests) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.8/dist-packages (from requests) (2.10)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.8/dist-packages (from requests) (1.24.3)
Requirement already satisfied: Click>=6.0 in /usr/local/lib/python3.8/dist-packages (from selectorlib) (7.1.2)
Requirement already satisfied: pyyaml>=3.12 in /usr/local/lib/python3.8/dist-packages (from selectorlib) (6.0)
Collecting parsel>=1.5.1
  Downloading parsel-1.7.0-py2.py3-none-any.whl (14 kB)
Collecting cssselect>=0.9
  Downloading cssselect-1.2.0-py2.py3-none-any.whl (18 kB)
Requirement already satisfied: packaging in /usr/local/lib/python3.8/dist-packages (from parsel>=1.5.1->selectorlib) (21.3)
Collecting w3lib>=1.19.0
  Downloading w3lib-2.1.1-py3-none-any.whl (21 kB)
Requirement already satisfied: filelock in /usr/local/lib/python3.8/dist-packages (from transformers) (3.8.2)
Collecting tokenizers!=0.11.3,<0.14,>=0.11.1
  Downloading tokenizers-0.13.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.6 MB)
     |████████████████████████████████| 7.6 MB 65.2 MB/s 
Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.8/dist-packages (from transformers) (4.64.1)
Collecting huggingface-hub<1.0,>=0.10.0
  Downloading huggingface_hub-0.11.1-py3-none-any.whl (182 kB)
     |████████████████████████████████| 182 kB 106.6 MB/s 
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.8/dist-packages (from transformers) (1.21.6)
Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.8/dist-packages (from transformers) (2022.6.2)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.8/dist-packages (from huggingface-hub<1.0,>=0.10.0->transformers) (4.4.0)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/local/lib/python3.8/dist-packages (from packaging->parsel>=1.5.1->selectorlib) (3.0.9)
Installing collected packages: w3lib, cssselect, tokenizers, parsel, huggingface-hub, transformers, selectorlib
Successfully installed cssselect-1.2.0 huggingface-hub-0.11.1 parsel-1.7.0 selectorlib-0.16.0 tokenizers-0.13.2 transformers-4.25.1 w3lib-2.1.1
In [ ]:
f= open("urls.txt","w+")
links = ["https://www.amazon.com/Nike-Womens-Reax-Running-Shoes/product-reviews/B07ZPL752N/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews", 
         "https://www.amazon.com/HP-Business-Dual-core-Bluetooth-Legendary/product-reviews/B07VMDCLXV/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews"
         ]
for item in links:
  f.write(item+"\n")
f.close()
In [ ]:
!pip install wget
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting wget
  Downloading wget-3.2.zip (10 kB)
Building wheels for collected packages: wget
  Building wheel for wget (setup.py) ... done
  Created wheel for wget: filename=wget-3.2-py3-none-any.whl size=9674 sha256=72ab57c5915e8516849b980bfee9848ec01c3deaf876e01e0464edaeb482961f
  Stored in directory: /root/.cache/pip/wheels/bd/a8/c3/3cf2c14a1837a4e04bd98631724e81f33f462d86a1d895fae0
Successfully built wget
Installing collected packages: wget
Successfully installed wget-3.2
In [ ]:
import wget
url = 'https://filebin.net/u24ymsapzpl3tulc/selectors.yml'
filename = wget.download(url)
In [ ]:
from selectorlib import Extractor
import requests 
import json 
from time import sleep
import csv
from dateutil import parser as dateparser

# Create an Extractor by reading from the YAML file
e = Extractor.from_yaml_file('/content/selectors.yml')

def scrape(url):    
    headers = {
        'authority': 'www.amazon.com',
        'pragma': 'no-cache',
        'cache-control': 'no-cache',
        'dnt': '1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'sec-fetch-site': 'none',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-dest': 'document',
        'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
    }

    # Download the page using requests
    print("Downloading %s"%url)
    r = requests.get(url, headers=headers)
    # Simple check to check if page was blocked (Usually 503)
    if r.status_code > 500:
        if "To discuss automated access to Amazon data please contact" in r.text:
            print("Page %s was blocked by Amazon. Please try using better proxies\n"%url)
        else:
            print("Page %s must have been blocked by Amazon as the status code was %d"%(url,r.status_code))
        return None
    # Pass the HTML of the page and create 
    return e.extract(r.text)

with open("urls.txt",'r') as urllist, open('data.csv','w') as outfile:
    writer = csv.DictWriter(outfile, fieldnames=["title","content","date","variant","images","verified","author","rating","product","url"],quoting=csv.QUOTE_ALL)
    writer.writeheader()
    for url in urllist.readlines():
        data = scrape(url) 
        if data:
            for r in data['reviews']:
                r["product"] = data["product_title"]
                r['url'] = url
                if 'verified' in r:
                    if 'Verified Purchase' in r['verified']:
                        r['verified'] = 'Yes'
                    else:
                        r['verified'] = 'Yes'
                r['rating'] = r['rating'].split(' out of')[0]
                date_posted = r['date'].split('on ')[-1]
                if r['images']:
                    r['images'] = "\n".join(r['images'])
                r['date'] = dateparser.parse(date_posted).strftime('%d %b %Y')
                writer.writerow(r)
Downloading https://www.amazon.com/Nike-Womens-Reax-Running-Shoes/product-reviews/B07ZPL752N/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews

Downloading https://www.amazon.com/HP-Business-Dual-core-Bluetooth-Legendary/product-reviews/B07VMDCLXV/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews

In [ ]:
import numpy as np
import pandas as pd
df = pd.read_csv('data.csv')
In [ ]:
df
Out[ ]:
title content date variant images verified author rating product url
0 Love these Nikes! My opinion not for narrow feet! For not liking sneakers I love these Nikes! I ... 10 Aug 2020 NaN https://images-na.ssl-images-amazon.com/images... Yes danielle 5.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
1 Great Shoe I have been looking for the shoes for a long t... 19 Apr 2022 NaN NaN Yes Happy 5.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
2 Perfect for jogging on pavement or trails. Love these shoes. Very comfortable for exercise. 24 May 2022 NaN NaN Yes Mama B 5.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
3 Not cloud #9 but not bad I tried these because of the reviews. Now I wo... 27 Aug 2020 NaN NaN Yes Thea Heinrichs 4.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
4 Good shoes Well made. Can be washed in machine. 13 May 2022 NaN NaN Yes CUSTOMER 7 5.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
5 Great shoes. Not shoes I wanted, but needed a pair I could ... 15 May 2019 NaN NaN Yes Shannon Jones 4.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
6 Nice Left foot feels like you have a bunches up soc... 03 Mar 2020 NaN NaN Yes JODY 4.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
7 Pretty shoe but runs small I wear a 7 in most shoes, my Brooks shoes are ... 28 Apr 2020 NaN NaN Yes Serena 4.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
8 It was a gift and they were happy Liked the support, light weight and plenty wide ! 27 Mar 2022 NaN NaN Yes Ryan Williams 5.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
9 Shoes Did not like them 30 Oct 2022 NaN NaN Yes Beth Jones 2.0 Nike Women's Reax Run 5 Running Shoes (White/M... https://www.amazon.com/Nike-Womens-Reax-Runnin...
10 Pretty much everything that i was looking for ... the box delivered on time and safe. it was do... 31 Oct 2019 NaN NaN Yes Gregg Stepp 4.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
11 The computer tutorials along with "Cortana" op... This is my first computer. I wasn't sure what ... 26 Sep 2020 NaN NaN Yes Joseph R. 5.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
12 Computer Came in with bad battery Don't trust the Amazon storage and handling pr... 11 Jun 2020 NaN NaN Yes David G. Moore 4.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
13 No cd drive if that matters to you It does its job. However in case you didn’t kn... 03 Jun 2021 NaN NaN Yes amanda 4.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
14 2 years and one month, died. Says no operating... Very light weight. Used for office work, some... 27 Apr 2022 NaN NaN Yes Bronwyn B. 2.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
15 clunky key board clunky , flat key action.. so much distractin... 08 Jan 2020 NaN NaN Yes P. Cassidy 4.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
16 Great Laptop, Good Value, well worth it Amazon delivered my order promptly. This lapto... 03 Sep 2019 NaN https://images-na.ssl-images-amazon.com/images... Yes Maggie 5.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
17 Came in one piece Well, I saw mouse pad bundle thinking a mouse ... 18 Feb 2020 NaN NaN Yes Amazon Customer 4.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
18 The best computer ever,I recommend this seller... I recommend this seller,this laptop is really ... 17 Aug 2020 NaN NaN Yes Josué BERNARD 5.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
19 Works great for my needs!!! I really needed a good reliable cost effective... 15 Feb 2020 NaN NaN Yes KR 5.0 2019 HP 15.6 Inch HD Premium Business Laptop P... https://www.amazon.com/HP-Business-Dual-core-B...
In [ ]:
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')

model = AutoModelForSequenceClassification.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
In [ ]:
import torch
def sentiment_score(review):
    tokens = tokenizer.encode(review, return_tensors='pt')
    result = model(tokens)
    return int(torch.argmax(result.logits))+1
In [ ]:
df2 = pd.DataFrame(df['content'])
In [ ]:
df2.head()
Out[ ]:
content
0 For not liking sneakers I love these Nikes! I ...
1 I have been looking for the shoes for a long t...
2 Love these shoes. Very comfortable for exercise.
3 I tried these because of the reviews. Now I wo...
4 Well made. Can be washed in machine.
In [ ]:
df2['sentiment'] = df['content'].apply(lambda x: sentiment_score(x[:512]))
In [ ]:
pd.set_option('display.max_colwidth', None)
df2.head()
Out[ ]:
content sentiment
0 For not liking sneakers I love these Nikes! I don't have a lot to compare them too so I did my best review. I am a shoe person haha. As far as arch support In my opinion it is great. High arch support. Now I Wear a true 7.5/8 M. I bought an 8. I am wearing a thin low cut no show sock. These fit true to size with perfect room in the toe box. I stated above I'm an average or ( M) width. These are perfect for me especially post surgery on my left foot. However, it is my opinion if you have a narrow foot These probably are not the best shoe for you. If my feet were no a bit swollen they'd be to wide for me and by no means am I narrow. I'm also going to swap insoles. There will be room for orthotic insoles. These are extremely comfortable they are sharp looking and they are light weight. I trust this review has been helpful. 4
1 I have been looking for the shoes for a long time they just were not available. I am so glad I found them, they are my favorite shoe, love the way they fit and feel and they last. 5
2 Love these shoes. Very comfortable for exercise. 5
3 I tried these because of the reviews. Now I wouldn't say I stepped on to a cloud, like some said, and definitely not cloud #9. With that said, I put my plantar fasciitis insole in. And my feet didn't hurt at the end of the day. I am a dental assistant so I am literally on my feet all day. I did go up 1\2 a size like some the reviews said. I like a little extra room. So of you like a more tight snug fit. Order ypur actual size and they probably will fit right to size. 4
4 Well made. Can be washed in machine. 5

Congratulation if you have come this far, you are awesome!!! You are about to be a professional programmer.

Here is what you can do with the superpower you have just acquired!!!

  • Topic Modelling: Distant reading tons of documents and getting an idea about what these documents are about without even reading them.
  • Further Sentiment Analysis: Extract more reviews and run sentiment analysis on them.
  • Authorship attribute: Find out patterns within the text to determine the author of unlabeled text.
  • Data visualization: make charts, and bars, and go wild with visualization.
  • Learn App development: You can develop android application with python!!! https://beeware.org/
  • Learn Backend Web Development: Django

The power is yours!! There are tons of python libraries at your disposal. Just get your hands dirty and start experimenting!!

Last but not the least, A community of learners including me is here to help in slack.

A way to contact me

In [ ]:

Out[ ]:

Further Resources

Web:

https://docs.python.org/3/tutorial/index.html (The official documentation is always the best!!)

https://www.pythontutorial.net/

https://www.w3schools.com/python/

Books:

Automate the Boring Stuff with Python: Practical Programming for Total Beginners, Al Sweigart

Python Crash Course, A Hands-On, Project-Based Introduction to Programming, Eric Matthes