B2.1a · Book pp. 296–310
Programming fundamentals, part 1
This is where the course starts to program. Every grey code box with a Run button is real Python: edit it, run it and break it.
What you need to know
- B2.1.1 Construct and trace programs using global and local variables of various data types.
- B2.1.2 Construct programs that extract and manipulate substrings.
🐍 Python or Java?
The exam lets you answer in either language. This site teaches Python, and notes where Java differs. Global and local variables are covered in B2.3.
B2.1.1 Variables and data types
A variable has an identifier, or name, and one current value. A new assignment overwrites the old value, which is then lost.
| Type | Python | Java | Examples |
|---|---|---|---|
| String | str | String | "Asha", "0812345678" |
| Integer | int | int | 42, −7 |
| Decimal | float | double | 3.5 |
| Character | A str of length 1 | char | "@" |
| Boolean | bool | boolean | True or False |
✏️ Python notes
- Python has no char type, as a single character is just a string of length 1.
- Python’s float is double precision, so it matches Java’s double.
- Python needs no declarations. In an exam, a comment can state a variable’s purpose.
- input() always gives a string, so use int() or float() before calculating.
Assignment and swapping
The value on the right is stored in the variable on the left, so count = count + 1 increases count by one. Java also has count++, which Python does not.
B2.1.1 Operators
| Operator | Meaning | Example |
|---|---|---|
+ − * | Add, subtract, multiply | 7 * 2 is 14 |
/ | Division | 7 / 2 is 3.5 |
// | Floor division | 7 // 2 is 3 |
% | Remainder | 7 % 2 is 1 |
< <= > >= | Compare sizes | 3 <= 3 is True |
== != | Equal, not equal | 3 != 4 is True |
and or not | Combine conditions | not (3 > 4) is True |
Binary operators take two operands. Unary operators, such as the minus in −5, take one. In Java, 7 / 2 gives 3 when both values are integers.
✏️ Book corrections
- The book says // truncates, “ignoring” the decimal part. It is floor division, which rounds down: −7 // 2 is −4, where truncation would give −3.
- It says the result is 3 whatever the data types are. But 7.0 // 2 gives 3.0, a float.
- Its Java line
float no1 = 7.0;will not compile. Write7.0for use double.
Try it: a = 8, b = 3. Work these out, then test them.
(a < b) or (a > 5)is Truenot (a >= b)is False(a < 8) and (b > 3)is Falsenot (a == b) or (a > b)is True
B2.1.2 Strings
Positions start at 0. A slice includes its start and stops before its end. A backslash escapes special characters: \" for a quote, \\ for a backslash and \n for a new line.
| Length | len(text) |
| Concatenate | a + b |
| Slice | text[9:16] |
| Last character | text[-1] |
| Replace | text.replace("is", "will be") |
| Remove outer spaces | text.strip() |
✏️ Book corrections
- The book says “S” in “Computer Science” is at index 8. That is the space, and S is at index 9. So its Java substring(8, 16) gives " Science" with a leading space.
- It says Java’s replace handles only a single character. It also accepts strings, and replaceAll is for patterns (regular expressions).
- It describes strip as removing trailing spaces. It removes leading and trailing spaces, as the book’s own example shows.
✍️ Challenge
Quick quiz
Exam-style questions
These are written for this site in the style of Paper 2. Answer on paper first, then open the mark points.
-
Identify an appropriate data type for a flight price and for whether a seat is taken. [2]
Mark points
- Price: float or double, as it can hold decimals [1].
- Seat taken: Boolean, as it is true or false [1].
-
Construct code to swap the values of a and b using a temporary variable. [3]
Mark points
- temp = a [1]
- a = b [1]
- b = temp [1]
-
A three-digit number is stored in n. Construct code that outputs the sum of its digits. [4]
Mark points
- hundreds = n // 100 [1]
- tens = (n // 10) % 10 [1]
- units = n % 10 [1]
- Output hundreds + tens + units [1]
-
Construct code that takes a first name and last name, and outputs the full name and its length without the space. [3]
Mark points
- Input both names [1].
- full = first + " " + last, then output it [1].
- Output len(first) + len(last) [1].
-
Outline why choosing the correct data type matters. [3]
Mark points
- It controls which operations are allowed [1].
- It affects memory use [1].
- It prevents errors, such as losing a phone number’s leading zero [1].