Wikipedia has this to say:
Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.
We will use 3.4+
$ python3 myscript.py
$ python myscript.py
$ python2 myscript.py
python
should point to 2.x (but might not)Add one of the following to the top of the script
#!/usr/bin/env python3
#!/usr/bin/python3
Run as
$ ./myscript.py
$ python3
)print('Hello World!')
Hello World!
for i in range(5):
if i % 2 == 0:
print(i, 'is even')
else:
print(i, 'is odd')
0 is even 1 is odd 2 is even 3 is odd 4 is even
for i in range(5):
if i % 2 == 0:
print(i, 'is even')
else:
print(i, 'is odd')
Most simple data types are built in
True
and False
)x = 1
y = 2.0
s = "Strings are unicode, so we can write in 日本語"
b = True
z = int(y)
z, type(z)
(2, int)
5 ** 2
25
and
, or
, not
5 > 3 and not 5 > 7
True
x = 5
3 < x < 7
True
list
tuple
dict
set
list
¶a = [1, 2, 3, 4]
print(a[0], a[3], a[-1])
1 4 4
a.append(100)
a.remove(2)
print(a)
[1, 3, 4, 100]
b = [1, 2.0, 'banana', [30, 40]]
in
'banana' in b
True
tuple
¶append
, remove
or altering valueslist
)a = [1, 2, 3, 4]
t = (1, 2, 3, 4) # tuple
a[2] = 88 # OK
t[2] = 88
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-22-ba2b2f1f2cb7> in <module>() 3 a[2] = 88 # OK 4 ----> 5 t[2] = 88 TypeError: 'tuple' object does not support item assignment
By using
len(seq)
- number of items in seq
range(N)
- generate sequence of integers in half-open interval [0, N)
fruits = ['apple', 'banana', 'kiwi']
for i in range(len(fruits)):
print(fruits[i])
apple banana kiwi
We use for item in sequence
to grab items from sequences like list
or tuple
fruits = ['apple', 'banana', 'kiwi']
for fruit in fruits:
print(fruit)
apple banana kiwi
seq[a:b]
gives the part of the sequence in the half-open range [a, b)
fruits = ['oranges', 'apple', 'banana', 'kiwi', 'raspberry']
fruits[:2]
['oranges', 'apple']
fruits[2:]
['banana', 'kiwi', 'raspberry']
fruits[-3:]
['banana', 'kiwi', 'raspberry']
We can also specify the step length (which can be negative!)
fruits[1::2]
['apple', 'kiwi']
fruits[-1::-2]
['raspberry', 'banana', 'oranges']
Any sequence can be unpacked into separate variables
person = ('Hannes', 31, 'CVL')
name, age, workplace = person
print(name, 'is', age, 'years old and works at', workplace)
Hannes is 31 years old and works at CVL
We can also just get some of the values
name, _, workplace = person
print(name, workplace)
Hannes CVL
name, *rest = person
print(name, rest)
Hannes [31, 'CVL']
eating_habits = [('monkey', 'bananas'),
('horse', 'grass'),
('human', 'hamburgers')]
for animal, food in eating_habits:
print('The', animal, 'eats', food)
The monkey eats bananas The horse eats grass The human eats hamburgers
This is equivalent to the less pretty
for item in eating_habits:
print('The', item[0], 'eats', item[1])
The monkey eats bananas The horse eats grass The human eats hamburgers
set
¶things = {5, 2, 1, 1, 1, 1, 'monkey', (1, 3)}
print(things)
{1, 2, (1, 3), 5, 'monkey'}
in
5 in things
True
We can add and remove things from sets
things.add(7)
things.remove('monkey')
things
{1, 2, (1, 3), 5, 7}
A = {1, 2, 3, 4, 5}
B = {1, 4, 10, 20}
print('Intersection:', A.intersection(B))
print('Union:', A.union(B))
print('Difference:', A - B)
Intersection: {1, 4} Union: {1, 2, 3, 4, 5, 10, 20} Difference: {2, 3, 5}
Only hashable types work. E.g. not list
objects
A.add([1, 2])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-40-4cd20301fb85> in <module>() ----> 1 A.add([1, 2]) TypeError: unhashable type: 'list'
dict
¶tuple
is ok, but not list
!)country_area = {
'Sweden' : 449964,
'Italy' : 301338, }
print('The area of Sweden is', country_area['Sweden'], 'km²')
The area of Sweden is 449964 km²
New values/keys can be inserted after creation
country_area['Germany'] = 357168
print(country_area)
{'Italy': 301338, 'Germany': 357168, 'Sweden': 449964}
dict.keys()
and dict.values()
dict
is an unordered containerprint(country_area.keys())
print(country_area.values())
dict_keys(['Italy', 'Germany', 'Sweden']) dict_values([301338, 357168, 449964])
Check for key existance with in
'Sweden' in country_area
True
Get key-value pairs using dict.items()
for country, area in country_area.items():
print('The area of', country, 'is', area, 'km²')
The area of Italy is 301338 km² The area of Germany is 357168 km² The area of Sweden is 449964 km²
dict
as ad-hoc "classes" for structured data¶movies = [
{'name' : 'Star Wars',
'year' : 1977,
'actors' : ['Mark Hamill', 'Harrison Ford']},
{'name' : 'Alien',
'year' : 1979,
'actors' : ['Sigourney Weaver',]}
]
for movie in movies:
print(movie['name'], 'was released in', movie['year'])
Star Wars was released in 1977 Alien was released in 1979
def square(x):
return x ** 2
square(4)
16
Functions are like any object, and can be used as input to other functions
def apply_func(func, x):
return func(x)
f = square
apply_func(f, 3)
9
A function can return multiple values
def square_and_cube(x):
return x ** 2, x ** 3
square, cube = square_and_cube(4)
print(square, cube)
16 64
This is just creating and unpacking a tuple!
y = square_and_cube(5)
print(y, type(y))
(25, 125) <class 'tuple'>
def greet(name, greeting='Hello'):
print(greeting, name)
greet('Hannes')
greet('Hannes', greeting='Hi')
greet('Hannes', 'Hi')
Hello Hannes Hi Hannes Hi Hannes
args
will be a tuple of valuesdef add(*args):
result = 0
for x in args: # Not *args!
result += x
return result
add(1, 2, 5, 9)
17
kwargs
will be a dictionarydef print_thing_length(**kwargs):
for name, length in kwargs.items(): # Not **kwargs
print(name, 'is', length, 'tall')
print_thing_length(hannes='182 cm', smurf='two apples')
smurf is two apples tall hannes is 182 cm tall
When is this useful?
def sum_all_the_things(a, b, *args, foo=5, bar=10, **kwargs):
result = a + b + foo + bar
for x in args:
result += x
for x in kwargs.values():
result += x
return result
sum_all_the_things(1, 0, 0, 0, 0, 0, monkey=100)
116
%
¶printf
-like format specifiers: '%s (%d) has an IMDB score of %.1f' % ('Alien', 1979, 8.5)
'Alien (1979) has an IMDB score of 8.5'
str.format()
¶'{movie} ({year}) has an IMDB score of {imdb:.2f}'.format(movie='Alien',
year=1979,
imdb=8.5)
'Alien (1979) has an IMDB score of 8.50'
'{} ({}) has an IMDB score of {:.1f}'.format('Alien', 1979, 8.5)
'Alien (1979) has an IMDB score of 8.5'
movies = [
{
'name' : 'Star Wars',
'year' : 1977,
'imdb' : 8.7
},
{
'name' : 'Alien',
'year' : 1979,
'imdb' : 8.5
},
{
'name' : 'The Terminator',
'year' : 1984,
'imdb' : 8.1
},
{
'name' : 'House of the Dead',
'year' : 2002,
'imdb' : 2.0
},
]
Batteries included!
def best_movie(movielist):
best = max(movielist, key=lambda movie: movie['imdb'])
return best['name'], best['imdb']
movie, score = best_movie(movies)
print("The best movie is '{}' with a score of {:.1f}".format(movie, score))
The best movie is 'Star Wars' with a score of 8.7
from math import pi
class Circle:
name = 'Circle'
def __init__(self, radius):
self.radius = radius
def area(self):
return pi * self.radius ** 2
c = Circle(2.0)
print('A circle with radius {} has area {:.2f}'.format(c.radius,
c.area()))
A circle with radius 2.0 has area 12.57
__init__
is called when the object is initialized (a "constructor")self
$\approx$ this
but is explicit__init__
, but don't!object
is the top level object (can be omitted like previous example)class Shape(object):
def print_info(self):
print('I am a {} with area {:.2f}'.format(self.shape_type,
self.area()))
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
self.shape_type = 'Circle'
def area(self):
return pi * self.radius ** 2
c = Circle(2.0)
c.print_info()
I am a Circle with area 12.57
public
and private
??!¶'_'
or '__'
can be used to signal "privateness"class A:
def __init__(self):
self._my_private = 10
a = A()
a._my_private # Hmm??
10
class A:
def __init__(self):
self.__really_private = 10
a = A()
a.__really_private
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-63-9cf669e59029> in <module>() 4 5 a = A() ----> 6 a.__really_private AttributeError: 'A' object has no attribute '__really_private'
a._A__really_private
10
We can never make something private. But we can give hints.
super()
¶class CircleWithSquareHole(Circle):
def __init__(self, radius, hole_side):
super().__init__(radius) # calls Circle.__init__(self, radius)
self.side = hole_side
def area(self):
circle_area = super().area() # calls Circle.area(self)
return circle_area - self.side ** 2
cwsh = CircleWithSquareHole(2, 1)
cwsh.area()
11.566370614359172
try
- except
- finally
blockstry:
y = 5 / 0
except ZeroDivisionError:
print('Oh no! Divide by zero!')
y = 0
finally:
print('This always executes')
print('y =', y)
Oh no! Divide by zero! This always executes y = 0
DEFAULT_VALUE = 1
d = {'a' : 10, 'b' : 20}
if 'c' in d:
x = 5
else:
x = DEFAULT_VALUE
print(x)
1
is equivalent to
try:
x = d['c']
except KeyError:
x = DEFAULT_VALUE
print(x)
1
try:
f = open('/tmp/thisfilereallydoesnotexist', 'r')
data = f.read()
except IOError:
print('Failed to open and read data from file')
Failed to open and read data from file
Compare to these pre-checks
Catch multiple exceptions
try:
x = some_calculation()
except (ZeroDivisionError, ValueError):
x = 0
Catch everything (avoid if possible). Why?
try:
x = some_calculation()
except:
x = 0
lambda
functions¶Short single statement anonymous functions
add = lambda a, b: a + b
add(3, 4)
7
alist = [(1, 2), (2,0), (0, 10)]
sorted(alist, key=lambda x: x[0] ** 2 + x[1] ** 2)
[(2, 0), (1, 2), (0, 10)]
squares = [x ** 2 for x in range(10)]
print(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
vehicles = [('SAAB 9-5', 'car'),
('Titanic', 'boat'),
('Tesla model S', 'car'),
('Atlantis', 'spaceship')]
cars = [name for name, vtype in vehicles if vtype == 'car']
print(cars)
['SAAB 9-5', 'Tesla model S']
result = [expr(item) for item in sequence
if condition(item)]
files
is a list of filenames in a directory (not only images!)image_
(e.g. image_000.png
)images = [cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
for image in
[cv2.imread(fn) for fn in files
if fn.startswith('image_')
]
]
dict
¶We can do the same for dict
s
square_lookup = {x : x ** 2 for x in range(10)}
square_lookup[7]
49
open()
to create a File
object.f = open('/etc/redhat-release', 'r') # Read-only
data = f.read()
print('File contents:')
print(data)
f.close()
File contents: Fedora release 22 (Twenty Two)
File
objects¶read()
, write()
readlines()
, writelines()
f = open('/etc/passwd', 'r')
lines = f.readlines()
for l in lines[:3]:
print(l)
f.close()
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
Why the extra linebreaks?
data = open('/etc/redhat-release', 'r').read()
or use a context manager, using with
with open('/etc/redhat-release', 'r') as f:
data = f.read()
The file is automatically closed after the with
finishes
s = 'The quick brown fox jumps over the lazy dog'
words = s.split()
words[3]
'fox'
s.upper()
'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG'
' extra whitespace '.strip()
'extra whitespace'
A list of strings can be turned to a string with str.join()
.
print('My favourite fruits are', ', '.join(fruits[:3]))
My favourite fruits are oranges, apple, banana
import
import time
print(time)
print('Current POSIX time:', time.time())
<module 'time' (built-in)> Current POSIX time: 1446481872.8159316
We can also import only specific names from a module using from ... import ...
from calendar import isleap, leapdays
isleap(1984)
True
help(obj)
launches a help section for some object "obj", which can be an object instance, a class, a module, function, ...dir(obj)
lists all attributes of an objectrepr(obj)
returns the represenation string for an objectdef do_duck_things(duck):
print("I'm a duck that goes", duck.quack())
class Duck:
def quack(self):
return 'quack!'
donald = Duck()
do_duck_things(donald)
I'm a duck that goes quack!
class DisguisedCat:
def quack(self):
return 'meuw-ack!'
barry = DisguisedCat()
do_duck_things(barry)
I'm a duck that goes meuw-ack!
If it walks like a duck, and talks like a duck ... then it's a duck!