We are focusing on Python 3 for most of the course, but for reference, I'll note when features became available for this set of slides only. Most things are applicable to both versions. Also, if using Python 2, include the following line:
from __future__ import print_function, division, absolute_import
Python 3 came out 10 years ago. Python 2 is dying in 2020. Packages are dropping support. Please convince everyone you work with to go to Python 3 if they can!
my_int = 3 # Python 2 has two integers, normal and long
my_float = 3.0
my_str = "hi" # Strings come in different flavors, like r"raw".
my_complex = 1j
my_bool = True
# None is it's own type. Odd.
my_list = [0, 1, 2, 3] # Can be changed later
my_tuple = (0, "anything") # Can't be changed later
my_dict = {"one": 1, "two": 2} # Order preserved in Python 3.6+
my_set = {1, 2, 3}
special_values = (None, True, False, Ellipsis)
_
[value]
[first:last:step]
:step
or either/both valueslast - first
...
means "all the rest"Overview of syntax:
:
for x in iterable: ...
listing = [x for x in iterable if True]
listing = list(iterable)
Common stumbling blocks:
x = [1, 2]
y = x
x[:] = [3, 4]
What is printed with print(x,y)
?
print(x, y)
x = [5, 6]
print(x, y)
Exhaustive and complete list:
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass async
break except in raise await
Python 2: add print
and exec
, remove nonlocal
Python before 3.7: remove async
and await
Every function Python 3.7 offers:
... | ... | ... | ... | ... |
---|---|---|---|---|
abs() |
dict() |
help() |
min() |
setattr() |
all() |
dir() |
hex() |
next() |
slice() |
any() |
divmod() |
id() |
object() |
sorted() |
ascii() |
enumerate() |
input() |
oct() |
staticmethod() |
bin() |
eval() |
int() |
open() |
str() |
bool() |
exec() |
isinstance() |
ord() |
sum() |
bytearray() |
filter() |
issubclass() |
pow() |
super() |
bytes() |
float() |
iter() |
print() |
tuple() |
callable() |
format() |
len() |
property() |
type() |
chr() |
frozenset() |
list() |
range() |
vars() |
classmethod() |
getattr() |
locals() |
repr() |
zip() |
compile() |
globals() |
map() |
reversed() |
__import__() |
complex() |
hasattr() |
max() |
round() |
breakpoint() |
delattr() |
hash() |
memoryview() |
set() |
You could even get this list in Python itself!
# All builtins are available in the builtins module
import builtins
# Iterate over the contents of the module
for item in dir(builtins):
# Items are strings; only look at the ones]
# that start with a lower case letter
if item[0] != item[0].upper():
print(item)
The if
statement:
x = True
if x:
print("x was true")
else:
print("x was not true")
The for each loop:
a = "abcdefg"
for item in a:
print(item)
You can also use for item in range(N)
to count over a range of values, or while CONDITION:
. You can use for
(and if
) inside []
, ()
, or {}
to build the data structures mentioned above inplace.
bad_way_to_make_a_set_of_tens = {x for x in range(100) if x % 10 == 0}
bad_way_to_make_a_set_of_tens
With lets you take code like this:
f = open(filename)
txt = f.read()
f.close() # Don't forget me!
and write instead:
with open(filename) as f:
txt = f.read()
# File automatically closed here!
With simply runs code at the start and at the end of a block. It also promises the code at the end runs, even if there's an error!
You can define your own functions:
def my_function(value, another=None):
if another is not None:
return another
else:
return value
print(my_function(2), my_function(3, 4), my_function(3, another=4))
def my_iterator():
for i in range(5):
yield i * 2
for j in my_iterator():
print(j)
Decorators are objects that can modify functions or classes:
@change_a_function
def something():
return 1
is the same as
def something():
return 1
something = change_a_function(something)
class MyFunctionLike(object):
def __init__(self, value=2):
self.value = value
def __call__(self, value):
return value * self.value
f2 = MyFunctionLike(2)
f3 = MyFunctionLike(3)
print(f2(2), f3(2))
object -> class -> parent class
Creating an object looks like this:
class.__new__
->
create objectobject.__init__
: prepare objectSpecial decorators:
@property
: Make a property function@classmethod
: Make a method runnable from the class@staticmethod
: Make a method not depend on anything in classclass MyTest(object):
@property
def x(self):
return 3
@staticmethod
def y():
return 2
print(MyTest.y(), MyTest().x)
Python knows everything about objects (basically)
def simple(x):
"This is help"
return x * 2
print(dir(simple))
help(simple)
import inspect
print(inspect.getsource(simple))
Signature details are available in Python 2, but are much more elegant in Python 3.
inspect.signature(simple)
inspect.getfile(inspect)
Errors are usually exceptions in Python.
def make_an_error():
raise RuntimeError("I'm an error!")
# Try uncommenting this
# make_an_error()
try:
make_an_error()
except RuntimeError as e:
print(e, "But I don't care, continuing anyway")
You can catch errors like ImportError
and then fix the problem, etc.
The only exception: segfault (C error) (but can still get some info about it in Python 3)
There are two blocks that also show up in Python: finally
and else
. Finally will run regardless of if an exception was caught or not, and else will run only if an exception was not caught. You can also use else
on a loop, where it only runs if break
was not used to exit the loop. (While we are at it, Python provides continue
to keep going in the loop as well)
for i in range(10):
print("Breaking out:")
# break
else:
print("Did not break out")
Folders and packages are intrinsically linked in Python.
import one
from two import three
from four.five import six
one
could be one.py
or one/__init__.py
three
could be in a package or another package__init__.py
from .one
are available inside a packagesys.path
(made from $PATH
and $PYTHONPATH
and the local dir)site-packages
To be installable:
pip install -e .
(recommended) or python setup.py develop
(not recommended)So why is Python 3 better and why is Python 2 still common?
TL;DR: Python 3 removes bad features and backward incompatible is slow when you have millions of lines of code.
See the great talk
*list
and **dict
, especially in 3.5first, *rest, last = range(10)
for i in range(1000000000000):
print("Your computer will die in Python 2!")
May require wrapping some functions with list
You can now yield from
another iterator (3.4)
"two" < 1
is now an error instead of the wrong answer!@
symbol was added for matrix multiplicationx = 3
print(f"This is {x} and x+1={x+1}")
print("This is {} and x+1={}".format(x, x + 1))
print("This is %i and x+1=%i" % (x, x + 1))
A new breakpoint builtin function was added to make breakpoints easier to set, and easier to customize.
As you could see, Python is simple. To do real work in Python, you need libraries.
The best library for Python: the Python standard library (included).
sys
, os
: system toolsre
: regular expressionsmath
, time
and friends, pathlib
(3 or backport), functools
, itertools
, pdb
, ...See PMOTW3 (or PMOTW2).
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
x = np.arange(100)
plt.plot(x, np.sin(x))
plt.show()
sympy
: Symbolic mathematics as Python objectsplumbum
: Set of tools for command line programming (calling other programs, command line arguments, paths, remote connections, color)numba
: Compile python into CPU, OpenMP, or CUDA codepytest
: Better testing for your code in pythonsetuptools
: The continued development of Python packagingtensorflow
: Google's computation engine designed for MLCython
: The C++ and Python mix for compiled python-like codeThe interactive language project, make up of:
This is the first project to drop support for Python 2!
IPython language extensions:
%line_magic
%%cell_magic
help?
!command
%debug
after an exception drops you into pdb
%timeit
will run a command multiple times and get the timing infoThe newest and shiniest member of the IPython/Jupyter family. It's a home for notebooks and other types of windows, all in a single browser window. The notebook has some nice updates, too, like drag-n-drop cells and collapsible input. Not ready for major sites yet, but good enough for you to run it on your own.
The Package Installer for Python, any package should be able to be installed with this.
getpip.py
before that)pip install a_package
pip install --user local_package
pip install --upgrade pip
pip install -e .
conda create -n my_python_27_env python=2.7 anaconda
source activate my_python_27_env
conda install paramiko
pip install plumbum