PIC 16A: Python with Applications I
About
This course covers the fundamentals of the Python programming language in lots of detail. Until 2026, students took this course after PIC 10A. From 2027 onwards, students can take this course after PIC 1 or PIC 10A.
Python is very popular because it offers thousands of packages for many tasks. Although C++ allows for more optimized code, for performance-critical tasks, Python easily connects to fast C/C++ libraries under the hood. It is the primary language for machine learning (PyTorch and TensorFlow) and data science (Pandas and NumPy). Some students prefer Python to C++ because its syntax is closer to standard English and it is higher-level language, automating tasks like memory management.
As a student in this course, you will master the details of the language, like how Python variables behave, and how Python functions and methods pass their arguments. Understanding such details thoroughly is valuable because many other coding languages behave in a similar way. You will learn how to use Python data structures like lists, tuples, sets, and dictionaries effectively. You will also learn about two of the most popular libraries for data science, NumPy and Pandas, and you will see applications to data science throughout the course, particularly on your homework assignments.
Learning Outcomes / Course Objectives / Core Competencies
Incoming students should be able to...
- Analyze short C++ programs involving control flow and functions to determine what the code is doing, to identify any errors the code has, and to correct the errors that are found.
- Create short C++ classes from scratch.
After successful completion of PIC 16A, students should be able to...
- Rewrite short C++ programs using Python.
- Analyze short programs involving control flow, functions, and classes to determine what the code is doing, to identify any errors the code has, and to correct the errors that are found.
- Recall that a variable's type can change, that a variable stores a reference to an object, and that assignment from one variable to another copies a reference.
- Recall that functions pass their arguments by value which amounts to copying an object reference.
- Articulate that a variable which is assigned to in a function definition will be local to any future function calls.
- Predict code output for code involving ints, lists, calls to functions, and a mixture of global and local variables.
- Use lists, tuples, sets, and dictionaries, and select the best data structure for a specific application.
- Write and debug a class which implements a data structure such as a linked list or binary search tree.
- Use magic methods and inheritance in short applications.
- Write code to handle exceptions gracefully, understanding that a larger codebase would require lots of exception handling to communicate errors reasonably to users.
- Use NumPy to perform array calculations efficiently.
- Deploy a range of Python packages to approach specialized tasks, using code documentation to learn about useful methods that have not been encountered before.
General Course Outline / Schedule of Lectures
The following approximate schedule details the topics that 16A instructors should teach. Except where clearly indicated, all topics should be covered. The order in which the material is taught can be changed by an instructor to suit their teaching style and their vision for the class.
Lecture 1-4
-
starting with Python
- commenting, printing
- ints, bools, and strings
- lists
-
control flow
- indentations
- two common ways to use for loops (with and without range)
- if, elif, else, and while
-
function basics
- specifying a type for the parameters is not necessary
- specifying a return type is not necessary
- when a value is not explicitly returned, None is returned.
-
variables
- the fact that the concept of "value" in Python is more complex than in C++: value == type + object reference
-
functions in more detail
- the fact that functions in Python pass arguments by value, and that this amounts to "pass by object reference"
- examples demonstrating the subtleties of this situation
-
function scope
- the fact that a variable is local to a function call exactly when it assigned to at some point in the function definition
- such an assignment always occurs implicitly for parameters, so parameters are local to a function call
- errors that can arise as a consequence of not understanding how function scope behaves
-
default arguments
- stored by the function object at the time the function is defined
- examples when the default value references a mutable object
Lectures 5-7
- keyword arguments
- lambda functions
- type, id, ==, is
- standard data structures: lists, tuples, sets, dictionaries, comprehension
- mutable and immutable data types
Lectures 8-11
-
classes
- instance variables
- class variables
- the interaction between instance variables and class variables
- instance methods and the role of self
- name-mangling with two underscores (optional)
- magic methods
Lectures 12-18
-
NumPy
- arrays, ndim, shape, dtype
- addition and multiplication of arrays, broadcasting
-
indexing
- basic indexing, views and copying, slicing
- masking, index arrays
-
Pandas
- index and columns
- iloc, loc, “usual” indexing, and masking
- plotting data
Lecture 19-21
- inheritance and simple applications of "super"
-
exceptions
- try-except
- raise
- inheriting from Exception (optional)
Lectures 22-26
-
Other Python libraries.
At least two of the following.
- Image Processing using NumPy more extensively (best included as part of NumPy)
- Graphical User Interfaces (GUIs) using PyQt5 or TkInter (best included as part of inheritance)
- Input/Output e.g. processing CSV files or XML files (best included as part of Pandas)
- Introduction to Machine Learning (Q-Learning is recommended)
- Regular Expressions
- Natural Language Processing using NLTK
- Threading
Expected Grade Scheme and Homework Assignments
- Homework: 15%
- Midterm 1: 25%
- Midterm 2: 25%
- Final: 35%
Since this is an introductory class in which students often rely on homework assignments to reinforce their lecture learning in a timely manner, PIC 16A instructors are required to assign at least five homework assignments.