Syllabus for all weeks#
The syllabus outlines the essential topics students are expected to learn in the course. Advanced topics mentioned in the course are not included in the syllabus. To pass the exam, you only need to know the material listed in the syllabus. For the exam, you are not allowed to import any libraries outside the syllabus.
Syllabus Week 1: Introduction#
Basic syntax and terminology:
A program is a text with a sequence of commands executed line-by-line
Each time a program runs, it starts with a clean slate (no previous definitions)
Python programs are saved as text files with a
.pyextensionPython syntax for comments, blank lines, indentation, and case sensitivity
Some ways to execute Python code:
Interactive and script mode in IDLE (you should be able to do this)
Executing Python code from a terminal (you should know of this)
Jupyter notebooks and cell mode for some IDES (you should know of this)
Running Python code directly in a browser (you should know of this)
Key concepts introduced through examples, no formal definition yet:
The
=operatorThe
print()functionNumeric variables for integers and floats
String variables
Common numeric operators:
+,-,*,**,/Operator precedence and the use of parentheses to control precedence
Functions
abs()andround()
Syllabus Week 2: Variables and Types#
Concept of statement illustrated by the print statement and assignment statement
Assignments and assignment operator
=(formal definition)The concept of expressions
Variables and types (formal definition)
Valid variable names and naming conventions
The
type()functionConcept of namespace (an informal definition)
Reassignments
Numeric data types:
int,floatString data type,
strwith single or double quoteString operators
+(concatenation) and*(repetition)The
len()function for stringsType casting with
int(),float(),str()Numeric operators:
%(modulus) and//(floor division)Built-in-functions with multiple arguments:
max(),min(), andprint()The
mathmoduleVia example keyword
importand the import statementFunctions from the
mathmodule:math.sin(),math.cos(),math.sqrt(),math.exp(),math.log(),math.ceil(),math.floor().Constant
math.piBoolean data type and
bool()Comparison operators:
==,!=,<,>,<=,>=Logical operators:
and,or,notGood practice, e.g. avoid
print = 6Understanding error messages
Syllabus Week 3: Conditionals#
Conditional execution with if statements, keyword
iffollowed by a Boolean expressionControlling scope with indented block
Alternative execution with if-else statements, keyword
elseChained conditionals with if-elif-else statements, keyword
elifNested conditionals
The concept of debugging
Debugging with print statement: using
print()to trace and understand code executionIdentifying errors in code that runs but does not produce the expected output
Problem-solving by constructing the correct conditions
Syllabus Week 4: Loops#
Looping over a sequence (known number of iterations), keyword
forand keywordin, therange()functionUnderstanding that indexing variable in the for loop is reassigned with each iteration
Looping as long as a condition is true (unknown number of iterations), keyword
whileUnderstanding that the variables used in the condition need to be defined before the while loop
Understanding that the variables used in the condition should be changed in the while loop body for loop to terminate
Keyword
breakSolve simple problems that require a lot of computation e.g. simulations and population models
The concept of pseudocode
Syllabus Week 5: Functions#
Functions, keyword
defFunction names and naming convention
Function parameters (names given by the function definition)
Function arguments (values passed to the function when called)
Function body and indentation
Calling functions
Variable scope
Returning values, keyword
returnFruitful and void function, side effects,
NonetypeFunction examples: build-in functions, functions included in the standard Python library, functions from common third-party libraries, user-defined functions.
Tracebacks in error messages
Good practice when writing functions (start by scripting, incremental development, scaffolding)
Testing functions
Writing tests for functions
Documenting functions
Syllabus Week 6: Lists#
Lists, a list is a sequence of values
Understanding sequences, elements, and indexes.
Creating lists: enclosing the elements in square brackets
List indexing
List slicing
Lists are mutable
The keyword
isas identity operator vs. equality operator==List methods:
append(),extend(),pop(),index(),sort(),count(),copy()List operators:
+(concatenate) and*(repeat)Difference between modification and reassignment
Empty lists
Deleting list elements
Pure functions and modifier functions.
Functions
min(),max(),sum(),len()and understanding functions that accept different data typesTraversing lists
Understanding that modifying the indexing variable does not affect the list
Constructor
list()i.e.list('hej'),list(range(4))Solving problems that involve custom searches within lists.
Syllabus Week 7: Strings#
A string is a sequence of characters (each character itself is also a string), revisit sequences, elements, indices
String indexing
String slicing
Negative index
Methods and invoking a method
String methods:
upper(),lower(),find(),index(),count(),strip()Keyword
inas a membership operatorRevisit the
len()function for stringsTraversing strings, keyword
infor traversalf-strings, and string formatting options
Escape sequence, e.g.
"/n"String methods with lists as return value or argument:
split()andjoin()The
repr()function and using it to display strings with escape sequences visible
Syllabus Week 8: Dictionaries and Tuples#
Dictionary, a collection of key-value pairs
Creating dictionaries: enclosing key-value pairs in curly brackets
Keyword
infor dictionaries operates on keys, not valuesTraversing a dictionary
Adding key-value pairs to the dictionary
Dictionaries are mutable
Tuple, an immutable sequence of values
Creating tuple with or without parenthesis
Unpacking, i.e
a, b = my_function()Using tuple as a return value
Syllabus Week 9: Files#
Module
osan interface to interact with the operating systemCurrent working directory
os.getcwd()Directory list
os.listdir()Check if the file exists
os.path.isfile(), alsoos.path.isdir()Relative and absolute paths,
os.path.join()Function
open()with'r'and'w'Statement
withand aliasasfor simplified handling of exceptionsReading methods:
read(),readlines()Splitting lines with
splitlines()Writing methods:
write(),writelines()More on string escape sequences,
repr()andstrip()Reading csv files as text files
Syllabus Week 10: Classes I#
Object-oriented programming, structuring code to bundle properties and behavior.
Classes, templates for creating instances that share common attributes and methods.
Instances, concrete objects created from a class template.
Keyword
classClass naming convention
Attributes
Objects are mutable
Methods and the argument
selfMethod
__init__()
Syllabus Week 11: Classes II#
Inheritance: parent class (superclass) and child class (subclass)
Method overriding
The
super()functionMethod
__str__()and overloadingstr()andprint()Operator overloading example on
__add__()and__eq__()
Syllabus Week 12: Numpy and matplotlib#
NumPy, a library for numeric computing in Python, especially when handling large arrays and matrices. Via examples, we will cover:
NumPy array, an n-dimensional array
Compact syntax
Vectorized NumPy operators, for example
+,*NumPy functions for element-wise operations:
numpy.sqrt(),numpy.exp(),numpy.sin(),numpy.abs()Other functions and methods:
numpy.mean(),numpy.std()Indexing, slicing, and boolean indexing
NumPy arrays are mutable
Preallocation
Matplotlib, a library for scientific visualization in Python. Via examples, we will cover:
Line plots
Bar plots
Labels
Legends
Titles
Text
Syllabus Week 13: Modules#
User-made modules, using them and creating them
Import statement, standard import, selective import and alias import
The
dirfunction