Syllabus for all weeks#
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
.py
extensionSyntax guidelines: comments, blank lines, indentation, and case sensitivity
Some ways to execute Python code:
Script mode in IDLE (practical knowledge: you should acquire this skill)
Jupyter notebooks and cell mode for some IDES (conceptual knowledge)
Running Python code directly in a browser
Key concepts introduced through examples, no formal definition yet:
Assignments
The
print()
functionString variables
Inputs to a function (later referred to as arguments)
Numeric variables for integers and floats
Common numeric operators:
+
,-
,*
,**
,/
Operator precedence and the use of parentheses to control precedence.
Simple functions:
abs()
,round()
Output of functions (later referred to as returned values)
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 expression.
Variables and types (formal definition)
Valid variable names and naming conventions
The
type()
functionConcept of namespace (an informal definition)
Reassignments
Numeric data types:
int
,float
String data type,
str
with 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
math
moduleVia example keyword
import
and the import statementFunctions from the
math
module:math.sin()
,math.cos()
,math.sqrt()
,math.exp()
,math.log()
,math.ceil()
,math.floor()
.Constant
math.pi
Boolean data type and
bool()
Comparison operators:
==
,!=
,<
,>
,<=
,>=
Logical operators:
and
,or
,not
Good practice, e.g. avoid
print = 6
Understanding error messages
Syllabus Week 3: Conditionals#
Conditional execution with if statements, keyword
if
followed by a Boolean expressionControlling scope with indented block
Alternative execution with if-else statements, keyword
else
Chained conditionals with if-elif-else statements, keyword
elif
Nested 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
for
and 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
while
Understanding 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
break
Solve simple problems that require a lot of computation e.g. simulations and population models
The concept of pseudocode
Syllabus Week 5: Functions#
Functions, keyword
def
Function 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
return
Fruitful and void function, side effects,
None
typeFunction 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
is
as 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
in
as a membership operatorRevisit the
len()
function for stringsTraversing strings, keyword
in
for 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
in
for 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
os
an 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
with
and aliasas
for 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
class
Class naming convention
Attributes
Objects are mutable
Methods and the argument
self
Method
__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
dir
function