In [1]:
print('hello world')
This was our first Python command.
There are some minor differences between Python2 and Python3. Let us consider an example:
In [3]:
# This is an inline comment: Python3
print('hello world')
# Python2
print 'hello world'
In [5]:
1 * 1.0
a = 3
type(a)
Out[5]:
In [6]:
b = 3 > 5
print(b), type(b)
Out[6]:
Let us now turn to containers: lists, dictionaries.
In [7]:
L = ['red', 'blue', 'green', 'black', 'white']
print(L)
In [10]:
L[1], L[3:], L[3:15]
Out[10]:
Lists are mutable objects, i.e. they can be changed.
In [11]:
L[1] = 'yellow'
print(L)
What is an example of an immutable object?
In [12]:
T = ('red', 'black')
T[1] = 'yellow'
Let us now turn to the distinction between independent copies and references to objects.
In [17]:
print(L)
G = L
print(G,L)
# But now:
L[1] = 'blue'
print(G,L)
# Let us now create an independent copy:
G = L[:]
print('Independent: ', G, L)
L[1] = 'yellow'
print('Independent: ', G, L)
Formatting
In [2]:
import urllib; from IPython.core.display import HTML
HTML(urllib.urlopen('http://bit.ly/1K5apRH').read())
Out[2]: