Commit 255bb4ab authored by Raniere Silva's avatar Raniere Silva Committed by Raniere Silva
Browse files

Convert print statement to print function

parent e9413cb1
......@@ -93,7 +93,7 @@ weight_kg = 55
Once a variable has a value, we can print it to the screen:
~~~ {.python}
print weight_kg
print(weight_kg)
~~~
~~~ {.output}
55
......@@ -102,7 +102,7 @@ print weight_kg
and do arithmetic with it:
~~~ {.python}
print 'weight in pounds:', 2.2 * weight_kg
print('weight in pounds:', 2.2 * weight_kg)
~~~
~~~ {.output}
weight in pounds: 121.0
......@@ -112,7 +112,7 @@ We can also change a variable's value by assigning it a new one:
~~~ {.python}
weight_kg = 57.5
print 'weight in kilograms is now:', weight_kg
print('weight in kilograms is now:', weight_kg)
~~~
~~~ {.output}
weight in kilograms is now: 57.5
......@@ -132,7 +132,7 @@ let's store the subject's weight in pounds in a variable:
~~~ {.python}
weight_lb = 2.2 * weight_kg
print 'weight in kilograms:', weight_kg, 'and in pounds:', weight_lb
print('weight in kilograms:', weight_kg, 'and in pounds:', weight_lb)
~~~
~~~ {.output}
weight in kilograms: 57.5 and in pounds: 126.5
......@@ -144,7 +144,7 @@ and then change `weight_kg`:
~~~ {.python}
weight_kg = 100.0
print 'weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb
print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb)
~~~
~~~ {.output}
weight in kilograms is now: 100.0 and weight in pounds is still: 126.5
......@@ -168,7 +168,7 @@ If we want to check that our data has been loaded,
we can print the variable's value:
~~~ {.python}
print data
print(data)
~~~
~~~ {.output}
[[ 0. 0. 1. ..., 3. 0. 0.]
......@@ -186,7 +186,7 @@ First,
let's ask what [type](reference.html#type) of thing `data` refers to:
~~~ {.python}
print type(data)
print(type(data))
~~~
~~~ {.output}
<type 'numpy.ndarray'>
......@@ -196,7 +196,7 @@ The output tells us that `data` currently refers to an N-dimensional array creat
We can see what its [shape](reference.html#shape) is like this:
~~~ {.python}
print data.shape
print(data.shape)
~~~
~~~ {.output}
(60, 40)
......@@ -217,14 +217,14 @@ we must provide an [index](reference.html#index) in square brackets,
just as we do in math:
~~~ {.python}
print 'first value in data:', data[0, 0]
print('first value in data:', data[0, 0])
~~~
~~~ {.output}
first value in data: 0.0
~~~
~~~ {.python}
print 'middle value in data:', data[30, 20]
print('middle value in data:', data[30, 20])
~~~
~~~ {.output}
middle value in data: 13.0
......@@ -261,7 +261,7 @@ we can select the first ten days (columns) of values
for the first four patients (rows) like this:
~~~ {.python}
print data[0:4, 0:10]
print(data[0:4, 0:10])
~~~
~~~ {.output}
[[ 0. 0. 1. 3. 1. 2. 4. 7. 8. 3.]
......@@ -279,7 +279,7 @@ but the rule is that the difference between the upper and lower bounds is the nu
We don't have to start slices at 0:
~~~ {.python}
print data[5:10, 0:10]
print(data[5:10, 0:10])
~~~
~~~ {.output}
[[ 0. 0. 1. 2. 2. 4. 2. 1. 6. 4.]
......@@ -300,8 +300,8 @@ the slice includes everything:
~~~ {.python}
small = data[:3, 36:]
print 'small is:'
print small
print('small is:')
print(small)
~~~
~~~ {.output}
small is:
......@@ -325,10 +325,10 @@ will create a new array `doubledata`
whose elements have the value of two times the value of the corresponding elements in `data`:
~~~ {.python}
print 'original:'
print data[:3, 36:]
print 'doubledata:'
print doubledata[:3, 36:]
print('original:')
print(data[:3, 36:])
print('doubledata:')
print(doubledata[:3, 36:])
~~~
~~~ {.output}
original:
......@@ -355,8 +355,8 @@ will give you an array where `tripledata[0,0]` will equal `doubledata[0,0]` plus
and so on for all other elements of the arrays.
~~~ {.python}
print 'tripledata:'
print tripledata[:3, 36:]
print('tripledata:')
print(tripledata[:3, 36:])
~~~
~~~ {.output}
tripledata:
......@@ -372,7 +372,7 @@ for example,
we can just ask the array for its mean value
~~~ {.python}
print data.mean()
print(data.mean())
~~~
~~~ {.output}
6.14875
......@@ -393,9 +393,9 @@ because it is an action.
NumPy arrays have lots of useful methods:
~~~ {.python}
print 'maximum inflammation:', data.max()
print 'minimum inflammation:', data.min()
print 'standard deviation:', data.std()
print('maximum inflammation:', data.max())
print('minimum inflammation:', data.min())
print('standard deviation:', data.std())
~~~
~~~ {.output}
maximum inflammation: 20.0
......@@ -413,7 +413,7 @@ then ask it to do the calculation:
~~~ {.python}
patient_0 = data[0, :] # 0 on the first axis, everything on the second
print 'maximum inflammation for patient 0:', patient_0.max()
print('maximum inflammation for patient 0:', patient_0.max())
~~~
~~~ {.output}
maximum inflammation for patient 0: 18.0
......@@ -423,7 +423,7 @@ We don't actually need to store the row in a variable of its own.
Instead, we can combine the selection and the method call:
~~~ {.python}
print 'maximum inflammation for patient 2:', data[2, :].max()
print('maximum inflammation for patient 2:', data[2, :].max())
~~~
~~~ {.output}
maximum inflammation for patient 2: 19.0
......@@ -442,7 +442,7 @@ If we ask for the average across axis 0 (rows in our 2D example),
we get:
~~~ {.python}
print data.mean(axis=0)
print(data.mean(axis=0))
~~~
~~~ {.output}
[ 0. 0.45 1.11666667 1.75 2.43333333 3.15
......@@ -459,7 +459,7 @@ As a quick check,
we can ask this array what its shape is:
~~~ {.python}
print data.mean(axis=0).shape
print(data.mean(axis=0).shape)
~~~
~~~ {.output}
(40,)
......@@ -470,7 +470,7 @@ so this is the average inflammation per day for all patients.
If we average across axis 1 (columns in our 2D example), we get:
~~~ {.python}
print data.mean(axis=1)
print(data.mean(axis=1))
~~~
~~~ {.output}
[ 5.45 5.425 6.1 5.9 5.55 6.225 5.975 6.65 6.625 6.525
......@@ -637,7 +637,7 @@ the graphs will actually be squeezed together more closely.)
> ~~~ {.python}
> first, second = 'Grace', 'Hopper'
> third, fourth = second, first
> print third, fourth
> print(third, fourth)
> ~~~
> ## Slicing strings {.challenge}
......@@ -647,8 +647,8 @@ the graphs will actually be squeezed together more closely.)
>
> ~~~ {.python}
> element = 'oxygen'
> print 'first three characters:', element[0:3]
> print 'last three characters:', element[3:6]
> print('first three characters:', element[0:3])
> print('last three characters:', element[3:6])
> ~~~
>
> ~~~ {.output}
......
......@@ -26,10 +26,10 @@ word on a line of its own. One way to do this would be to use a series of `print
~~~ {.python}
word = 'lead'
print word[0]
print word[1]
print word[2]
print word[3]
print(word[0])
print(word[1])
print(word[2])
print(word[3])
~~~
~~~ {.output}
......@@ -53,10 +53,10 @@ but that's a bad approach for two reasons:
~~~ {.python}
word = 'tin'
print word[0]
print word[1]
print word[2]
print word[3]
print(word[0])
print(word[1])
print(word[2])
print(word[3])
~~~
~~~ {.output}
......@@ -68,9 +68,9 @@ n
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-3-7974b6cdaf14> in <module>()
3 print word[1]
4 print word[2]
----> 5 print word[3]
3 print(word[1])
4 print(word[2])
----> 5 print(word[3])
IndexError: string index out of range
~~~
......@@ -81,7 +81,7 @@ Here's a better approach:
~~~ {.python}
word = 'lead'
for char in word:
print char
print(char)
~~~
......@@ -98,7 +98,7 @@ more robust as well:
~~~ {.python}
word = 'oxygen'
for char in word:
print char
print(char)
~~~
~~~ {.output}
......@@ -130,7 +130,7 @@ Here's another loop that repeatedly updates a variable:
length = 0
for vowel in 'aeiou':
length = length + 1
print 'There are', length, 'vowels'
print('There are', length, 'vowels')
~~~
~~~ {.output}
......@@ -162,8 +162,8 @@ and we can re-use variables previously defined as loop variables as well:
~~~ {.python}
letter = 'z'
for letter in 'abc':
print letter
print 'after the loop, letter is', letter
print(letter)
print('after the loop, letter is', letter)
~~~
~~~ {.output}
......@@ -177,7 +177,7 @@ Note also that finding the length of a string is such a common operation
that Python actually has a built-in function to do it called `len`:
~~~ {.python}
print len('aeiou')
print(len('aeiou'))
~~~
~~~ {.output}
......@@ -213,7 +213,7 @@ so we should always use it when we can.
> Exponentiation is built into Python:
>
> ~~~ {.python}
> print 5 ** 3
> print(5**3)
> 125
> ~~~
>
......
......@@ -18,7 +18,7 @@ We create a list by putting values inside square brackets:
~~~ {.python}
odds = [1, 3, 5, 7]
print 'odds are:', odds
print('odds are:', odds)
~~~
~~~ {.output}
......@@ -28,7 +28,7 @@ odds are: [1, 3, 5, 7]
We select individual elements from lists by indexing them:
~~~ {.python}
print 'first and last:', odds[0], odds[-1]
print('first and last:', odds[0], odds[-1])
~~~
~~~ {.output}
......@@ -40,7 +40,7 @@ the loop variable is assigned elements one at a time:
~~~ {.python}
for number in odds:
print number
print(number)
~~~
~~~ {.output}
......@@ -57,9 +57,9 @@ For example:
~~~ {.python}
names = ['Newton', 'Darwing', 'Turing'] # typo in Darwin's name
print 'names is originally:', names
print('names is originally:', names)
names[1] = 'Darwin' # correct the name
print 'final value of names:', names
print('final value of names:', names)
~~~
~~~ {.output}
......@@ -112,7 +112,7 @@ individual elements:
~~~ {.python}
odds.append(11)
print 'odds after adding a value:', odds
print('odds after adding a value:', odds)
~~~
~~~ {.output}
odds after adding a value: [1, 3, 5, 7, 11]
......@@ -120,7 +120,7 @@ odds after adding a value: [1, 3, 5, 7, 11]
~~~ {.python}
del odds[0]
print 'odds after removing the first element:', odds
print('odds after removing the first element:', odds)
~~~
~~~ {.output}
odds after removing the first element: [3, 5, 7, 11]
......@@ -128,7 +128,7 @@ odds after removing the first element: [3, 5, 7, 11]
~~~ {.python}
odds.reverse()
print 'odds after reversing:', odds
print('odds after reversing:', odds)
~~~
~~~ {.output}
odds after reversing: [11, 7, 5, 3]
......
......@@ -24,7 +24,7 @@ while `?` matches any one character.
We can use this to get the names of all the html files:
~~~ {.python}
print glob.glob('*.html')
print(glob.glob('*.html'))
~~~
~~~ {.output}
......@@ -46,7 +46,7 @@ import matplotlib.pyplot
filenames = glob.glob('*.csv')
filenames = filenames[0:3]
for f in filenames:
print f
print(f)
data = numpy.loadtxt(fname=f, delimiter=',')
......
......@@ -23,10 +23,10 @@ We can ask Python to take different actions, depending on a condition, with an i
~~~ {.python}
num = 37
if num > 100:
print 'greater'
print('greater')
else:
print 'not greater'
print 'done'
print('not greater')
print('done')
~~~
~~~ {.output}
not greater
......@@ -50,10 +50,10 @@ Python simply does nothing if the test is false:
~~~ {.python}
num = 53
print 'before conditional...'
print('before conditional...')
if num > 100:
print '53 is greater than 100'
print '...after conditional'
print('53 is greater than 100')
print('...after conditional')
~~~
~~~ {.output}
before conditional...
......@@ -68,11 +68,11 @@ The following Python code uses `elif` to print the sign of a number.
num = -3
if num > 0:
print num, "is positive"
print(num, "is positive")
elif num == 0:
print num, "is zero"
print(num, "is zero")
else:
print num, "is negative"
print(num, "is negative")
~~~
~~~ {.output}
"-3 is negative"
......@@ -87,9 +87,9 @@ We can also combine tests using `and` and `or`.
~~~ {.python}
if (1 > 0) and (-1 > 0):
print 'both parts are true'
print('both parts are true')
else:
print 'one part is not true'
print('one part is not true')
~~~
~~~ {.output}
one part is not true
......@@ -99,7 +99,7 @@ while `or` is true if at least one part is true:
~~~ {.python}
if (1 < 0) or (-1 < 0):
print 'at least one test is true'
print('at least one test is true')
~~~
~~~ {.output}
at least one test is true
......@@ -115,7 +115,7 @@ We can check for this inside the `for` loop we wrote with the following conditio
~~~ {.python}
if data.min(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
print 'Suspicious looking maxima!'
print('Suspicious looking maxima!')
~~~
We also saw a different problem in the third dataset;
......@@ -124,14 +124,14 @@ We can also check for this with an `elif` condition:
~~~{.python}
elif data.min(axis=0).sum() == 0:
print 'Minima add up to zero!'
print('Minima add up to zero!')
~~~
And if neither of these conditions are true, we can use `else` to give the all-clear:
~~~ {.python}
else:
print 'Seems OK!'
print('Seems OK!')
~~~
Let's test that out:
......@@ -139,11 +139,11 @@ Let's test that out:
~~~ {.python}
data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
print 'Suspicious looking maxima!'
print('Suspicious looking maxima!')
elif data.min(axis=0).sum() == 0:
print 'Minima add up to zero!'
print('Minima add up to zero!')
else:
print 'Seems OK!'
print('Seems OK!')
~~~
~~~ {.output}
......@@ -153,11 +153,11 @@ Suspicious looking maxima!
~~~ {.python}
data = numpy.loadtxt(fname='inflammation-03.csv', delimiter=',')
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
print 'Suspicious looking maxima!'
print('Suspicious looking maxima!')
elif data.min(axis=0).sum() == 0:
print 'Minima add up to zero!'
print('Minima add up to zero!')
else:
print 'Seems OK!'
print('Seems OK!')
~~~
~~~ {.output}
......@@ -182,11 +182,11 @@ freeing us from having to manually examine every plot for features we've seen be
>
> ~~~ {.python}
> if 4 > 5:
> print 'A'
> print('A')
> elif 4 == 5:
> print 'B'
> print('B')
> elif 4 < 5:
> print 'C'
> print('C')
> ~~~
> ## What is truth? {.challenge}
......@@ -199,12 +199,18 @@ and false statements. However, they aren't the only values in Python that are tr
> (Note that if the body of a conditional is a single statement, we can write it on the same line as the `if`.)
>
> ~~~ {.python}
> if '': print 'empty string is true'
> if 'word': print 'word is true'
> if []: print 'empty list is true'
> if [1, 2, 3]: print 'non-empty list is true'
> if 0: print 'zero is true'
> if 1: print 'one is true'
> if '':
> print('empty string is true')
> if 'word':
> print('word is true')
> if []:
> print('empty list is true')
> if [1, 2, 3]:
> print('non-empty list is true')
> if 0:
> print('zero is true')
> if 1:
> print('one is true')
> ~~~
> ## Close enough {.challenge}
......@@ -224,7 +230,7 @@ and false statements. However, they aren't the only values in Python that are tr
> x = 1 # original value
> x += 1 # add one to x, assigning result back to x
> x *= 3 # multiply x by 3
> print x
> print(x)
> ~~~
> ~~~ {.output}
> 6
......
......@@ -52,8 +52,8 @@ Let's try running our function.
Calling our own function is no different from calling any other function:
~~~ {.python}
print 'freezing point of water:', fahr_to_kelvin(32)
print 'boiling point of water:', fahr_to_kelvin(212)
print('freezing point of water:', fahr_to_kelvin(32))
print('boiling point of water:', fahr_to_kelvin(212))
~~~
~~~ {.output}
freezing point of water: 273.15
......@@ -82,14 +82,14 @@ and printing out the value of each part.
~~~ {.python}
# We'll use temp = 212, the boiling point of water, which was incorrect
print "212 - 32:", 212 - 32
print("212 - 32:", 212 - 32)
~~~
~~~ {.output}
212 - 32: 180
~~~
~~~ {.python}
print "(212 - 32) * (5/9):", (212 - 32) * (5/9)
print("(212 - 32) * (5/9):", (212 - 32) * (5/9))
~~~
~~~ {.output}
(212 - 32) * (5/9): 0
......@@ -116,7 +116,7 @@ If we divide one integer by another,
we get the quotient without the remainder:
~~~ {.python}
print '10/3 is:', 10/3
print('10/3 is:', 10/3)
~~~
~~~ {.output}
10/3 is: 3
......@@ -127,7 +127,7 @@ on the other hand,
the computer creates a floating-point answer:
~~~ {.python}
print '10.0/3 is:', 10.0/3
print('10.0/3 is:', 10.0/3)
~~~
~~~ {.output}
10.0/3 is: 3.33333333333
......@@ -149,7 +149,7 @@ is to explicitly tell the computer that you desire one.
This is achieved by [casting](reference.html#typecast) one of the numbers:
~~~ {.python}
print 'float(10)/3 is:', float(10)/3
print('float(10)/3 is:', float(10)/3)
~~~
~~~ {.output}
float(10)/3 is: 3.33333333333
......@@ -161,8 +161,8 @@ Let's take a look:
~~~ {.python}
a = 10
b = 3
print 'a/b is:', a/b
print 'float(a)/b is:', float(a)/b
print('a/b is:', a/b)
print('float(a)/b is:', float(a)/b)
~~~
~~~ {.output}
a/b is: 3
......@@ -175,8 +175,8 @@ Let's fix our `fahr_to_kelvin` function with this new knowledge:
def fahr_to_kelvin(temp):
return ((temp - 32) * (5.0/9.0)) + 273.15
print 'freezing point of water:', fahr_to_kelvin(32)
print 'boiling point of water:', fahr_to_kelvin(212)
print('freezing point of water:', fahr_to_kelvin(32))
print('boiling point of water:', fahr_to_kelvin(212))
~~~
~~~ {.output}
freezing point of water: 273.15
......@@ -192,7 +192,7 @@ it's easy to turn Kelvin into Celsius:
def kelvin_to_celsius(temp):
return temp - 273.15
print 'absolute zero in Celsius:', kelvin_to_celsius(0.0)
print('absolute zero in Celsius:', kelvin_to_celsius(0.0))
~~~
~~~ {.output}
absolute zero in Celsius: -273.15
......@@ -210,7 +210,7 @@ def fahr_to_celsius(temp):
result = kelvin_to_celsius(temp_k)
return result
print 'freezing point of water in Celsius:', fahr_to_celsius(32.0)
print('freezing point of water in Celsius:', fahr_to_celsius(32.0))
~~~
~~~ {.output}
freezing point of water in Celsius: 0.0
......@@ -262,11 +262,11 @@ def detect_problems(filename):
data = np.loadtxt(fname=filename, delimiter=',')
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
print 'Suspicious looking maxima!'
print('Suspicious looking maxima!')
elif data.min(axis=0).sum() == 0:
print 'Minima add up to zero!'
print('Minima add up to zero!')
else:
print 'Seems OK!'
print('Seems OK!')
~~~
Notice that rather than jumbling this code together in one giant `for` loop,
......@@ -275,7 +275,7 @@ We can reproduce the previous analysis with a much simpler `for` loop:
~~~ {.python}
for f in filenames[:3]:
print f
print(f)
analyze(f)
detect_proble