University of Toronto · FACULTY OF COMPUTER SCIENCE

CSC108H1 Chap.1 Expressions, Types and Built-in Functions

- one subject, every graph, every model, every mark
6 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 1 of 10 · CSC108H1

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.

In this chapter

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

Q [3 marks]. AskSia-authored practice. Without running code, show each intermediate line Python produces for 4 + 3 ** 2 * 2 and give the final value with its type. The marks shown are an AskSia study allocation, not an official University marking scheme.
Figure 1.1.Figure 1.1. Each box is the whole expression after one operator has been applied. Exponentiation goes first, multiplication second and addition last, so the starting line shrinks to the single integer 22.4 + 3 ** 2 * 2** binds tightest4 + 9 * 2then *4 + 18then +22one int value
Figure 1.1. Each box is the whole expression after one operator has been applied. Exponentiation goes first, multiplication second and addition last, so the starting line shrinks to the single integer 22.
  • 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.
Python rewrites the line three times: 4 + 9 * 2, then 4 + 18, then 22. The answer is the int 22. Adding first would turn 4 + 3 into 7 and give 98 instead, so the intermediate lines are what show that precedence was applied.
Sia tip — Underline the operator you apply on each line before rewriting it; if two underlined operators share a line, you skipped a step.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 21 of your University of Toronto subjects - and 1,000+ Bibles across every Australian university.
Sia - your CSC108H1 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works