CS229
Python & Numpy
Angelica Sun
How does python relate to other languages?
Python 2.0 released in 2000 (Python 2.7 “end-of-life” in 2020)
Python 3.0 released in 2008 (Python 3.6+ for CS 229)
• Interpreted, like MATLAB • Object-oriented
• Dynamically-typed
https://www.researchgate.net/figure/Genealogy-of-Programming-Languages-doi101371- journalpone0088941g001_fig1_260447599
Before you start
Always use conda for environment management Create a new environment
conda create -n cs229 python=3.9 Create an environment (from configuration)
conda env create -f environment.yml Activate an environment after creation
conda activate cs229
List existing environments
conda env list
Install a package in current environment
conda install PACKAGENAME (or pip)
More commands:
https://conda.io/projects/conda/en/latest/_downloads/843d9e0198f2a193a3484886f a28163c/conda-cheatsheet.pdf
Notepad is not your friend …
Get a text editor/IDE
• PyCharm (IDE)
• Visual Studio Code (IDE??)
• Sublime Text (IDE??)
• Notepad ++/gedit
• Vim (for Linux)
To make you more prepared
PyCharm
• Great debugger • Proper project
management
FYI, professional version free for students: https://www.jetbrains.com/student/
To make you more prepared
Using PyCharm with conda
To make you more prepared
Visual Studio Code
• Light weight
• Wide variety of plugins
to enable support for
all languages
• Better UI
Python
Live Demo in Jupyter Notebook
● Links:
● Notebook:
● http://cs229.stanford.edu/notes2021spring/notes2021sprin
g/cs229-python-review-code.ipynb
● PDF Version:
http://cs229.stanford.edu/notes2021spring/notes2021sprin g/python-review-code.pdf
Your friend for debugging
Python Command
Description
array.shape
Get shape of numpy array
array.dtype
Check data type of array (for precision, for weird behavior)
type(stuff)
Get type of a variable
import pdb; pdb.set_trace()
Set a breakpoint (https://docs.python.org/3/library/pdb.html)
print(f’My name is {name}’)
Easy way to construct a message
Good luck on your HW/Project!
Questions?