Variables II: Containers
[outline]
In this part we look at how to store multiple items in containers. Containers are probably the most important data structures in Python; there are a number of easy-use functions to work with containers for processing.
First up, we'll look at tuples, a collection for storing objects that can't change.
Tuples (powerpoint)
Further info:
Immutable types are often more efficient as the computer can predetermine processing requirements, but they generally aren't as friendly to use as mutable types. We'll look at the most common immutable types: tuples and strings, but if you're interested in the others, here's more information on:
For more on the super-efficient by restrictive array type objects, see the documentation.
Quiz: In the following code from the Python tutorial, the order of execution of the last line is _____________________
a, b = 0, 1
while b < 10:
print(b)
a, b = b, a+b
- a = b; b = a+b
- b = a+b; b = a
- all right of the equals is calculated and then assigned to the left.
This is a tricky one, and worth testing by running the code with a, b = b, a+b
and b, a = a+b, b
. In fact, if you think
about it as a tuple packing and unpacking operation, it should be clear that the right expressions are calculated first and then assigned to the
variables on the left. We certainly don't want to potentially alter a
before a+b
is calculated, or we might as well just say
b+b
.
Having looked at the core immutable type and how data in it is accessed, let's now look at the core mutable type, and what it adds to the pot.
Lists (powerpoint)
Further info:
Command line arguments are key to how a lot of programs work. For example, when you double-click a word file in Windows, Word is run with the doc's file name as a command line argument.
You can make your own file associations processed using Python easily in Windows. You'll need a Python file that processes sys.argv
, a
.bat
file to run it, and a file with a file extension you're not likely to use for anything else. Here's an example Python file:
filename.py you'll see it just prints the first argument (after the current script name) loads of times.
Here's a bat file: piiiie.bat. If you open it up, you'll see it runs the
python file, and passes in %1
as a command line argument: this is the DOS representation of the first argument passed to this script.
Finally, here's a file with an extension you probably won't use: myfile.piiiie.
What's going to happen is that when we double click the file, we're going run the bat file. Windows will automatically pass the bat file the name of the
file we've clicked. The bat file will then pass this info to the Python file (why can't we run the Python file directly?). To do this, right-click the
.piiie
file, and select Open with and Choose another app. In the list of apps, make sure Always use this app to open .piiie files? is
selected, then click More apps. Scroll down to Look for another app on this PC and navigate to where you've saved piiiie.bat. Select it and push the Open
button. Whenever you now click on a file with the extension .piiie it should now run it through filename.py.
Quiz: In the following code a[0][0] = ________________________________________
a = ([2],[3])
a[0] = 4
- 4
- [2]
- nothing, the program would break as tuples are immutable
Correct! This is another tricky one. So, a
is a tuple, and therefore immutable with regards its objects, but it's a tuple containing lists so that suggests we can
change the 2
to a 4
. However, that's not what we're doing. Notice we have a 2D container, but we only give one dimension
a[0]
; therefore we're replacing the whole of the [2]
list with the number 4
, not its content. We're therefore back in the
situation of tuples not being mutable with regards the objects they store. If we wanted to replace the contents of the mutable list, we should have done
a[0][0] = 4
. It's this kind of mistake that's very easy to make.
Ok, let's wrap up by looking at the other major containers: Strings, Sets, and Dicts.
Other containers (powerpoint)
Further info:
Note that there are some cases where we want to display the escape characters in strings when we print or
otherwise use the text. To do this, prefix the literal with "r":
>>> a = r"This contains a \\ backslash escape"
Note that the escape is escaped.
>>> a
'This contains a \\\\ backslash escape'
String literal markups:
R
or r
is a "raw" string, escaping escapes to preserve their appearance.
F
or f
is a formatted string (we'll come to these).
U
or u
is Python 2 legacy similar to R.
Starting br
or rb
or any variation capitalised - a sequence of bytes.
Note that print
inserts spaces when comma separated.
By default print ends with a newline. But this can be overridden (best in scripts):
print ("a", end=",")
print ("b", end=",")
print ("c")
prints:
a,b,c
There are an inordinate number of ways of formating strings in Python; the community is continually picking away at it like an itchy scab. Here's a brief summary of the major ones. Of these, we'd recommand for the moment:
str.format (info)
Example:
a = "Bob"
b = 2.23333
print( "{0} has: {1:10.2f} pounds".format(a,b) )
Output:
Bob has: 2.23 pounds
The markup for this can be found in the documentation.
Quiz: The code below prints ________________________________________
daisy_bell = "Daisy, Daisy/Give me your answer, do./I'm half crazy/all for the love of you..."
i = daisy_bell.find("/")
if (i > 0):
daisy_bell = daisy_bell.replace("/", "\n")
print (daisy_bell)
- Daisy, Daisy/Give me your answer etc...
- Daisy, Daisy\nGive me your answer etc...
- Daisy, Daisy
Give me your answer etc...
Correct! This code replaces the forward slashes traditionally used to delimit lines of poetry etc. with line breaks, which show as proper line breaks when printed. Daisy Bell has a long traditional association with computing.
[Key ideas from this part]