0% found this document useful (0 votes)
33 views

Python - Basic - 3 - Jupyter Notebook (Student)

The document discusses the agenda for Python Fundamentals Day 3. It will cover reading and writing files, JSON formatting, file browsing, exception handling, and object-oriented programming. For file I/O, it will discuss opening files, reading and writing lines, and JSON formatting. For exception handling, it will cover different exception types, try/except blocks, and handling exceptions. For OOP, it will summarize classes and discuss class inheritance.

Uploaded by

Mesbahur Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Python - Basic - 3 - Jupyter Notebook (Student)

The document discusses the agenda for Python Fundamentals Day 3. It will cover reading and writing files, JSON formatting, file browsing, exception handling, and object-oriented programming. For file I/O, it will discuss opening files, reading and writing lines, and JSON formatting. For exception handling, it will cover different exception types, try/except blocks, and handling exceptions. For OOP, it will summarize classes and discuss class inheritance.

Uploaded by

Mesbahur Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

Python Fundamentals Day 3


Welcome to Python Fundamentals Day 3.

So what is on the menu for today?

We start with reading and writing from files, learn how to transport data objects in the JSON format
and how to browse the filesystem.

Then we move to Exception handling. We learn about the different types of Exceptions, what they
mean and how to resolve them. Then we learn how to handle Exceptions in our code using the try-
except statement.

Last but not least, we learn about Object Oriented Programming including class inheritance and
how to define and use your own classes in your coding.

Let's go!

Table of Contents
File I/O
Reading and writing lines with open
JSON
File browsing with glob
Summary
open
JSON
import
glob
RUN ME
Exercises
Exercise 1 - Read in stocks
Exercise 2 - First ten names
Exercise 3 - Inc only
Exercise 4 - Average PE
localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 1/28
8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

Exception Handling
Exceptions
Try except
Summary
try except
try except else finally
RUN ME
Exercises
Exercise 1 - Fix it Multiply
Exercise 2 - Fix it Numbers
Exercise 3 - Fix it Open
Exercise 4 - Try salaries
Object Oriented Programming
Summary
class
RUN ME
Exercises
Exercise 1 - Get pokemon info
Exercise 2 - Create a pokemon class
Exercise 3 - Load all pokemons
Exercise 4 - Provide insights

File I/O

Reading and writing lines with open

Communicating with files is quite useful in programming.

With the open() built-in function we can read, write and append to files.

First check what is your working directory. You will be able to find the file that we will read and write
there.

In [ ]: # MC

%pwd

We now create a file object which we will use to write something to file.

In [ ]: # MC

f = open('test.txt', 'w')
f.write("Hey!")

Now check the file in your folder. Can you see Hey! in test.txt?

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 2/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

No you cannot. Yet..

In [ ]: # MC

f.close()

Check test.txt again.

Now it is there.

So it is important to not forget to close the file object.

There is an elegant construct for this. Let's try it.

In [ ]: # MC - a better version



with open('test2.txt', 'w') as f:
f.write("Hello!")

Check again. Test test2.txt file is there.

So what did we do here?

open() returns a file object and it is commonly used with two arguments: open(file, mode)

file is a string containing the filename


mode describes in which way the file will be used: writing, reading, appending.

mode can be:

'r' read (by default)


'w' write
'a' append

Notice the fat-green with as statement. Within the with statement the file object will be available as
f. After the statement the file object will be closed.

In [ ]: # MC

with open('test2.txt', 'r') as f:
print(f.read())

In [ ]: # MC

with open('test2.txt', 'a') as f:
f.write("\n")
f.write("Hello!")

Now we can check the contents of the file using

!cat -- for Mac / Linux users


localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 3/28
8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

!type -- for Windows users

In [ ]: !cat test2.txt

Use strip() to remove all whitespace characters from the beginning and the end of the string.

In [ ]: text = """


The names are:

Jeremy
Jan
Akmal
""".strip()

text

In [ ]: # MC

import os
file_names = os.path.join("..","data", "names_raw.txt")
file_names

The import statement allows you to import libraries which you can use.

Here we use os.path.join to safely join the paths of the data folder and the names_raw. txt file so
that it works for any operating system (Windows/Mac).

You can iterate over the file object and do something line by line.

In [ ]: # MC

with open(file_names, 'r') as f:
for line in f:
print(line)

Let's say now we want to transform this file into a list of names.

Question: How can we transform this into a list of the names?

Let's parse the contents of this file into a list of names.

In [ ]: # MC - str.strip()

with open(file_names, 'r') as f:
lines_list = []
for line in f:
lines_list.append(line.strip())

lines_list[2:]

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 4/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

JSON

JSON (JavaScript Object Notation) is an open-standard format that uses human-readable text to
transmit data objects consisting of attribute–value pairs.

It is used often by API's (Application Programming Interface) to communicate data between


programs or to commumnicate data between an applications and users.

The notation is almost the same as the dictionary in Python except for:

JSON: true/false -- Python: True/False


JSON: null -- Python: None

Here is an example JSON from wikipedia


(https://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example)

"firstName": "John",

"lastName": "Smith",

"isAlive": true,

"age": 25,

"address": {

"streetAddress": "21 2nd Street",

"city": "New York",

"state": "NY",

"postalCode": "10021-3100"

},

"phoneNumbers": [

"type": "home",

"number": "212 555-1234"

},

"type": "office",

"number": "646 555-4567"

},

"type": "mobile",

"number": "123 456-7890"

],

"children": [],

"spouse": null

First we import the json library to provide us with the functionality.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 5/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: # MC

import json

We can transform a data object into a json string and transform it back.

In [ ]: # Create a dictionary



data = {"name": "Jeremy",
"grades": [10, 15],
"teacher": True,
"complaints": None}
data

In [ ]: # MC - use dumps to save dict into JSON format



data_json = json.dumps(data)
data_json

Question: Do you see the difference between a dictionary and a JSON?

Notice the difference in true and null. Also notice the addidtional quotes at the start and the end
as this is a string.

Now we del data. Then load it again from the JSON file.

In [ ]: # MC

del data

In [ ]: # MC - Stay calm



data

In [ ]: # MC - use loads() to get JSON into dictionary



data = json.loads(data_json)
data

In [ ]: # MC - access the keys of a dictionary



data["grades"]

Now we can dump and load data from text files that contain json directly.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 6/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: data = [{'name': 'Jeremy', 'grades': [10, 15], 'teacher': True, 'complaints': Non
{'name': 'Akmal', 'grades': [3, 4], 'teacher': True, 'complaints': "Many"
{'name': 'Jan', 'grades': [8, 13, 20], 'teacher': True, 'complaints': Non
{'name': 'Heng', 'grades': [5, 5, 5], 'teacher': False, 'complaints': "Ma
]

In [ ]: # MC

with open('cads.json', 'w') as f:
json.dump(data, f)

Let's look at the resulting json file using !cat for Mac/Linux and !type for windows

In [ ]: !cat cads.json

Now we can load that data back into a dictionary

In [ ]: # MC - try check the type of cads



with open('cads.json', 'r') as f:
cads = json.load(f)

cads

File browsing with glob


With glob you can easily list files and folders in a directory

In [ ]: import glob



folder = os.path.join("..", "data", "folders")

for filename in glob.glob('{}/*.txt'.format(folder)):
print(filename)

And recursively.

In [ ]: import glob



folder = os.path.join("..", "data", "folders")

for filename in glob.glob('{}/**/*.txt'.format(folder), recursive=True):
print(filename)

You can get the files in a list like this.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 7/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: # MC - the output is a list



files = list(glob.glob('{}/**/*.txt'.format(folder), recursive=True))
files

Summary

open

Built-in function to open a file object to read/write/append to a file.

with open('test.txt', 'w') as f:

f.write("Hello")

with open('test2.txt', 'r') as f:

data = f.readlines()

with open('test2.txt', 'r') as f:

f.readline()

data = []

for line in f:
data.append(line)

JSON

JSON (JavaScript Object Notation) is an open-standard format that uses human-


readable text to transmit data objects consisting of attribute–value pairs. Used
often to communicate data with API's.

import json

json.dumps(data)

json.loads(json_str)

with open('data.json', 'w') as f:

json.dump(data, f)

with open('data.json', 'r') as f:

data = json.load(f)

import

import libraries to use the functionality

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 8/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

import json

import os

glob

module that is used for file listing

for filename in glob.glob('{}/*.txt'.format(folder_path)):

print(filename)

for filename in glob.glob('{}/**/*.txt'.format(folder_path), recursive=T


rue):
print(filename)

RUN ME
Please run the below code snippet. It is required for running tests for your solution.

In [ ]: def test(got, expected):


max_width_print = min([int(79/3),
len(repr(got)) + 10,
len(repr(expected)) + 10])

if got == expected:
prefix = ' OK '.center(max_width_print, '=')
else:
prefix = ' FAIL '.center(max_width_print, '=')
print(('%s\n got: %s \nexpected: %s' % (prefix, repr(got), repr(expected)
end = '\n\n')

In [ ]: test('a', 'ab')


test('a', 'a')

Exercises

Exercise 1 - Read in stocks

Read in stocks data in "data/stocks.json" and store it into stocks.

Each line of this file is a json object, but the whole file content is not a valid json object since the
jsons are no separated by a comma and there is no extra brackets surrounding the jsons.

So you need to read the file line by line and transform those strings into dict using json.loads.

Store the dictionaries into stocks.

Hints: import os, os.path.join, import json, json.loads


localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 9/28
8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: ## your code

# TEST
print("read_in_stocks")
test(len(stocks), 6756)
test(type(stocks[0]), dict)

In [ ]: # MC

import os
import json

stocks_file = os.path.join("..", "data", "stocks.json")
stocks = []

with open(stocks_file, 'r') as f:
for line in f:
stocks.append(json.loads(line))

# TEST
print("read_in_stocks")
test(len(stocks), 6756)
test(type(stocks[0]), dict)
test(type(stocks[-1]), dict)

In [ ]: # MC
# or

import os
import json

stocks_file = os.path.join("..", "data", "stocks.json")

with open(stocks_file, 'r') as f:
stocks = [json.loads(line) for line in f]

# TEST
print("read_in_stocks")
test(len(stocks), 6756)
test(type(stocks[0]), dict)
test(type(stocks[-1]), dict)

Exercise 2 - First ten names

What are the names of the first ten companies?

Hints: inspect one dict to see which key contains the name.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 10/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: names = [s['Company'] for s in stocks[0:10]]



# TEST
print("first_ten_names")
from answers import ten_companies
test(names, ten_companies)

In [ ]: # MC

names = [stock["Company"] for stock in stocks[:10] ]

# TEST
print("first_ten_names")

from answers import ten_companies
test(names, ten_companies)

Exercise 3 - Inc only

From the top 10 companies, now only show the names that contain the word 'Inc.'

In [ ]: names = ## your code



# TEST
from answers import inc_companies
print("inc_only")
test(names, inc_companies)

In [ ]: # MC

names = [n for n in names if 'Inc.' in n]

# TEST
from answers import inc_companies
print("inc_only")
test(names, inc_companies)

Exercise 4 - Average PE

Now show the average P/E for all the data. Round it by 2.

Not all the stocks have the P/E reported, so you need to handle that.

What percentage of the stocks has P/E reported? Round it by 2.

In [ ]: stocks[0]['Ticker']

In [ ]: s_pe = [s['P/E'] for s in stocks if 'P/E' in s]


len(s_pe)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 11/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: ## your code



pe =
avg_pe = ## your code
perc_with_pe = ## your code

# TEST
print("average_pe")
test(avg_pe, 41.71)
test(perc_with_pe, 0.5)

In [ ]: # MC

pe = [s['P/E'] for s in stocks if 'P/E' in s] # not all stock has 'P/E'
avg_pe = round(sum(pe) / len(pe), 2)
perc_with_pe = round(len(pe) / len(stocks), 2)

# TEST
print("average_pe")
test(avg_pe, 41.71)
test(perc_with_pe, 0.5)

Exception Handling

Exceptions
Here is an example Exception.

In [ ]: while True print('Hello world')

Arrow ^ pointing at the earliest point in the line where the error was detected
The error is caused by the token preceding the arrow
File name and line number are printed

There are different types of Exceptions.

Previous one was SyntaxError, other built-in exception types include:

ZeroDivisionError -- division by zero


NameError -- name not defined
TypeError -- incorrect type
KeyError -- key not found in dict
IndexError -- index greater than length in list

The last line of the error message indicates what happened. Let's go over them.
Find more
information here (https://docs.python.org/3/library/exceptions.html).

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 12/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: 10 * (1/0)

In [ ]: 4 + result*3

In [ ]: '1' + 1

In [ ]: d = {}
d[0]

In [ ]: l = []
l[0]

The preceding part of the error message shows the context where the exception happened.

It contains a stack traceback listing source lines.

Here is an example.

In [ ]: def divide(x, y):


return x/y

def call_divide(x, y):
return divide(x, y)

def f(x, y):
return call_divide(x, y)

f(1, 0)

Question: What has caused the Error?

Answer: We cannot divide by zero! 1/0 gives an Exception.

Try except
Sometimes exceptions are expected to happen and we want our code to handle those exceptions
in a certain way. For this there is the try except statement.

Let's try these inputs.

typing a number
typing a non-numeric string
typing a zero
ending the cell by intterupting the kernel

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 13/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: try:
print(var)
except:
print("An exception occurred")

In [ ]: x = [1,3,6,4,7,"6", 9,"5"]



try:
print(sum(x))
except:
print("There was An exception.")

In [ ]: try:
x = float(input("Please enter a number: "))
print("inverse is:", 1/x)
except:
print("Oops! That was no valid number.")

The try statement.

1. the try clause (block under try:) is executed


2. if no exception occurs, except clause is skipped
3. if an exception occurs, the rest of the try clause is skipped,

and the first except clause matching the exception is executed,


if no handler is found, execution stops, an error Traceback is displayed.

In this case we have used a bare except statement. This means all exception are catched
including the KeyboardInterrupt.

You can specifiy which exception you want to be catched and you want to handle them.

Let's try typing an number, a non-numeric number, a zero and a keyboardintterup again.

The raise statement allows the programmer to force a specified exception to occur. For example:

raise NameError('HiThere')

In [ ]: try:
x = float(input("Please enter a number: "))
print("inverse is:", 1/x)
except ValueError:
print("Oops! That was no valid number. Try again...")
except ZeroDivisionError:
print("Oops! Cannot divide by 0!")
except:
print("Something else went wrong")
# raise # return error message

else and finally are usefull to define clean-up action.

else statements are executed only if no exceptions occur in try block.


finally statements are always executed.
localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 14/28
8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

else clause avoids accidentally catching an exception.

In [ ]: def divide(x, y):


try:
result = x / y
except ZeroDivisionError:
print("division by zero!")
else:
print("result is", result)
finally:
print("executing finally clause")

divide(2,0)

In [ ]: divide(2,1)

In [ ]: divide("a","b")

Let's do another case.

In [ ]: total = 0
numbers = [1,2,3,"a", "b", "c", 5, 6, 7]

for number in numbers:
total += number

total

We cannot add up strings to a number.

If we want to have the total of the numbers in the list we can ignore the strings.

In [ ]: # MC - handel the Exception



total = 0

for number in numbers:
try:
total += number
except:
print("Something went wrong")

total

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 15/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: # MC

total = 0

for number in numbers:
try:
total += number
except TypeError as e:
print("Error: {}".format(e))

total

You can use the bare except statement. As long as you are mindful about that it catches all
exceptions it's fine. For a script you operate yourself it can be fine.

In [ ]: # MC

total = 0

for number in numbers:
try:
total += number
except:
pass
total

Summary

try except

If an error is encountered, a try block code execution is stopped and transferred


down to the except block.

try:

total += number

except:

pass

try except else finally

In addition to using an except block after the try block, you can also use the
finally
block. The code in the finally block will be executed regardless of whether an
exception
occurs. The else code is executed in case the try statement was a
succes.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 16/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

try:

result = x / y

except ZeroDivisionError:

print("division by zero!")

else:
print("result is", result)

finally:

print("executing finally clause")

RUN ME
Please run the below code snippet. It is required for running tests for your solution.

In [ ]: def test(got, expected):


max_width_print = min([int(79/3),
len(repr(got)) + 10,
len(repr(expected)) + 10])

if got == expected:
prefix = ' OK '.center(max_width_print, '=')
else:
prefix = ' FAIL '.center(max_width_print, '=')
print(('%s\n got: %s \nexpected: %s' % (prefix, repr(got), repr(expected)
end = '\n\n')

In [ ]: test('a', 'ab')


test('a', 'a')

Exercises

Exercise 1 - Fix it Multiply

We want to multiple the values of a and b of the dictionary.

Can you help?

In [ ]: data = {"a": 10, "b": 20}



answer = data[a] * data["b"]

# TEST
print("fix_it_1")
test(answer, 200)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 17/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: # MC
# "a" is the key, a is a variable that does not exist
# data not dala

data = {"a": 10, "b": 20}

answer = data["a"] * data["b"]

# TEST
print("fix_it")
test(answer, 200)

Exercise 2 - Fix it Numbers

We want to extend numbers with another list and then get the sum.

Can you help?

In [ ]: numbers = [1,2,3,4]


numbers = numbers.extend([5,6,7,8])

answer = sum(numbers)

# TEST
print("fix_it_numbers")
test(answer, 36)

In [ ]: # MC
# extend is an in-place operation!

numbers = [1,2,3,4]
numbers.extend([5,6,7,8])

answer = sum(numbers)

# TEST
print("fix_it_numbers")
test(answer, 36)

Exercise 3 - Fix it Open

We try to read the contents of data/names_raw.txt into a string.

Can you help?

In [ ]: import os

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 18/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: with open(os.path.join(data, "names_raw.txt"), 'a') as f:


content = f.read()

# TEST
print("fix_it_open")
test(content, 'The names are:\n\nJeremy\nJan\nAkmal')

In [ ]: # MC
# data is a string
# read mode needs to be 'r' for reading

with open(os.path.join("data", "names_raw.txt"), 'r') as f:
content = f.read()

# TEST
print("fix_it_open")
test(content, 'The names are:\n\nJeremy\nJan\nAkmal')

Exercise 4 - Try salaries

Have a look at the data/salaries.txt file

Load it into a list of dictionaries and use try catch to handle the non-dictionary lines.

In [ ]: salaries = ## your code

# TEST
print("try_salaries")
from answers import the_salaries
test(salaries, the_salaries)

In [ ]: from answers import the_salaries


the_salaries

In [ ]: # MC - remember to make sure the directory is correct - %pwd



salaries = []

with open(os.path.join("data", "salaries.txt")) as f:
for line in f:
try:
salaries.append(json.loads(line))
except:
pass

# TEST
print("try_salaries")
from answers import the_salaries
test(salaries, the_salaries)

Object Oriented Programming


localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 19/28
8/22/22, 9:20 PM
j g Python_Day3_MC
g - Jupyter Notebook
Object-oriented Programming, or OOP for short, is a programming paradigm in which properties
and behaviours are bundled into objects.

We will define a class Student. A class is a blue-print for an object. After defining the class Student
there is only one Student class and you can create many Student objects out of that class.

Then we create a child class PythonStudent. The child class inherits all the properties from its
parent and has it's own functionalities additionally.

How to create a class

In [ ]: # MC

class Student:

def __init__(self, name, grades=[]):


self.name = name
self.grades = grades

Instantiating objects

In [ ]: jeremy = Student("Jeremy", [10,4,5]) # instantiate an object name 'jeremy'

In [ ]: print(jeremy.grades)
print(jeremy.name)

Define methods in a class


We will extend this class with an average methods the returns the average grade of the student.

You can copy paste again and add the average function.

In [ ]: # MC

class Student:

def __init__(self, name, grades=[]):


self.name = name
self.grades = grades

def __repr__(self):
return "{}:{}".format(self.name, self.grades)

def average(self):
return round(sum(self.grades) / len(self.grades), 2)

In [ ]: jeremy = Student("Jeremy", [10,4,5])


jeremy.average()

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 20/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In OOP there is the concept of inheritance. You can make a class that is the child of another class.

We will define a class PythonStudent.

In [ ]: # MC

class PythonStudent(Student):

def can_program(self):
return True

In [ ]: narjes = PythonStudent("Narjes", [10, 20])


narjes

In [ ]: # MC

narjes.average()

In [ ]: # MC

narjes.can_program()

In [ ]: amin = PythonStudent("Amin", [30,20,15])

In [ ]: students = [jeremy, amin, narjes] # jeremy, amin, narjes are objects.
print(students)

We can filter ONLY the Python students.

In [ ]: # MC

[n for n in students if isinstance(n, Student)]

Well done!

Summary

class

Python is an Object Oriented Programming language (OOP). This means that


almost all the code is implemented using a special construct called classes.
Programmers use classes to keep related things together. When defining a child
class it will inherit the methods from the parent class.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 21/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

class Student:

def __init__(self, name, grades=[]):

self.name = name

self.grades = grades

def average(self):

return round(sum(self.grades) / len(self.grades), 2)

class PythonStudent(Student):

def can_program(self):

return True

RUN ME
Please run the below code snippet. It is required for running tests for your solution.

In [ ]: def test(got, expected):


max_width_print = min([int(79/3),
len(repr(got)) + 10,
len(repr(expected)) + 10])

if got == expected:
prefix = ' OK '.center(max_width_print, '=')
else:
prefix = ' FAIL '.center(max_width_print, '=')
print(('%s\n got: %s \nexpected: %s' % (prefix, repr(got), repr(expected)
end = '\n\n')

In [ ]: test('a', 'ab')


test('a', 'a')

Exercises

The CEO of your company happens to have a kid who is totally into Pokemon! For this reason he
is very interested in finding out more about these Pokemon for himself. The thing is.. all these
Pokemons are hidden away in files and spread out over different continents and countries folders.
Oh no, what a bummer.

Ambitious as you are and on the verge of locking down that next promotion you walk in and say:

"Calm down, I have just finished my Python-Fundamentals course, let me handle


this."

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 22/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

Use your Python skills in browsing folder structure, lists, dictionaries, reading from csv and writing
to Excel to provide those insights and totally save the day!

Have a look the the pokeworld folder inside the data folder.

The folder structure is like this: continent/country/pokemon.txt

Each file represents a pokemon and inside the file is the description.

Exercise 1 - Get pokemon info

Get the continent, country, name and description for the pokemon in
pokeworld/Asia/Malaysia/Wooper.txt

Hints: help(str.split), with open()

In [ ]: import os
pokefile = os.path.join("data", "pokeworld/Asia/Malaysia/Wooper.txt")
pokefile

In [ ]: # MC

lst = pokefile.split("/")
lst[-1].split('.')[0]

In [ ]: # MC

continent = ## your code
country = ## your code
name = ## your code
description = ## your code

# TEST
print("get_pokemon_info")

from answers import wooper_desc
test(continent, 'Asia')
test(country, 'Malaysia')
test(name, 'Wooper')
test(description, wooper_desc)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 23/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: # MC

continent = pokefile.split("/")[-3]
country = pokefile.split("/")[-2]
name = pokefile.split("/")[-1].split(".")[0]

# this is for later test(description, wooper_desc) purpose
with open(pokefile, 'r') as f:
description = f.read()

# TEST
print("get_pokemon_info")

from answers import wooper_desc

test(continent, 'Asia')
test(country, 'Malaysia')
test(name, 'Wooper')
test(description, wooper_desc)

Exercise 2 - Create a pokemon class

Create a pokemon class which has three methods:

__init__(self, pokefile) -- assign self.name, self.continent, self.country and self.description with


contents from the file
__repr__(self) -- represent a pokemon like 'Wooper from Malaysia, Asia'
is_big_data(self) -- if there is 'Apache' or 'Hadoop' in self.description return True

In [ ]: ## your code here

# TEST
print("create_pokemon_class")
pokefile = os.path.join("data", "pokeworld/Asia/Malaysia/Wooper.txt")
pokefile2 = os.path.join("data", "pokeworld/Asia/Thailand/Hive.txt")

wooper = Pokemon(pokefile)
hive = Pokemon(pokefile2)

test(wooper.__repr__(), 'Wooper from Malaysia, Asia')
test(wooper.is_big_data(), False)
test(hive.is_big_data(), True)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 24/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: # MC

class Pokemon():

def __init__(self, pokefile):

self.continent = pokefile.split("/")[-3]
self.country = pokefile.split("/")[-2]
self.name = pokefile.split("/")[-1].split(".")[0]

with open(pokefile, 'r') as f:
self.description = f.read()

def is_big_data(self):
return 'Hadoop' in self.description or 'Apache' in self.description

def __repr__(self):
return "{} from {}, {}".format(self.name, self.country, self.continent)

# TEST
print("create_pokemon_class")
pokefile = os.path.join("data", "pokeworld/Asia/Malaysia/Wooper.txt")
pokefile2 = os.path.join("data", "pokeworld/Asia/Thailand/Hive.txt")

wooper = Pokemon(pokefile)
hive = Pokemon(pokefile2)

test(wooper.__repr__(), 'Wooper from Malaysia, Asia')
test(wooper.is_big_data(), False)
test(hive.is_big_data(), True)

Exercise 3 - Load all pokemons

Now that we have defined the Pokemon class we can make a list of all the pokemon.

Hints: glob.glob, Pokemon(pokefile)

In [ ]: ## your code here



pokemons = ## your code here

# TEST
print("load_all_pokemons")
test(len(pokemons), 750)
test(type(pokemons[0]), Pokemon)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 25/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

In [ ]: # MC
import glob

folder = "data/pokeworld"
pokefiles = glob.glob('{}/**/*.txt'.format(folder), recursive=True)
pokemons = [Pokemon(f) for f in pokefiles] # remember that 'Pokemon' is a class d

# TEST
print("load_all_pokemons")
test(len(pokemons), 750) # pokemons is a list of 750 objects
test(type(pokemons[0]), Pokemon) # check if the first item in 'pokemons' has the

Exercise 4 - Provide insights

Now that we have a list of pokemon, let's answer some questions!

1. In which country is Pikachu?


2. How many pokemons in Malaysia?
3. How many pokemons in Asia?
4. Which continent has the most pokemons?
5. Which country has least pokemons?
6. Which countries have bigdata?

4.1 In which country is Pikachu?

In [ ]: # MC

pikachu = [p for p in pokemons if p.name == "Pikachu"][0] # get the first item w

pikachu.country

4.2 How many pokemons in Malaysia?

In [ ]: # MC

malaysians = [p for p in pokemons if p.country == "Malaysia"] # remember that 'po

len(malaysians)

4.3 How many pokemons in Asia?

In [ ]: # MC

asians = [p for p in pokemons if p.continent == "Asia"]

len(asians)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 26/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

4.4 Which continent has the most pokemons?

Hints:

make a dictionary continents for the count


continents.setdefault
sorted(continens.items(), key=... , reverse=True)

setdefault() method returns the value of a key (if the key is in dictionary). If not, it inserts
key with a value to the dictionary.

In [ ]: # MC

continents = {}

for p in pokemons:
continents.setdefault(p.continent, 0) # if p.continent not found, insert 0
continents[p.continent] += 1

sorted(continents.items(), key=lambda x: x[-1], reverse=True)[0]

4.5 Which country has the least pokemon?

In [ ]: # MC

countries = {}

for p in pokemons:
countries.setdefault(p.country, 0)
countries[p.country] +=1

sorted(countries.items(), key=lambda x:x[-1])[0]

4.6 Which countries have big data?

Hints: set() to get unique values of a list.

In [ ]: # MC

bigdata = set([p.country for p in pokemons if p.is_big_data()])
bigdata

Well done! Congrats on completing Python


Fundamentals!

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 27/28


8/22/22, 9:20 PM Python_Day3_MC - Jupyter Notebook

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 3/Python_Day3_MC.ipynb 28/28

You might also like