When you are editing a cell in Jupyter notebook, you need to re-run the cell by pressing <Shift> + <Enter>. This will allow changes you made to be available to other cells.
Use <Enter> to make new lines inside a cell you are editing.
Re-running will execute any statements you have written. To edit an existing code cell, click on it.
Re-running will render the markdown text. To edit an existing markdown cell, double-click on it.
Near the top of the https://try.jupyter.org page, Jupyter provides a row of menu options (File, Edit, View, Insert, ...) and a row of tool bar icons (disk, plus sign, scissors, 2 files, clipboard and file, up arrow, ...).
*.ipynb file and click "open"
In [1]:
5 + 5
Out[1]:
In [2]:
x = 5
y = 'Hello There'
z = 10.5
In [3]:
x + 5
Out[3]:
In [4]:
x = 1
print ('The value of x is ', x)
In [5]:
x = 2.5
print ('Now the value of x is ', x)
In [6]:
x = 'hello there'
print ('Now it is ', x)
In [7]:
print (round(3.14))
In [8]:
help(round)
In [9]:
import builtins
dir(builtins)
Out[9]:
Later we will discuss how to built our own function
In [10]:
print (y)
In [11]:
y + 5
In [12]:
type(1)
Out[12]:
In [13]:
type('hello')
Out[13]:
In [14]:
type(2.5)
Out[14]:
In [15]:
type(True)
Out[15]:
In [16]:
type(y)
Out[16]:
In [17]:
result = None
None in the interactive interpreter, no result is printed out.
In [18]:
print(result)
In [19]:
type(result)
Out[19]:
is operator:
In [20]:
result is None
Out[20]:
In [21]:
x = 5
x is None
Out[21]:
In [22]:
x = 100
print (type(x))
print (x)
In [23]:
y = float(x)
print (type(y))
print (y)
In [24]:
print (int(y))
chr() and ord() can be used to convert characters from and to ASCII.
In [25]:
print (ord('a'))
In [26]:
print (chr(97))
In [27]:
y = 'hello'
print ('The type of the value referred to by y is ', type(y))
y = 5.0
print ('And now the type of the value is ', type(y))
In [28]:
1//5
Out[28]:
In [29]:
1/5
Out[29]:
In [30]:
1.0 / 5.0
Out[30]:
In [31]:
1 + 1
Out[31]:
In [32]:
'a' + 'b'
Out[32]:
In [33]:
'1' + '1'
Out[33]:
The syntax for control structures in Python use colons and indentation.
Beware that white-space affects the semantics of Python code.
In [34]:
x = 5
if x > 0:
print ('x is strictly positive')
print (x)
print ('finished.')
In [35]:
x = 0
if x > 0:
print ('x is strictly positive')
print (x)
print ('finished')
We can use lists to hold an ordered sequence of values.
In [36]:
l = ['first', 'second', 'third']
print (l)
Lists can contain different types of variable, even in the same list.
In [37]:
another_list = ['first', 'second', 'third', 1, 2, 3]
print (another_list)
Lists are mutable; their contents can change as more statements are interpreted.
In [38]:
l.append('fourth')
print (l)
In [39]:
X = [1, 2, 3]
Y = X
The above code creates two different references (named X and Y) to the same value [1, 2, 3]
Because lists are mutable, changing them can have side-effects on other variables.
If we append something to X what will happen to Y?
In [40]:
X.append(4)
print(X)
In [41]:
print(Y)
In [42]:
X = [1, 2]
Y = [1]
Y.append(2)
In [43]:
X == Y
Out[43]:
In [44]:
X is Y
Out[44]:
In [45]:
Y.append(3)
X
Out[45]:
In [46]:
X = Y
In [47]:
X is Y
Out[47]:
In [48]:
for i in l:
print (i)
In [49]:
for i in [0, 1, 2, 3]:
print ("Hello!")
In [50]:
for i in range(4):
print ("Hello!")
In [51]:
print (l[0])
In [52]:
print (l[1])
In [53]:
print (l[0:2])
In [54]:
print (l[:2])
In [55]:
print (l[2:])
In [56]:
print (l[-1])
In [57]:
print (l[:-1])
In [58]:
tuple1 = (50, 'hello')
print(tuple1)
In [59]:
print(tuple1[0])
In [60]:
type(tuple1)
Out[60]:
In [61]:
tuple1.append(2)
In [62]:
tuple1[0] = 5
In [63]:
X = set([1, 2, 3, 3, 4])
print(X)
In [64]:
type(X)
Out[64]:
{ and } brackets.
In [65]:
X = {1, 2, 3, 4}
type(X)
Out[65]:
In [66]:
X.add(5)
print(X)
In [67]:
X.add(5)
print(X)
In [68]:
X[0]
In [69]:
X = {1, 2, 3}
Y = {4, 5, 6}
X.union(Y)
Out[69]:
In [70]:
X = {1, 2, 3, 4}
Y = {3, 4, 5}
X.intersection(Y)
Out[70]:
In [71]:
X - Y
Out[71]:
In [72]:
person = {'name':'sofian','gender':'male', 'age':17}
person['new'] = 'new'
print (person.keys())
print (person.values())
print (person['name'])
print (person)
In [73]:
def squared(x):
return x ** 2
print (squared(5))
In [74]:
def squared(x):
result = x ** 2
return result
print (squared(5))
In [75]:
print (result)
In [76]:
print(squared)
In [77]:
y = squared
print (y)
In [78]:
print (y(5))
We can apply a function to each element of a collection using the built-in function map().
This will work with any collection: list, set, tuple or string.
This will take as an argument another function, and the list we want to apply it to.
It will return the results of applying the function, as a list.
In [79]:
list(map(squared, [1, 2, 3, 4]))
Out[79]:
In [80]:
[squared(i) for i in [1, 2, 3, 4]]
Out[80]:
In [81]:
{squared(i) for i in [1, 2, 3, 4]}
Out[81]:
The Cartesian product of two collections $X = A \times B$ can be expressed by using multiple for statements in a comprehension.
In [82]:
A = {'x', 'y', 'z'}
B = {1, 2, 3}
{(a,b) for a in A for b in B}
Out[82]:
In [83]:
first_names = ('Steve', 'John', 'Peter')
surnames = ('Smith', 'Doe')
[(first_name, surname) for first_name in first_names for surname in surnames]
Out[83]:
In [ ]:
list(map(lambda x: x ** 2, [1, 2, 3, 4]))
In [ ]:
list(filter(lambda x: x > 0, [-5, 2, 3, -10, 0, 1]))
We can use both filter() and map() on other collections such as strings or sets.
In [ ]:
list(map(ord, 'hello world'))
In [ ]:
''.join(list(filter(lambda x: x != 'l' , 'hello world')))
In [ ]:
data = [-5, 2, 3, -10, 0, 1]
[x for x in data if x > 0]