Python Cheat Sheet
This is a quick walk-through of basic Python syntax - download the Jupyter notebook here
Basic Math
Python can carry out basic mathematical operations:
In [1]:
5
In [2]:
0.4
In [3]:
32
In [4]:
2
Basic Data Types
Python has a number of basic data types and containers, here is a list of those we will use frequently.
We are assigning them to a variable, using the =.
In code blocks, a # indicates a comment, and is not read by the interpreter.
In [5]:
Now the common data ‘containers’
In [6]:
Subsetting
We can get objects out of our data structures by subsetting.
Each data type has slightly different ways of subsetting
In [7]:
1
In [8]:
5
In [9]:
[3, 4, 5]
In [10]:
5
In [11]:
[1, 2, 3]
Importing Modules
External libraries, or modules, allow Python to be extended. We can access these libraries by importing them.
Anaconda comes with a lot of included modules, more can be installed using pip or conda at command line
In [12]:
Advanced Data Types
Numpy arrays, and Pandas Series and DataFrames are optimised for large data and data science applications. They can only contain one type of data (or one type per column in DataFrames) allowing code to be executed much faster, as well as vectorized operations. More on subsetting of these data types in the full course
In [13]:
array([0, 1, 2, 3, 4])
In [14]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [15]:
a 1
b 2
c 3
d 4
e 5
dtype: int64
In [16]:
a | b | |
---|---|---|
X | 1 | 2 |
Y | 3 | 4 |
Control Flow
Python has the usual for, while and if else statements.
Python does not use braces like most other languages, it’s indentation is controlled by whitespace and :.
This enforces at least some degree of readability on code.
In [17]:
1
4
9
16
In [18]:
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
In [19]:
1
two
I'm not sure
I'm not sure
List (and dict) comprehensions are a shortcut (or syntactic sugar) for ‘for’ loops. They allow sucinct, ‘pythonic’ code:
In [20]:
[1, 4, 9, 16, 25]
Functions
Functions are reusbale pieces of code, either built in or custom written. They
take arguments inside brackets and return
a result
In [21]:
3
In [22]:
In [23]:
Help on function mysum in module __main__:
mysum(a, b)
This is the help (docstring), to see this type help(mysum)
In [24]:
3
Methods
Methods are functions, which are particular to a certain data type. For example, a list has different methods than a set. They are called using a .method() after your variable name
In [25]:
1
In [26]:
Help on built-in function pop:
pop(...) method of builtins.set instance
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
In [27]:
[1, 2, 3, 4, 5]
In [28]:
[5, 4, 3, 2, 1]
Objects
An object is a data type, either built in or one which we can custom define. All items in Python are objects.
In [29]:
In [30]:
(1, 2)
In [31]:
mytext
Useful Modules
There are a wide range of modules that are useful to data scientists that we have not had time to cover in class.
Here are some of my favourites:
SymPy - symbolic mathematics
Bokeh - interactive in browser plotting
d3Py - D3.js based plots
json - JSON data (Standard library)
PyMongo - mongoDB
Flask - Web Dev
Django - Web Dev
eve - API construction
BeautifulSoup - Web scraping
More generally, if you would like to do something in Python, try googling “[task] python” you will likely find a useful module or two.