Advanced Python - Overview
Outline of chapters:
- Dictionaries, tuples (Ch. 1 - More data structures: Dictionaries and Tuples)
- Advanced Functions (Ch. 2 of Advanced Python)
- List comprehension (Ch. 3 of Advanced Python)
- Unit testing (Ch. 4 - ch. 11 of the book)
- Reading and Writing Files (Ch. 5)
- Decorators (Ch. 6 - see Yu’s chapter - MOSTLY DONE FOR NOW)
Need to put Tuples in somewhere
Need to put JSON somewhere
Need to put DocString somewhere
Remove slicing from intro, incorporate into advanced
Is there Inheritance in intro? Put that in somewhere….
Expand on assignments for each chapter.
Come up with bigger programming assignments
Quizzes for all Advanced chapters
GUI
Topics:
- Descriptors (skip this for now)
- Magic or Dunder methods (is this necessary?)
- Iterators (Look into this more)
- Dictionaries, tuples (Ch. 3 - More data structures: Dictionaries and Tuples)
- Decorators (Ch. 5 - see Yu’s chapter - MOSTLY DONE FOR NOW)
.join function for strings
Sources:
- Python official docs
NOTES FROM BOOK: Python Crash Course
- Powerful list functions, slicing (move from intro curric., also, remove try/except)
- looping through a slice
- copying a list [:]
- P91 - Checking if a list is empty
- p63 list comprehensions
- Allows you to generate a list using one line of code.
- Without comprehensions
- Allows you to generate a list using one line of code.
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
- With
squares = [value**2 for value in range(1, 11)]
print(squares)
- (see assignments on pg 64)
- Tuples
- p72 - Using Tab, but inserting spaces
- Dictionaries:
- Ch. 6 pg 95
- Functions:
- p137 - keyword arguments
- p138 - default values
- Returning a dictionary (p144)
- Sending a COPY of a list to a function:
- function_name(list_name[:])
- p151 passing an arbitrary number of arguments
- make_pizza(*toppings)
- collects as many arguments as provided
- makes an empty Tuple called “toppings”
- make_pizza(*toppings)
- p152 - Mixing positional and arbitrary arguments
- Python matches positional and keyword arguments first and then collects any remaining arguments in the final parameter
- p152 - Using arbitrary keyword arguments
- def build_profil(first, last, **user_info)
- This function expects a first and last name, and then it allows the user to pass in as many name-value pairs as they want.
- The double asterisk causes Python to create an empty dictionary called user_info
- def build_profil(first, last, **user_info)
(maybe go back to inheritance?)
- Ch. 10 - Files and exceptions, storing data
- Ch. 11 - Testing code
NOTES FROM: LEARN PYTHON - FULL COURSE FOR BEGINNERS
Try/except:
- Look out for specific errors and decide how to handle them
try:
number = int(input(“Enter a number:”)
print(number)
except:
print(“Invalid input”)
- except - Catches any error
- 10 / 0 is an error - Division by zero
except ZeroDivisionError:
print(“Division by zero”)
except ValueError:
print(“Invalid input”)
- Can store the error as a variable:
except ZeroDivisionError as err:
print(err)
- Good practice not to just use except for everything.
Read from a file:
open(“employees.txt”, “r”) # path to the file, read mode
# w is write mode
# a is to append to end of file
# r+ read and write
- Want to store file in variable:
employee_file = open….
- Good to close the file:
employee_file.close()
- print(employee_file.readable()) # make sure file is readable true/false
print(employee_file.read()) # prints entire file
- print(employe_file.read()) # grabs first line, waits at next line
- .readlines() # puts each line into an array
for employee in employee_file.readlines():
print(employee) # prints each line
Writing and appending to files:
open(“employees.txt”, “a”) # adds onto file
employee_file.write(“Toby - human resources”)
- Easy to mess up a file. Run it again and another copy of the employee will be added
- Didn’t have a new line so its on the same line.
- Could overwrite the entire file when you use w instead of a
- Can create a new file using w also.
Modules and pip:
- Can import variables with values
- How to access modules that other people have written?
- pip - program to install python modules. Package manager.
- comes with Python3
- Terminal:
- pip - program to install python modules. Package manager.
pip –version # make sure you have it
pip install python- docx
- Where is this? Usually gets put in site packages (maybe libs)
import docx # pulls from site packages
- pip uninstall python-docx #uninstalls
Classes and Objects:
- Create our own types by creating a class
- Student class:
class Student:
def __init__(self, name, major, gpa, is_on_probation):
self.name = name
self.major = major
self.gpa = gpa
self.is_on_probation = is_on_probation
from Student import Student # from student file, import student class
student1 = Student(“Jim”, “Business”, 3.1, False)
print(student1.name) # prints name
- Class functions:
class Student:
def __init__(self, name, major, gpa, is_on_probation):
self.name = name
self.major = major
self.gpa = gpa
self.is_on_probation = is_on_probation
def on_honor_roll(self):
if self.gpa >= 3.5:
return True
else:
return False
print(student1.on_honor_roll())
- Inheritance:
class Chef:
def make_chicken(self):
print(“The chef makes a chicken”)
def make_salad(self):
print(“The chef makes salad”)
def make_special_dish(self):
print(“The chef makes BBQ ribs”)
from Chef import Chef
myChef = Chef()
myChef.make_chicken()
from Chef import Chef
class ChineseChef(Chef): # use everything that is in the Chef class
def make_fried_rice(self):
print(“The chef makes fried rice”)
def make_special_dish(self): # Overrides the other special
print(“The chef makes orange chicken.”)
NOTES FROM: LEARN PYTHON - FULL COURSE FOR INTERMEDIATE