Python Tutorial: Basic data structures

Lists and Tuples (and Strings, again)
List literals
List access
list elements can be accessed via the square brackets operator.
List indices are 0-based, i.e. the first element has index 0, the second has index 1 etc.
Lists can be accessed backward with negative indices
When the index exceed the list size an error is generated
xxxxxxxxxx
["hi", "mom"][1] # returns "mom"
["thanks", "for", "all", "the", "fish"][-2] # returns "the"
[][1] # throws an exception
Splices
In order to show some result, starting from this paragraph I will use the
print
function. A more detailed description of functions will be presented later.
When applied to lists, the square bracket operator accepts splices, returning sublists.
A splice has two possible forms
- start : stop
- start : stop : step
- start will point to the beginning of the string
- end will point to the end of the string
- step will be 1
xxxxxxxxxx
print(['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'][1:4])
print(['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'][1:6:2])
print(['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'][:4])
print(['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'][4::2])
print(['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'][-3:9:2])
Accessing strings with index and splice
index and splice work in the very same way with strings as they do with lists
xxxxxxxxxx
print("Here we are, born to be Kings"[5])
print("Here we are, born to be Kings"[-3])
print("Here we are, born to be Kings"[:4])
print("Here we are, born to be Kings"[-5:])
print("Here we are, born to be Kings"[2:8:2])
Tuple literals
Tuple can contain ordered sequences of various kinds of objects, as list do
Tuple literal constructor is the comma, but as the empty tuple is represented by an empty parens () usually parens are always used in tuple literals for better readability
Indices and tuples also apply as in lists; the main difference with list is related to mutability, a theme I will explain later.
xxxxxxxxxx
print((True,"hi",3.14159,0+1j)[-1])
print((True,"hi",3.14159,0+1j)[2])
print((True,"hi",3.14159,0+1j)[:3])
Operators on lists and tuples
as we already saw with strings the + operator concatenates lists and tuples with similar containers (i.e. tuples can't be concatenated with lists and vice versa)
The * operator with an integer repeats the content of the sequence
xxxxxxxxxx
print(["This", "is", "not"] + ["America"])
print(("hi", "ho") * 3)
String formatting with modulo and tuples
the modulo operator accepts a string on the left side and a tuple or a list on the right side.
the result is equivalent to the c "sprintf" function: the string content will be interpolated with the content of the sequence; placeholders begin with a % sign and use a letter code to define the expected type of datum. Here is an incomplete list
Between the % sign and the letter some combination of digits and symbols can modify the output; here are some example: please refer to a printf manual for more details
sequence | data type |
---|---|
%s | any object |
%d | integers |
%f | numbers (fixed point format) |
%e | numbers (scientific format) |
xxxxxxxxxx
print("|%10s| and |%-10s| space padding" % ("positive", "negative"))
print("fixed point %.4f and scientific %.4e modifiers" % (3.14159, 3.14159 / 1000))
Dictionaries and Sets (and more Strings)
Set iterals
Sets are containers which behave as math sets:
lists are not allowed to be set values while tuples are. This is related to their immutability as we will see later
- they contain just one copy of each value
- it is possible to efficiently test if a value belongs to the sets
xxxxxxxxxx
{} # an empty set
{"Hi"} # a set with only one item
print({2,2,3,1,"Joe"}) # duplicate item in literals will be dropped
Dictionary literals
dictionaries or maps associate keys with values.
As their literal constructor is a list of key-value pairs; each pair is divided by a colon and the list is surrounded by curl braces
As with other containers there is no restriction to use different types of objects in the same container.
Lists are not valid keys while tuples are (as with set contents).
xxxxxxxxxx
{"hello":1, 10:True, (1,2,3,4):3.14159}
Dictionary access
Set and dictionary operators
the
while this operator also works on tuples and lists its time complexity is linear while it is constant on dictionaries and sets, so it is not recommended to use it with them.
in
operator can check if an element is part of a set or if there is a key in a dictionary
xxxxxxxxxx
print(2 in {10, "Joe", 2})
print(2 in {"hello":1, 10:True, (1,2,3,4):3.14159})
String formatting with modulo and dictionaries
Dictionaries can be used as the right operand in string formatting expressions with the modulo operator.
This can be useful when
- formatting strings with many data without worrying about order
- rusing the same value multiple times
xxxxxxxxxx
print("on %(date)s the temperature is %(temperature).2f degrees" % {"temperature":2.3, "date":"Monday, January 1st"})
print("My name is %(surname)s, %(first name)s %(surname)s" % {"first name":"James", "surname":"Bond"})