Commit 18209b24 authored by Holger Dinkel's avatar Holger Dinkel
Browse files

Revert "change all printing functions to version 3; add import from future"

This reverts commit 0260a392.
parent 0260a392
......@@ -39,11 +39,10 @@
In the window image above, the `In[1]` is the Python prompt and it should have a flashing cursor. As is traditional under these circumstances, the first thing you should do is get Python to print out the text “Hello, World”. To do this, type the command below at the prompt and press return (control+return in the notebook).
%% Cell type:code id: tags:
``` python
from __future__ import print_function # This statement ensures that the print function behaves the same in Python version 2 and 3
print('Hello, World')
```
%% Cell type:code id: tags:
......@@ -420,11 +419,11 @@
```
%% Cell type:code id: tags:
``` python
print(s)
print s
```
%% Cell type:markdown id: tags:
Again, you can just type the variable name or use it in a `print` statement, but what makes a variable containing a string more interesting is that it is one of Python’s object data types. This means that it doesn’t just passively hold the value of the string, it also knows about things you can do to the string. For example:
......
......@@ -16,26 +16,20 @@
%% Cell type:markdown id: tags:
Using an editor instead of the shell allows you to quickly go back and change code that you've already written, which can make it easier to correct typos, add additional lines, and 'debug' your script to help figure out where an error or unwanted behaviour is occurring. Although you can use the command history at the shell prompt to access your previous lines of code, it is often easier to keep your scripting separate from the output. Later, we will see an example of where using an editor is really useful.
%% Cell type:code id: tags:
``` python
from __future__ import print_function # This statement ensures that the print function behaves the same in Python version 2 and 3
```
%% Cell type:markdown id: tags:
Start by entering the following code:
%% Cell type:code id: tags:
``` python
shopping = ['bread', 'potatoes', 'eggs', 'flour', 'rubber duck', 'pizza', 'milk']
for item in shopping:
print(item)
print item
```
%% Cell type:markdown id: tags:
This is a very simple program, which creates a variable (`shopping`) that refers to a list and then prints out each of the items in turn. There are a couple of things to comment on here. Firstly, the `for` statement creates the variable `item` (the variable name can of course be anything that you want), then sets the value to each of the elements in the list. The line that is indented is then executed for each value assigned to the `item` variable, printing out this value.
......@@ -227,11 +221,11 @@
When you print out pairs of values like in the exercise above, the output is a bit boring. It’s just a name and a number on a line. It could be a bit prettier, or at least more nicely formatted. You can put a few extra strings in there to make it clearer like this,
%% Cell type:markdown id: tags:
`print('I need to buy', amounts[i], shopping[i] # Python v2`)
`print 'I need to buy', amounts[i], shopping[i] # Python v2`
%% Cell type:markdown id: tags:
or this,
......@@ -241,11 +235,11 @@
%% Cell type:code id: tags:
``` python
for i in range(len(shopping)):
print('I need to buy', amounts[i], shopping[i])
print 'I need to buy', amounts[i], shopping[i]
```
%% Cell type:markdown id: tags:
which is maybe a bit better. Taking this approach is ok, but it is difficult to control the formatting, particularly when you are mixing numbers and strings. Most programming languages have some function or facility for creating formatted strings and Python is no exception __[note]__.
......@@ -375,11 +369,11 @@
%% Cell type:code id: tags:
``` python
for item in studentNumbers:
print(item, studentNumbers[item])
print item, studentNumbers[item]
```
%% Cell type:markdown id: tags:
That should work as expected. Note that Python doesn’t make any promises about the order the keys will be supplied in: they will be given the way Python thinks is best. It almost certainly won’t be either the order the keys were added to the dictionary or alphabetical order.
......@@ -406,22 +400,22 @@
%% Cell type:code id: tags:
``` python
for data in studentNumbers.items():
print(data[0], data[1])
print data[0], data[1]
```
%% Cell type:markdown id: tags:
or (this is usually my preference) you can unpack the data directly and more explicitly in the `for` statement:
%% Cell type:code id: tags:
``` python
for course, students in studentNumbers.items():
print(course, students)
print course, students
```
%% Cell type:markdown id: tags:
This is a little terse, so let's use the `.format()` method that was introduced earlier.
......
%% Cell type:markdown id: tags:
# Introduction to Python Programming
%% Cell type:code id: tags:
``` python
from __future__ import print_function # This statement ensures that the print function behaves the same in Python version 2 and 3
```
%% Cell type:markdown id: tags:
## 3. Input and Output
%% Cell type:markdown id: tags:
......@@ -67,11 +61,11 @@
%% Cell type:code id: tags:
``` python
datafile = open('speciesDistribution.txt', 'r')
for line in datafile:
print(line)
print line
```
%% Cell type:markdown id: tags:
OK, so far so good, the program is basically printing the whole file out to the Python Shell window. However, I forgot about the newline characters at the end of the lines. You have probably noticed that the `print` statement automatically adds a newline to the end of everything it prints, so now we are getting two after each line, which is why the output is double-spaced. So the first thing to do is to fix that, by removing the newline characters from the lines as we read them in. Strings have a `.strip()` method which removes any newlines, spaces or tabs (we called these characters 'whitespace') at the start and end of each line. So add the line
......@@ -88,11 +82,11 @@
``` python
datafile = open('speciesDistribution.txt', 'r')
for line in datafile:
line = line.strip()
print(line)
print line
```
%% Cell type:markdown id: tags:
#### Processing the File
......@@ -327,11 +321,11 @@
Compare this output to the way that the same dictionary is displayed by the default `print` function:
%% Cell type:code id: tags:
``` python
print(studentNumbers)
print studentNumbers
```
%% Cell type:markdown id: tags:
I hope you'll agree that the `pprint` version is much easier to interpret by eye.
......
%% Cell type:markdown id: tags:
# Introduction to Python Programming
%% Cell type:code id: tags:
``` python
from __future__ import print_function # This statement ensures that the print function behaves the same in Python version 2 and 3
```
%% Cell type:markdown id: tags:
## 4. Nested Data Structures
%% Cell type:markdown id: tags:
......
......@@ -4,16 +4,10 @@
%% Cell type:markdown id: tags:
## 5. Plotting Data with Bokeh
%% Cell type:code id: tags:
``` python
from __future__ import print_function # This statement ensures that the print function behaves the same in Python version 2 and 3
```
%% Cell type:markdown id: tags:
#### Opening Files
%% Cell type:markdown id: tags:
......@@ -340,11 +334,11 @@
Compare this output to the way that the same dictionary is displayed by the default `print` function:
%% Cell type:code id: tags:
``` python
print(studentNumbers)
print studentNumbers
```
%% Cell type:markdown id: tags:
I hope you'll agree that the `pprint` version is much easier to interpret by eye.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment