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 .py extension

  • Python 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 = operator

  • The print() function

  • Numeric variables for integers and floats

  • String variables

  • Common numeric operators: +, -, *, **, /

  • Operator precedence and the use of parentheses to control precedence

  • Functions abs() and round()

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() function

  • Concept of namespace (an informal definition)

  • Reassignments

  • Numeric data types: int, float

  • String data type, str with single or double quote

  • String operators + (concatenation) and * (repetition)

  • The len() function for strings

  • Type casting with int(), float(), str()

  • Numeric operators: % (modulus) and // (floor division)

  • Built-in-functions with multiple arguments: max(), min(), and print()

  • The math module

  • Via example keyword import and the import statement

  • Functions 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 expression

  • Controlling 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 execution

  • Identifying 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 keyword in, the range() function

  • Understanding 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 type

  • Function 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 types

  • Traversing 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 operator

  • Revisit the len() function for strings

  • Traversing strings, keyword in for traversal

  • f-strings, and string formatting options

  • Escape sequence, e.g. "/n"

  • String methods with lists as return value or argument: split() and join()

  • 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 values

  • Traversing 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 system

  • Current working directory os.getcwd()

  • Directory list os.listdir()

  • Check if the file exists os.path.isfile(), also os.path.isdir()

  • Relative and absolute paths, os.path.join()

  • Function open() with 'r' and 'w'

  • Statement with and alias as for simplified handling of exceptions

  • Reading methods: read(), readlines()

  • Splitting lines with splitlines()

  • Writing methods: write(), writelines()

  • More on string escape sequences, repr() and strip()

  • 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() function

  • Method __str__() and overloading str() and print()

  • 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