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.
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
The goal of the intermediate section
The goal of the advanced section
Feel free to start at any point.
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.
A Variable is just a fancy container that can contain anything.
Anything can be text, numbers, lists, tuples, dictionaries. Let's explore.
# 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 = {}
# 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))
Only the variable of the same type can be added or concatenated together. That means int(integer) can not be concatenated with str(string).
z = x + y
print(z)
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.
z = str(x) + y
print(z)
Project Calculator: addition, subtraction, multiplication, division
# 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))
Let's learn how to take user input.
x = input("Enter a number:")
print("You Entered: " + str(x))
now let's implement the addition of a calculator. We will take two inputs from the user and get the output.
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))
Congratulation, you have built the addition function of the calculator!! Try to build subtraction, multiplication, and division as you wish.
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.
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!!")
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.
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')
Congrats!! We have updated our calculator!! Now user can choose either of the operations in one try.
Next up while and for loops.
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.
# declaring empty list
x = []
# checcking the type of x and indeed it is a list
print(type(x))
# 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)
# 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])
# to find the number of element we can use the built in len function
x = [100, 100.32, "Woohoo"]
print(len(x))
# please find the last element of the list remember the index starts at 0.
# 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])
# 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)
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.
x = [100, 100.32, 'Woohoo', 'Blah Blah']
for item in x:
print(item)
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.
# 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)
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.
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')
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
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
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.
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.
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.
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)
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.
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!!
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!!!
!pip3 install python-dateutil lxml requests selectorlib transformers
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()
!pip install wget
import wget
url = 'https://filebin.net/u24ymsapzpl3tulc/selectors.yml'
filename = wget.download(url)
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)
import numpy as np
import pandas as pd
df = pd.read_csv('data.csv')
df
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')
import torch
def sentiment_score(review):
tokens = tokenizer.encode(review, return_tensors='pt')
result = model(tokens)
return int(torch.argmax(result.logits))+1
df2 = pd.DataFrame(df['content'])
df2.head()
df2['sentiment'] = df['content'].apply(lambda x: sentiment_score(x[:512]))
pd.set_option('display.max_colwidth', None)
df2.head()
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!!!
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.
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