Computer Science · Theme B: Computational thinking and problem-solving
B2.1 — Programming fundamentals
Computer Science · SL / HL · syllabus-mapped notes
CS_B2.1.1
Variables, data types and scope
Constructing and tracing programs with global and local variables of the five data types.
Full note — freeA variable is a named store for a value. You must be able to construct programs using variables and to trace them (follow each value by hand). The syllabus names five data types . The five data types Boolean value : True or False. char : a single character, for example 'A'. integer : a whole number, for example 42. decimal : a number with a fractional part, for example 3.14 (a float). string : a sequence of characters, for example "hello". Scope: global versus local A global variable is declared outside all functions and is visible everywhere. A local variable is declared inside a function and exists only while that function runs. e.g. rate = 0.2 # global (decimal) def net(pay): # pay is local (integer) tax = pay * rate # tax is local (decimal) return pay - tax print(net(100)) # 80.0 Tracing means writing a trace table: one column per variable, one row each time a value changes, so you can predict the output. Do not confuse declaring a variable inside a function (local) with reassigning a global. In Python, changing a global from inside a function needs the global keyword; otherwise you create a new local.
CS_B2.1.2
Extracting and manipulating substrings
Identifying and extracting substrings, then altering, concatenating or replacing them.
A substring is part of a larger string. You must be able to construct programs that identify, extract and then manipulate substrings. Identify and extract using slicing / index positions e.g. s = "IB Computer Science" first = s[0:2] # "IB" (extract by slice) i …
CS_B2.1.3
Exception handling techniques
Points of failure and the try/catch, try/except and finally constructs.
Exception handling lets a program respond to run-time errors instead of crashing. You must be able to describe how it manages potential points of failure. Points of failure (must include) Unexpected inputs : a user types text where a number is expected. Resource unavailability : …
CS_B2.1.4
Common debugging techniques
Trace tables, breakpoints, print statements and step-by-step execution.
Debugging is the process of finding and removing errors. You must be able to construct and use common techniques. (These are illustrative, not a closed set.) Trace tables Record each variable value step by step, by hand, to see exactly where the program deviates from …
