In [1]:
def foo(a,b):
print('Hi!')

In [2]:
foo(3,17)

Hi!

In [5]:
def pow(a,b):
c = a**b
return c

In [7]:
x = pow(3,4)

In [8]:
x

Out[8]:
81
In [9]:
def pow(a,b):
c = a**b
return c,c,c

In [10]:
pow(3,4)

Out[10]:
(81, 81, 81)
In [11]:
x,y,z = pow(3,4)

In [12]:
x

Out[12]:
81
In [13]:
y

Out[13]:
81
In [14]:
z

Out[14]:
81

Euclid's algorithm for GCD

In [18]:
def mygcd(a,b):
while a!=b:
if a<b:
a,b = b,a # swap so that a is the larger
a -= b  # equivalent to a = a - b # replace a by a-b
return a  # (or b, they are the same at this point)

In [19]:
mygcd(5,13)

Out[19]:
1
In [20]:
mygcd(31415926534676736647, 438478473847834834784748)

Out[20]:
7
In [21]:
mygcd(31415926534676736647, 7)

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-21-f86fdc012c70> in <module>()
----> 1 mygcd(31415926534676736647, 7)

<ipython-input-18-590b075cbfb3> in mygcd(a, b)
3         if a<b:
4             a,b = b,a # swap so that a is the larger
----> 5         a -= b  # equivalent to a = a - b # replace a by a-b
6     return a  # (or b, they are the same at this point)

KeyboardInterrupt: 
In [60]:
def mygcd(a,b):
while a!=b:
if a<b:
a,b = b,a # swap so that a is the larger
a = a%b  # mod operator gives remainder
if a==0: return b
return a  # (or b, they are the same at this point)

In [25]:
mygcd(31415926534676736647, 7)

Out[25]:
7

New language features for PPT exploration

Iterable objects like lists, tuple, ranges

Lists

In [26]:
mylist = [1,2,5,'hello']

In [27]:
mylist

Out[27]:
[1, 2, 5, 'hello']

Iteration over a list

In [28]:
for item in mylist:
print(item)

1
2
5
hello

In [29]:
for blah in mylist:
print(blah)

1
2
5
hello

In [30]:
students = ['Chaoran', 'Minyue']
for student in students:   # a nice idiom but not necessary
print(student)

Chaoran
Minyue


Tuples are like lists, but they are "immutable"

In [31]:
students = ('Chaoran', 'Minyue')
for student in students:   # a nice idiom but not necessary
print(student)

Chaoran
Minyue

In [32]:
range(10)

Out[32]:
range(0, 10)
In [33]:
type( range(10))

Out[33]:
range
In [34]:
type(mylist)

Out[34]:
list
In [35]:
for item in range(10):
print(item)

0
1
2
3
4
5
6
7
8
9

In [36]:
for item in range(1,10):
print(item)

1
2
3
4
5
6
7
8
9

In [37]:
for item in range(1,10,2):
print(item)

1
3
5
7
9


Scatter plot

In [45]:
%matplotlib notebook
# This is a so-called notebook magic command
import matplotlib.pyplot as plt
xlist = [1,2,4]
ylist = [5,3,6]
plt.plot(xlist,ylist,'ro',clip_on=False,markersize=10)
plt.xlabel('a')
plt.ylabel('b')
plt.title('My PPTs')
plt.show()

In [56]:
max = 5
for a in range(1,max):
for b in range(a,max):
csq = a**2 + b**2
print(a,b,csq)
# write code here to see if a**2 + b**2 is the square of an integer

1 1 2
1 2 5
1 3 10
1 4 17
2 2 8
2 3 13
2 4 20
3 3 18
3 4 25
4 4 32

In [49]:
x = [1,2,4]  # list
y = (1,2,4)  # tuple

In [50]:
x[2]

Out[50]:
4
In [51]:
y[2]

Out[51]:
4
In [52]:
for item in x: print(item)

1
2
4

In [53]:
for item in y: print(item)

1
2
4

In [54]:
x[2]=77
x

Out[54]:
[1, 2, 77]
In [55]:
y[2]=77

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-e0816450bc47> in <module>()
----> 1 y[2]=77

TypeError: 'tuple' object does not support item assignment
In [57]:
mylist = []
for i in range(5):
mylist.append(i)
mylist

Out[57]:
[0, 1, 2, 3, 4]
In [ ]:
alist = []
blist = []
for
for

if ....
alist.append(a)
blist.append(b)

plt.plot(alist,blist,'ro')

In [61]:
from math import sqrt
max = 50
for a in range(1,max):
for b in range(a,max):
csq = a**2 + b**2
c = sqrt(csq)
# Is c an integer?
if int(c)==c and mygcd(a,b)==1:
print(a,b,int(c))
# write code here to see if a**2 + b**2 is the square of an integer

3 4 5
5 12 13
7 24 25
8 15 17
9 40 41
12 35 37
20 21 29
28 45 53