Basic types#

Numerical types#

Floats:

c = 2.1
type(c)
float

Complex:

a = 1.5 + 0.5j
a.real
1.5
a.imag
0.5
type(1. + 0j)
complex

Booleans:

3 > 4
False
test = (3 > 4)
test
False
type(test)
bool
7 * 3.
21.0
2**10
1024
8 % 3
2

Type conversion (casting):

float(1)
1.0

Containers#

Lists#

colors = ['red', 'blue', 'green', 'black', 'white']
type(colors)
list

Indexing: accessing individual objects contained in the list:

colors[2]
'green'

Counting from the end with negative indices:

colors[-1]
'white'
colors[-2]
'black'

Warning

Indexing starts at 0 (as in C), not at 1 (as in Fortran or Matlab)!

Slicing: obtaining sublists of regularly-spaced elements:

colors
['red', 'blue', 'green', 'black', 'white']
colors[2:4]
['green', 'black']

Warning

Note that colors[start:stop] contains the elements with indices i such as start<= i < stop (i ranging from start to stop-1). Therefore, colors[start:stop] has (stop - start) elements.

Slicing syntax: colors[start:stop:stride]

colors
['red', 'blue', 'green', 'black', 'white']
colors[3:]
['black', 'white']
colors[:3]
['red', 'blue', 'green']
colors[::2]
['red', 'green', 'white']

Lists are mutable objects and can be modified:

colors[0] = 'yellow'
colors
['yellow', 'blue', 'green', 'black', 'white']
colors[2:4] = ['gray', 'purple']
colors
['yellow', 'blue', 'gray', 'purple', 'white']

Note

The elements of a list may have different types:

colors = [3, -200, 'hello']
colors
[3, -200, 'hello']
colors[1], colors[2]
(-200, 'hello')

Add and remove elements:

colors = ['red', 'blue', 'green', 'black', 'white']
colors.append('pink')
colors
['red', 'blue', 'green', 'black', 'white', 'pink']
colors.pop() # removes and returns the last item
'pink'
colors
['red', 'blue', 'green', 'black', 'white']
colors.extend(['pink', 'purple']) # extend colors, in-place
colors
['red', 'blue', 'green', 'black', 'white', 'pink', 'purple']
colors = colors[:-2]
colors
['red', 'blue', 'green', 'black', 'white']

Reverse:

rcolors = colors[::-1]
rcolors
['white', 'black', 'green', 'blue', 'red']
rcolors2 = list(colors) # new object that is a copy of colors in a different memory area
rcolors2
['red', 'blue', 'green', 'black', 'white']
rcolors2.reverse() # in-place; reversing rcolors2 does not affect colors
rcolors2
['white', 'black', 'green', 'blue', 'red']

Concatenate and repeat lists:

rcolors + colors
['white',
 'black',
 'green',
 'blue',
 'red',
 'red',
 'blue',
 'green',
 'black',
 'white']
rcolors * 2
['white',
 'black',
 'green',
 'blue',
 'red',
 'white',
 'black',
 'green',
 'blue',
 'red']

Sort:

sorted(rcolors) # new object
['black', 'blue', 'green', 'red', 'white']
rcolors
['white', 'black', 'green', 'blue', 'red']
rcolors.sort()  # in-place
rcolors
['black', 'blue', 'green', 'red', 'white']

Methods and Object-Oriented Programming

The notation rcolors.method() (e.g. rcolors.append(3) and colors.pop()) is our first example of object-oriented programming (OOP). Being a list, the object rcolors owns the method function that is called using the notation .. No further knowledge of OOP than understanding the notation . is necessary for going through this tutorial.

Discovering methods:

Reminder: in Ipython: tab-completion (press tab)



rcolors.<TAB>
                 append()  count()   insert()  reverse()
                 clear()   extend()  pop()     sort()
                 copy()    index()   remove()

Strings#

Different string syntaxes (simple, double or triple quotes):

s = 'Hello, how are you?'
s = "Hi, what's up"
s = '''Hello,
       how are you'''         # tripling the quotes allows the
                              # string to span more than one line
s = """Hi,
what's up?"""

However, if you try to run this code:

'Hi, what's up?'

— you will get a syntax error. (Try it.) (Why?)

This syntax error can be avoided by enclosing the string in double quotes instead of single quotes. Alternatively, one can prepend a backslash to the second single quote. Other uses of the backslash are, e.g., the newline character \n and the tab character \t.

Indexing:

a = "hello"
a[0]
'h'
a[1]
'e'
a[-1]
'o'

Slicing:

a = "hello, world!"
a[3:6] # 3rd to 6th (excluded) elements: elements 3, 4, 5
'lo,'
a[2:10:2] # Syntax: a[start:stop:step]
'lo o'
a[::3] # every three characters, from beginning to end
'hl r!'

A string is an immutable object and it is not possible to modify its contents. One may however create new strings from the original one.

a = "hello, world!"
a[2] = 'z'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[46], line 2
      1 a = "hello, world!"
----> 2 a[2] = 'z'

TypeError: 'str' object does not support item assignment
a.replace('l', 'z', 1)
'hezlo, world!'
a.replace('l', 'z')
'hezzo, worzd!'

String formatting:

'An integer: %i; a float: %f; another string: %s' % (1, 0.1, 'string') # with more values use tuple after %
'An integer: 1; a float: 0.100000; another string: string'
i = 102
filename = 'processing_of_dataset_%d.txt' % i   # no need for tuples with just one value after %
filename
'processing_of_dataset_102.txt'

Dictionaries#

tel = {'emmanuelle': 5752, 'sebastian': 5578}
tel['francis'] = 5915
tel
{'emmanuelle': 5752, 'sebastian': 5578, 'francis': 5915}
tel['sebastian']
5578
tel.keys()
dict_keys(['emmanuelle', 'sebastian', 'francis'])
tel.values()
dict_values([5752, 5578, 5915])
'francis' in tel
True
d = {'a':1, 'b':2, 3:'hello'}
d
{'a': 1, 'b': 2, 3: 'hello'}

More container types

Tuples

Tuples are basically immutable lists. The elements of a tuple are written between parentheses, or just separated by commas:

t = 12345, 54321, 'hello!'
t[0]
12345
t
u = (0, 2)

Sets: unordered, unique items:

s = set(('a', 'b', 'c', 'a'))
s
{'a', 'b', 'c'}
s.difference(('a', 'b'))
{'c'}

Assignment operator#

Things to note:

  • A single object can have several names bound to it:

a = [1, 2, 3]
b = a
a
[1, 2, 3]
b
[1, 2, 3]
a is b
True
b[1] = 'hi!'
a
[1, 'hi!', 3]
  • to change a list in place, use indexing/slices:

a = [1, 2, 3]
a
[1, 2, 3]
a = ['a', 'b', 'c'] # Creates another object.
a
['a', 'b', 'c']
id(a)
140609110764160
a[:] = [1, 2, 3] # Modifies object in place.
a
[1, 2, 3]
id(a)
140609110764160
  • the key concept here is mutable vs. immutable

    • mutable objects can be changed in place

    • immutable objects cannot be modified once created

See also

A very good and detailed explanation of the above issues can be found in David M. Beazley’s article Types and Objects in Python.