General Information:
Python is a scripting language
Probably the simplest "Hello world" ever:
print "Hello world!"
ipython [--pylab] [script]
--pylab
option: support for plotting and vector operationsGive it a try!
print 'My first python print'
Some built-in types (those that matter for our course):
a = 10 # integer (typically C long)
b = 2.5 # real-valued numbers (typically C double)
c = 'A string with "quotes"'
d = "A string with 'apostrophes'"
e = True # A true boolean
f = False # A false boolean
g = None # Nothing (like "null" in Java)
Arithmetic operators:
a = 10 + 2 - 4 # sum and difference
b = 10 + (2 - 4) # change of priority (parantheses)
c = 10 * 2 # product
d = 10 / 3 # integer division
e = 10 % 3 # modulus (remainder of integer division)
f = 10 / 3.0 # real division
g = 10**2 # power
h = abs(-3.4) # absolute value
i = floor(3.4) # floor rounding
j = ceil(3.4) # ceil rounding
...
Logical operators:
a = v and w # Logical "and"
b = v or w # Logical "or"
c = not v # not
Comparison operators:
a = 5 < 10 # Less than
b = 5 <= 10 # Less than or equal
c = 5 == 10 # equal
e = 5 >= 10 # Greater than or equal
f = 5 > 10 # Greater than
g = 5 <= 7 <= 10 # Newbie error in C, works in python!
String formatting (C-style):
one = 1
two = 2.0
three = one + two
print 'And %d + %f = %.2f' % (one, two, three);
%d
prints an integer%f
prints a real%.2f
prints a real with two decimals% (one, ...)
specifies the source for each fieldLists (mutable sequences):
a = ['a', 'is', 'a', 'list']
b = ['a', 1, True] # a list with multiple types
c = [1, 2, 3]
a[0] = 'b' # indexing (traditional)
print a[-1] # backwards indexing
a.append('another element') # append an element
a.pop() # pop last element
Lists have several other methods
Want to know which ones?
First, built a list:
In [1]: a = [1, 2, 3]
Then, use tab completion:
In [2]: a.[HIT TAB]
a.append a.count a.extend a.index...
Pick you favorite method and add a "?":
In [2]: a.count?[HIT ENTER]
And get some help!
Docstring: L.count(value) -> integer -- return ...
Type: builtin_function_or_method
...
Tuples (immutable sequences):
a = ('a', 'is', 'a', 'tuple')
b = ('a', 1, True) # a tuple with multiple types
a[0] = 'b' # ERROR!!! Tuples are immutable
a = ('b', a[1], a[2], a[3]) # This works
print a[-1] # backwards indexing
print len(a) # number of elements
Tuple "superpowers" :-)
# tuple assignment
a, b = 10, 20
# tuple unpacking
c = (1, 2, 3)
d, e, f = c
# works with every iterable sequence!
d, e, f = [1, 2, 3]
# tuple printing
print d, e, f # automatic separator: space
There are a few other methods.
Dictionaries (i.e. maps):
a = {'name':'gigi', 'age':23} # key:value pairs
b = {'name':'gigi', 23:'age'} # keys of different types
# a key can be of any immutable type
c = {(0, 1):'a tuple as key!'}
print a['name'] # prints 'gigi'
print len(a) # number of items
print a.keys() # list of keys
print a.values() # list of values
print a.items() # list of (key, value) tuples
Many other methods!
Sets (with unique elements):
a = [1, 1, 2, 2, 3, 3]
b = set(a) # returns {1, 2, 3}
c = {1, 1, 2, 2, 3, 3} # builds {1, 2, 3}
b.add(4) # add an element
b.remove(3) # remove an element
print len(b) # number of elements
Other methods, as usual...
Special operators for collections:
a = [1, 2, 3] + [4, 5] # list concatenation
b = (1, 2, 3) + (4, 5) # tuple contatenation
print sum(a) + sum(b) # sum (lists and tuples)
print 2 in a # membership testing (any collection)
c = {'a':1, 'b':2}
print 'a' in c # "in" looks for keys in dictionaries
print 1 in c # so this is False
print 1 + 2 + 3 + 4 + 5 + 6 + 7 + 9 + 10 + 11 + 12 + \
13 + 14 # Use "\" to break a line
print (1 + 2 + 3 + 4 + 5 + 6 + 7 + 9 + 10 + 11 + 12 +
13 + 14) # no need to do that in parentheses
Conditional branches:
b = 10
if -1 <= a <= 1: # no parentheses needed
b *= a;
print 'b = %f' % b # no need for parantheses
# with a single term
elif a > 1: # "elif"/"else" are optional
print 'Big number' # diff. block, diff. indentation
else:
print 'Small number'
if
lineif x > 0: print x
"For" loops:
for x in ['first', 'second', 'third']:
print x
for i in range(10): # range returns [1,..., 9]
if i == 0: continue # continue, as in C
if i > 5: break # break, as in C
print i
# enumerate returns a list of (index, item) tuples
# they can be "separated" via tuple unpacking
for i, x in enumerate(['three', 'item', 'list']):
print i, x
"For" loops:
# "zip" returns a list of tuples
# range(x, y) = list of integers between x and y-1
for x, y in zip(range(5), range(5, 10)):
print x, y # "0 5" "1 6" "2 7" ...
While loops:
a = 10
while a > 0:
print a
a -= 1 # decrement (no ++ or -- operators)
A syntactical construct to build data structures on the fly
# list comprehension
a = [i**2 for i in range(5) if i % 2 == 0]
# set comprehension
b = {i**2 for i in range(5) if i % 2 == 0}
# dictionary comprehension (<key>:<expr>)
c = {i : i**2 for i in range(5) if i % 2 == 0}
# multiple levels
a = {(i,j) : 0 for i in range(10)
for j in range(10)}
General pattern (lists used as example):
<list comprehension> ::= [<generator expression>]
<generator expression> ::= <expr>
{for <var> in <collection>
[if <condition>]}
Function definition:
# Simple function
def f1(x):
return x**2;
# Function within function
def f3(x):
a = 10
def f4(x): # scope includes that of f3
return x*a; # f4 can access local vars of f3
return f4(x)
Functions with named arguments (and default values):
def the_answer_is(res = 42):
return res
print the_answer_is() # prints 42
print the_answer_is(13) # prints 13
print the_answer_is(res = 3) # prints 3
Functions are objects!
def transform(x, f):
return [f(v) for v in x]
def g(x):
return 2*x;
transform([1, 2, 3], g)
%paste
command in ipythonFunctions as objects, an important case:
voti = [('gigi', 20), ('gianni', 30), ('gino', 24)]
def f(t): return -t[1]
# sorted returns a sorted copy of the collection
# key = score function to be used for sorting
for name, score in sorted(voti, key=f):
print name # prints gianni, gino, gigi
Same as before, but more compact version:
voti = [('gigi', 20), ('gianni', 30), ('gino', 24)]
# sorted returns a sorted copy of the collection
# key = score function to be used for sorting
for name, score in sorted(voti, key=lambda t: -t[1]):
print name # prints gianni, gino, gigi
Where:
lambda t: -t[1]
is a nameless function.
For now, we need to know only two things:
import <module name>
A software suite for solving combinatorial problems
Web site: https://developers.google.com/optimization/?hl=en
Several tools:
However:
In the lab, we have installed the or-tools CP solver
You can install Google or-tools at home
In brief:
About obtaining Python:
On Linux (e.g. Ubuntu):
On OS X:
On Windows:
Google or-tools Documentation is quite scarce
Luckily, the API reference is good enough
For us, the key parts of the API can be found at:
And:
Let's see some golden rules for C++/Python translation...
Solver API:
MakeIntVar
becomes IntVar
IntVar API:
Download & unzip the start-kit associated to this lecture
lab01-ortools-ref.py
contains a very simple CSP modeled and solved with or-toolspython lab01-ortools-ref-py
Or with:
ipthon
run lab01-ortools-ref-py
The script should print a list of solutions
Let's see the structure of an or-tools model...
First, we have to import the or-tools module:
from ortools.constraint_solver import pywrapcp
Then we have to build an instance of a solver object:
slv = pywrapcp.Solver(model_name)
Then we can build variables:
x1 = slv.IntVar(min, max, name) # API 1
x2 = slv.IntVar(values, name) # API 2
For building constraints, the python API offers a trick:
+, -, <, ==, ...
) have been redefined+, -, *, ...
" build an expression<, <=, ==, >=, >, !=
" build a constraintSo we can build constraints like:
x + 2 <= y
x != y
x = 2*y - 3
Once a constraint has been built, it must be added to the solver:
svl.Add(constraint)
Then, this part of the script defines how to do search:
decision_builder = slv.Phase(all_vars,
slv.INT_VAR_DEFAULT,
slv.INT_VALUE_DEFAULT)
Finally, we have the code to:
slv.NewSearch(decision_builder)
while slv.NextSolution():
# here we can print the solution data, e.g.
print 'The value of x is %d' % x.Value()
slv.EndSearch()
Problem 1: Map Coloring
lab01-ex1.py
Problem 2: A Puzzle by Lewis Carrol
Problem 2: A Puzzle by Lewis Carrol
lab01-ex2.py