Week-2 Python Fundamentals
Week-2 Python Fundamentals
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 2, Lecture 1
i = 5
j = 2*i
j = j + 5
int — integers
9//5 is 1, 9%5 is 4
Exponentiation: **
3**4 is 81
Other operations on
numbers
i = 5 # i is int
i = 7*1 # i is still int
j = i/3 # j is float, / creates float
…
i = 2*j # i is now float
True, False
divisor = (m%n == 0)
Examples
def divides(m,n):
if n%m == 0:
return(True)
else:
return(False)
def even(n):
return(divides(2,n))
def odd(n):
return(not divides(2,n))
Summary
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 2, Lecture 2
Document preparation
s = "hello"
t = s + ", there"
s = "hello"
s[1:4] is “ell"
s = s[0:3] + "p!"
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 2, Lecture 3
nested = [[2,[37]],4,["hello"]]
nested[0] is [2,[37]]
nested[1] is 4
nested[2][0][3] is "l"
nested[0][1:2] is [[37]]
Updating lists
Unlike strings, lists can be updated in place
nested = [[2,[37]],4,["hello"]]
nested[1] = 7
nested is now [[2,[37]],7,["hello"]]
nested[0][1][0] = 19
nested is now [[2,[19]],7,["hello"]]
x = 5
y = x
x = 7
list1 = [1,3,5,7]
list2 = list1
list1[2] = 4
list2[2] is also 4
list1 and list2 are two names for the same list
Copying lists
How can we make a copy of a list?
list1 = [1,3,5,7]
list2 = [4,5,6,8]
list3 = list1 + list2
list3 is now [1,3,5,7,4,5,6,8]
Note that + always produces a new list
list1 = [1,3,5,7]
list2 = list1
list1 = list1 + [9]
list1 and list2 no longer point to the same object
Summary
Lists are sequences of values
Values need not be of uniform type
Lists may be nested
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 2, Lecture 4
Conditional execution
Function definitions
Conditional execution
if m%n != 0:
(m,n) = (n,m%n)
Second statement is executed only if the condition
m%n != 0 is True
Indentation demarcates body of if — must be uniform
if condition:
statement_1 # Execute conditionally
statement_2 # Execute conditionally
statement_3 # Execute unconditionally
Alternative execution
if m%n != 0:
(m,n) = (n,m%n)
else:
gcd = n
else: is optional
Shortcuts for conditions
Numeric value 0 is treated as False
if m%n:
(m,n) = (n,m%n)
else:
gcd = n
Multiway branching, elif:
if x == 1: if x == 1:
y = f1(x) y = f1(x)
else: elif x == 2:
if x == 2: y = f2(x)
y = f2(x) elif x == 3:
else: y = f3(x)
if x == 3: else:
y = f3(x) y = f4(x)
else:
y = f4(x)
Loops: repeated actions
for i in [1,2,3,4]:
y = y*i
z = z+1
def factors(n):
flist = []
for i in range(1,n+1):
if n%i == 0:
flist = flist + [i]
return(flist)
Loop based on a condition
Often we don’t know number of repetitions in
advance
while condition:
. . .
def gcd(m,n):
if m < n:
(m,n) = (n,m)
while m%n != 0:
(m,n) = (n,m%n)
return(n)
Summary
Normally, statements are executed top to bottom,
in sequence
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 2, Lecture 5
Body is indented
def stupid(x):
n = 17
return(x)
n = 7
v = stupid(28)
# What is n now?
n is still 7
Name n inside function is separate from n outside
Defining functions
A function must be defined before it is invoked
def factorial(n):
if n <= 0:
return(1)
else:
val = n * factorial(n-1)
return(val)
Summary
Functions are a good way to organise code in logical
chunks
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 2, Lecture 6
def factors(n):
factorlist = []
for i in range(1,n+1):
if n%i == 0:
factorlist = factorlist + [i]
return(factorlist)
Primes
Prime number — only factors are 1 and itself
factors(17) is [1,17]
factors(18) is [1,2,3,6,9,18]
def isprime(n):
return(factors(n) == [1,n])
def primesupto(n):
primelist = []
for i in range(1,n+1):
if isprime(i):
primelist = primelist + [i]
return(primelist)
First n primes
def nprimes(n):
(count,i,plist) = (0,1,[])
while(count < n):
if isprime(i):
(count,plist) = (count+1,plist+[i])
i = i+1
return(plist)
for and while
primesupto()
nprimes()
for n in range(i,j): n = i
statement while n < j:
statement
n = n+1
for n in l: i = 0
statement while i < len(l):
n = l[i]
statement
i = i+1
for and while
Can use while to simulate for
However, use for where it is natural
Makes for more readable code
What makes a good program?
Correctness and efficiency — algorithm
Readability, ease of maintenance — style
What you say, and how you say it