Class 0: Intro to coding#
NYU Wagner - Python Coding for Public Policy#
Aidan Feldman
Structure for today#
Intros
Going over course info like the syllabus, tools, etc.
Intro to Python/code
About me#
Coding since 2005 đĽ
Government since 2014 đŚ
Teaching since 2011 đ
Also a modern dancer đ and cyclist đ˛
Day jobs#
Currently freelancing with the Colorado Behavioral Health Administration and Reinvent Albany. In the past, have worked forâŚ
Government#
Tech companies#
grader intro
Access these slides#
You can get there through Brightspace.
Wait list#
Introductions#
Share the following:
Name (what you go by)
Pronouns
What youâre studying
Fun fact
Who are you (as a whole)#
[This study] found that the modern language aptitude test was the strongest predictor of how quickly people would learn to code in Python ⌠language aptitude explained 43 percent, fluid reasoning explained 12.8 percent, ⌠and numeracy just 6 percent [of the variance].
In other words: Being good at learning foreign languages is a better predictor for coding aptitude than being good at math.
Accomodations#
Illness, childcare, mental health issues, etc.
Reach out via email
Class structure#
Class materials walkthrough#
Disclaimers#
Me#
Here to teach you to:
Understand the power of code
Not be afraid of code
Do a lot with just a little code
Troubleshoot
Google stuff
Not a statistician
You#
Are not going to:
Be good at coding seven weeks in
Understand everything the first time
Will want to throw your computer out a window at one or many points in the class
Celebrate the little victories
Will get out of it what you put into it
Spreadsheets vs. programming languages#
What do you like about spreadsheets?
Why spreadsheets#
The easy stuff is easy
Lots of people know how to use them
Mostly just have to point, click, and scroll
Data and logic live together as one
Why programming languages#
Data and logic donât live together
Why might this matter?
More powerful, flexible, and expressive than spreadsheet formulas; donât have to cram into a single line
=SUM(INDEX(C3:E9,MATCH(B13,C3:C9,0),MATCH(B14,C3:E3,0)))
Better at working with large data
Google Sheets and Excel have hard limits at 1-5 million rows, but get slow long before that
Reusable code (packages)
Automation
Side-by-side1#
Task |
Spreadsheets |
Programming Languages |
---|---|---|
Loading data |
Easy |
Medium |
Viewing data |
Easy |
Medium |
Filtering data |
Easy |
Medium |
Manipulating data |
Medium |
Medium |
Joining data |
Hard |
Medium |
Complicated transforms |
Impossible2 |
Medium |
Automation |
Impossible2 |
Medium |
Making reusable |
Limited3 |
Medium |
Large datasets |
Impossible |
Hard |
1 These ratings are obviously subjective
2 Not including scripting, including Excelâs new Python+pandas support
3 Google Sheets supports named functions
Python vs. other languages#
Why are you taking this class instead of R or whatever else?
Python vs. other languages#
Good for general-purpose and data stuff
Widely used in both industry and academia
Relatively easy to learn
Open source
What is Python?#
A general-purpose programming language
Text that your computer understands
Usually saved in a text file
This is true of most programming languages
Popular for data analysis and data science
Where to Python#
Pyton can be run in:
A text file, using the
python
commandA Jupyter notebook
Google Colab, Mode, Kaggle, and other sites/tools are built around it
What weâll be using for this class
Each can be on your computer (âlocalâ), or in the cloud somewhere.
Try it!#
Go to python.org/shell
Do some math (after typing each line, press
Enter
to submit)1 + 1
10 / 4
10 / 3
Calculate the number of minutes in a year
Try to break it!#
Itâs ok, you wonât hurt it.
What happened?
Jupyter#
Web based programming environment
Supports Python by default, and other languages with plugins
Nicely displays output of your code so you can check and share the results
Avoids using the command line
Avoids installation problems across different computers and operating systems
Weâre using JupyterHub, offered by NYUâs High Performance Computing (HPC) group.
Command line vs. Jupyter#
Try it!#
Go to JupyterHub
Create a notebook
Click
New
Click
Notebook
When it asks you to
Select Kernel
, choosePython [conda env:python-public-policy]
Paste in the following example
Press the âśď¸ button (or
Control
+Enter
on your keyboard)
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", trendline="ols")
fig.show()
FYI px.data.tips()
loads one of Plotlyâs sample datasets. You donât need that when plotting other datasets.
Jupyter basics#
A âcellâ can be either code or Markdown (text). Raw Markdown looks like this:
## A heading
Plain text
[A link](https://somewhere.com)
Running#
You ârunâ a cell by either:
Pressing the âśď¸ button
Pressing
Control
+Enter
on your keyboard
Cells donât run unless you tell them to, in the order you do so
Generally, you want to do so from the top every time you open a notebook
Output#
The last thing in a code cell is what gets displayed when itâs run
The output gets saved as part of the notebook
Just because thereâs existing output from a cell, doesnât mean that cell has been run during this session
Computers are not smart.#
They do exactly what you tell them to do (not what you meant them to do) in the order you tell them to do it.
Homework 0#
Walk through the assignment
Make a copy of the assignment