SCC461 – Programming for Data Scientists
Leandro Marcolino
Lancaster University
Week 6 – Discussions
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 1 / 34
Module Structure
Outline
1 Module Structure
2 Exercises
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 1 / 34
Module Structure
Overview
Weeks 1 to 5:
Taught by Clement
Focused on the statistical side of programming using R
Weeks 6 to 10
Taught by Leandro
Object-oriented programming using Python
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 2 / 34
Module Structure
Tentative Schedule
Week 6: Basics of programming
Week 7: OOP + Data Structures I
Week 8: Data Structures II
Week 9: Inheritance + Libraries
Week 10: Problem Solving
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 3 / 34
Module Structure
Assessment
Weekly Assessment (25%)
Deadline: 9am the following Monday
Moodle Quiz
Plenty of comments
Immediate Feedback
Multiple attempts per question allowed, but with a penalty
Pre-check available, no penalty
Assignment (50%)
Details will be published in week 8, due in week 11 (after Christmas)
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 4 / 34
Module Structure
Teaching Assistants
Gao Peng, g.peng1@lancaster.ac.uk
Somayeh Bazin, s.bazin@lancaster.ac.uk
Abdulrahman Kerim, a.kerim@lancaster.ac.uk
Farid Bello, f.bello@lancaster.ac.uk
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 5 / 34
Exercises
Outline
1 Module Structure
2 Exercises
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 5 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 7, 8]
for x in myList:
print(x)
Iterative code
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 6 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 7, 8]
for x in myList:
print(x)
Iterative code
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 6 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 7, 8]
for x in range(len(myList)):
print(x)
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 7 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 7, 8]
for x in range(len(myList)):
print(myList[x])
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 8 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 7, 8]
for x in myList:
x = 5
print(myList [0])
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 9 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 99, 95, 7]
x = -1
xPos = -1
for y in range(len(myList)):
if (myList[y] > x):
x = myList[y]
xPos = y
print(x)
print(xPos)
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 10 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 99, 95, 7]
x = -1
xPos = -1
for y in range(len(myList)):
if (y == 2):
continue
if (myList[y] > x):
x = myList[y]
xPos = y
print(x)
print(xPos)
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 11 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 99, 95, 7]
x = -1
xPos = -1
for y in range(len(myList)):
if (y == 2):
break
if (myList[y] > x):
x = myList[y]
xPos = y
print(x)
print(xPos)
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 12 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 7, 10, 23, 2, 3]
specialList = []
for x in myList:
if (x % 2 == 0):
specialList.append(x)
print(specialList [1])
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 13 / 34
Exercises
Lists
What will be printed?
myList = [3, 4, 7, 10, 23, 2, 3]
specialList = []
for x in myList:
if (x % 2 != 0):
specialList.append(x)
print(specialList [1])
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 14 / 34
Exercises
Exercises
Exercise handout, “1. Fundamentals”
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 15 / 34
Exercises
Functions
What will be printed?
def callMe(y):
b = 5;
return y*b;
x = callMe (5);
print(x);
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 16 / 34
Exercises
Functions
What will be printed?
def callMe(y):
b = 5;
x = 10;
return y*b;
b = 3;
x = callMe (5);
print(x);
print(b);
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 17 / 34
Exercises
Exercises
Exercise handout, “2. Functions”
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 18 / 34
Exercises
Pointers
What will be printed?
a = 2;
b = a;
b = 999;
print(a);
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 19 / 34
Exercises
Pointers
What will be printed?
a = [1, 2, 3, 4, 5];
b = a;
b[1] = 999;
print(a);
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 20 / 34
Exercises
Pointers
What will be printed?
a = [1, 2, 3, 4, 5];
b = a[:];
b[1] = 999;
print(a);
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 21 / 34
Exercises
Pointers
What will be printed?
def callMe(x):
x = 1;
return;
myInput = 0;
callMe(myInput);
print(myInput);
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 22 / 34
Exercises
Pointers
What will be printed?
def callMe(x):
x[0] = 1;
return;
myList = [0];
callMe(myList);
print(myList [0]);
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 23 / 34
Exercises
Recursive Functions
How do you sum n numbers?
5 + 8 + 18 + 5 + 2 + 20 ...
First number + sum n − 1 numbers
Wha
t is
the
Pyth
on c
ode
?
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 24 / 34
Exercises
Recursive Functions
How do you sum n numbers?
5 + 8 + 18 + 5 + 2 + 20 ...
First number + sum n − 1 numbers
Wha
t is
the
Pyth
on c
ode
?
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 24 / 34
Exercises
Recursive Functions
How do you sum n numbers?
5 + 8 + 18 + 5 + 2 + 20 ...
First number + sum n − 1 numbers
Wha
t is
the
Pyth
on c
ode
?
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 24 / 34
Exercises
Recursive Summation
def sumRec(l):
if (l == []):
return 0
summation = l[0] + sumRec(l[1:])
return summation
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 25 / 34
Exercises
Recursive Summation
Another Solution
def sumRec(current ,l):
if (l == []):
return current
partialResult = current + l[0]
summation = sumRec(partialResult ,l[1:])
return summation
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 26 / 34
Exercises
Recursive Functions
def callMe(y):
print(y);
if (y >= 0):
callMe(y - 1);
return 0;
x = callMe (5);
print(x);
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 27 / 34
Exercises
Recursive Functions
def callMe(y):
callMe (5);
return 0;
x = callMe (5);
print(x);
What will be printed?
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 28 / 34
Exercises
Recursive Functions
Exercise handout, “3. Recursive Functions”
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 29 / 34
Exercises
Assignment
Deadline: 9am the following Monday
Submit your source code on the Moodle Quiz
Code must have plenty of comments
Pre-check available, no penalty
Wrong replies can be modified, with a penalty
All or nothing test cases – wrong solutions must be fixed.
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 30 / 34
Exercises
Rules of the Game
Discussions are allowed
Searching for algorithms online is allowed (e.g., pseudo-code)
Copying code to do a “building block” of your solution is allowed
E.g., how to open a file, how to print in the screen, how to draw a
random variable, etc.
NOT HOW TO SOLVE THE FULL ASSIGNMENT!
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 31 / 34
Exercises
Rules of the Game
Copying and pasting full Python code that solves the whole
assignment IS NOT allowed
Replacing variable names is still copying and pasting!
Just changing the text output is still copying and pasting!
References and discussions included in the comments
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 32 / 34
Exercises
TA Support
Gao Peng: Tuesday, 10am-12pm.
Somayeh Bazin: Tuesday, 11am-1pm.
Abdulrahman Kerim: Thursday, 8am-10am.
Farid Bello: Monday, 2pm-4pm.
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 33 / 34
Exercises
Thank you!
l.marcolino@lancaster.ac.uk
http://www.lancaster.ac.uk/staff/sorianom/
Leandro Marcolino (Lancaster) SCC461 Week 6 – Discussions 34 / 34
学霸联盟