← IB Computer Science

B2.1a · Book pp. 296–310

Programming fundamentals, part 1

Your level

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.

 

Next: variables →

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.

TypePythonJavaExamples
StringstrString"Asha", "0812345678"
Integerintint42, −7
Decimalfloatdouble3.5
CharacterA str of length 1char"@"
BooleanboolbooleanTrue 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.

🔬 Lab: swap two cups

 

Next: operators →

B2.1.1 Operators

OperatorMeaningExample
+ − *Add, subtract, multiply7 * 2 is 14
/Division7 / 2 is 3.5
//Floor division7 // 2 is 3
%Remainder7 % 2 is 1
< <= > >=Compare sizes3 <= 3 is True
== !=Equal, not equal3 != 4 is True
and or notCombine conditionsnot (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. Write 7.0f or use double.
 
Try it: a = 8, b = 3. Work these out, then test them.
  • (a < b) or (a > 5) is True
  • not (a >= b) is False
  • (a < 8) and (b > 3) is False
  • not (a == b) or (a > b) is True

Next: strings →

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.

🔬 Lab: slice a string

Lengthlen(text)
Concatenatea + b
Slicetext[9:16]
Last charactertext[-1]
Replacetext.replace("is", "will be")
Remove outer spacestext.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

 

Next: review →

Quick quiz

1. A variable is…
2. What is the best type for a phone number?
3. count = count + 1 is called…
4. What does 17 % 5 give?
5. What does 17 // 5 give in Python?
6. What does -7 // 2 give in Python?
7. To swap a and b you need…
8. If text = "Python", text[1:4] is…
9. If text = "Python", text[-2:] is…

Exam-style questions

These are written for this site in the style of Paper 2. Answer on paper first, then open the mark points.

  1. 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].
  2. 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]
  3. 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]
  4. 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].
  5. 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].