CSC108H1 Chap.1 Expressions, Types and Built-in Functions
Expressions, Types and Built-in Functions
What Week 1 starts with
The course opens in the Python shell and treats it as a calculator. An expression is code that produces one value, and every value has a type: int for whole numbers, float for numbers with a fractional part.
Python reduces an expression one operator at a time, choosing the operator with the highest precedence first.
Where marks slip
Tracing questions ask for a value and its type together, so 2 and 2.0 are different answers. Integer division and modulo give whole pieces and leftovers, while true division always returns a float.
Built-in calls evaluate their arguments first, from left to right, and max or min keep the type of the argument they choose.
How Week 1 is assessed
Worksheet 1 asks for Python expressions that turn a word problem into arithmetic, such as a percentage grade or a bill split evenly, and then for the value and type each produces.
Worksheet 4 asks for the results of built-in calls without running them, and for facts read from a help entry: which types round can return and how many arguments it accepts. Quiz 1 covered all of Weeks 1 to 3 on paper, so these hand-evaluation skills were tested from the first quiz onward.
What this chapter covers
- 01
Expressions, values and the shell prompt
- 02
The int and float types and the type function
- 03
Operator precedence from parentheses to addition
- 04
True division, integer division and modulo
- 05
Syntax errors, semantic errors and tracebacks
- 06
Built-in functions, optional arguments and nested calls
Worked example · free
Reduce an expression one operator at a time
- 1Apply ** first: 3 ** 2 becomes 9, giving 4 + 9 * 2.
- 1Apply * next: 9 * 2 becomes 18, giving 4 + 18.
- 1Apply + last: the result is 22, an int because every operand is an int.
Key terms
- Expression
- Code that Python evaluates to produce a single value.
- Operator Precedence
- The order in which Python applies operators when an expression contains several.
- Integer Division
- The // operator, which gives the whole number of times one value fits into another.
- Modulo
- The % operator, which gives the remainder left after integer division.
- Traceback
- Python's error report, whose last line names the kind of error that occurred.
Expressions, Types and Built-in Functions FAQ
Why does 8 / 4 give 2.0 rather than 2?
True division with / always produces a float in Python, even when the division is exact. Use // when you want the whole-number result as an int, and remember that a tracing question treats 2 and 2.0 as different answers.
Where does negation sit in the order of operations?
Below exponentiation and above multiplication. That is why -2 ** 2 squares first and then negates, giving -4, while (-2) ** 2 gives 4. Parentheses settle any doubt and make the intent clear to a reader.
What type does max return when given an int and a float?
It returns whichever argument is largest, unchanged, so the type is the type of that argument. max(2.5, 7) is the int 7 and max(7.5, 2) is the float 7.5; the other argument does not convert the result.
How do I read a Python error message?
Start with the last line of the traceback, which names the kind of error, such as ZeroDivisionError or TypeError. The lines above it point to where the problem happened. A SyntaxError means the code is not valid Python, while other errors appear only when the code runs.
What does round return when given one argument?
An int. The help entry for round explains that the result is an integer when the number of digits is left out, and has the same type as the number otherwise. So round(4.6) is the int 5, while round(4.567, 2) is the float 4.57.
Exam move
Write ten mixed expressions using every operator, including one negation and one ** next to *, and reduce each by hand one operator per line before checking it in the shell. Then do the same for nested built-in calls, writing the value of each argument before the outer call.
Keep a list of every case where your type was wrong; those are the cases to revisit before Quiz 1. Add three word problems of your own, each turned into one expression, and state the type of every answer.
Working through Expressions, Types and Built-in Functions in CSC108H1? Sia is AskSia’s AI Computer Science tutor — ask any CSC108H1 Expressions, Types and Built-in Functions question and get a clear, step-by-step explanation grounded in how CSC108H1 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.