A. Colin Cameron: Python coding tips
Basics
Python is case sensitive.
Use ASCII mode in editors, not UTF-8.
In most cases one can use either single ' ' or
double quotes " ".
Long lines can carry over to subsequent lines by using backslash
\ at end of line
Python uses indentation to indicate a block of code,
whereas many other programming languages use { }
Exactly the same level of indentation (4 spaces is
recommended) must be used throughout the code segment.
Comments
Comment a line using #
Comment several lines by three successive “ (so “””) at beginning
and three successive “ “ (so
“””) at beginning of a final line at the end followed by a
subsequent blank line.
Filenames
Using absolute paths is recommended. To use working directories
use the os module.
For complete listing of filename use / and not \
e.g. df =
pandas.read_csv("c:/Users/cameron/Tmp/mydata.csv
Shortening commands
You can shorten commands by assignment.
For example, instead of
import pandas
pandas.read_stata("myfile.dta")
shorten using
import pandas as pd
pd.read_stata("myfile.dta")
Regression commands using statistics packages usually order y
then X.
Regression commands using computer science packages such
as machine learning can instead order X then y.
Kevin Sheppard "Introduction to Python for Econometrics,
Statistics and Numerical Analysis: Fourth+ Edition"
pdf at https://www.kevinsheppard.com/teaching/python/notes/
Wes McKinney "Python for Data Analysis: data wrangling with
pandas, NumPy, and Jupyter", Third edition.
html web version at https://wesmckinney.com/book/
and paperback at Amazon
A. Colin Cameron / UC-Davis Economics / http://www.econ.ucdavis.edu/faculty/cameron