University of Glasgow · FACULTY OF COMPUTER SCIENCE

COMPSCI4039 Chap.2 Expressions and Type Casting

- one subject, every graph, every model, every mark
7 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 2 of 10 · COMPSCI4039

Expressions and Type Casting

Expressions and Type Casting covers how Python turns operators and operands into a single value, and how to move a value between types on purpose. Arithmetic follows familiar rules: brackets first, then multiplication and division, then addition and subtraction, with equal-ranked operators applied from left to right. Python adds two operators that are central to many exercises.

Floor division keeps the whole part of a quotient and the remainder operator gives what is left over, which together split quantities into full groups and leftovers and test divisibility. True division always returns a float, and mixing an int with a float always gives a float, a point the course reinforced when it corrected two of its own quiz answers.

Floats are approximations, so the lecture warns against using floor division or remainder on them and against storing money in floats. Casting functions convert values deliberately: int, float, str and bool. The most frequent need is converting keyboard input, which always arrives as a string, before calculating with it. Shortcut operators such as += are explained by writing them out in full.

In this chapter

What this chapter covers

  • 01

    Expressions, operands and operators

  • 02

    Precedence, brackets and left-to-right evaluation

  • 03

    True division, floor division and remainder

  • 04

    Float results and float approximation

  • 05

    Shortcut assignment operators

  • 06

    Casting with int, float, str and bool

  • 07

    Concatenating text with numbers

Worked example · free

Evaluate a mixed expression in the right order

Q [4 marks]. What value does 3 + 18 / 4 * 2 - 7 // 2 produce, and what is its type?
The tree evaluates from the leaves upward: multiplication and division finish first, then the addition, and the subtraction at the root combines 22.0 and 2.0 inThe tree evaluates from the leaves upward: multiplication and division finish first, then the addition, and the subtraction at the root combines 22.0 and 2.0 into the final 20.0.- gives 20.0+ gives 22.0/ gives 2.02.0* gives 20.06.03.05.04.0
The tree evaluates from the leaves upward: multiplication and division finish first, then the addition, and the subtraction at the root combines 22.0 and 2.0 into the final 20.0.
  • 1Division and multiplication run first, left to right: 18 / 4 is 4.5, then 4.5 * 2 is 9.0. This mark allocation is our own practice aid and not a published university scheme.
  • 1Floor division has the same rank: 7 // 2 is 3.
  • 1Addition and subtraction run last, left to right: 3 + 9.0 is 12.0, then 12.0 - 3 is 9.0.
  • 1A float took part, so the result is the float 9.0.
The expression evaluates to 9.0, a float, because true division produced 4.5 before anything was added.
Sia tip — Mark each operator with its rank before calculating, then work from the highest rank down.
Glossary

Key terms

Floor division
The // operator, which keeps only the whole-number part of a quotient.
Remainder operator
The % operator, which gives what is left after whole-number division.
Operator precedence
The ranking that decides which operator in an expression is applied first.
Shortcut operator
A combined form such as += that updates a variable using its own current value.
FAQ

Expressions and Type Casting FAQ

Why does 8 / 2 print 4.0 instead of 4?

In Python the single slash is true division and always returns a float, even when both operands are whole numbers and the division is exact. Use the double slash when you want whole-number division of two ints.

Why should I avoid // and % with floats?

Floats are stored as close approximations, so a value like 0.1 is very slightly off. Floor division and remainder then give surprising answers, such as 1.7 // 0.1 producing 16.0, which is why the lecture advises keeping these operators for integers.

What happens if I cast a word to an int?

Python can only convert strings that look like whole numbers. int("12") gives 12, but int("seven") stops the program with an error at that line, so check or control what users type before converting it.

How do I split a quantity into groups and a leftover?

Use floor division for the number of whole groups and the remainder operator for what is left. Sharing 17 pies among 5 people gives 17 // 5, which is 3 each, and a remainder of 2 pies left over. Multiplying the groups by the divisor and adding the remainder always rebuilds the original number, a handy check.

Study strategy

Exam move

Practise expressions in short, timed bursts. Write ten mixed expressions on paper, mark each operator with its rank, evaluate them, and then check in the interpreter, paying special attention to whether the answer should show a decimal point.

Make a small table of division results for pairs such as 29 and 6, 135 and 60, and 7 and 2, giving the true quotient, the floor quotient and the remainder, and confirm that the floor quotient times the divisor plus the remainder rebuilds the original number.

For casting, collect the cases that surprise people: bool of a non-empty string such as "0" is True, int of a float drops the fractional part, and adding a number to a string fails until str is applied. Rewrite every shortcut operator you meet in its long form before evaluating it.

When you read a lab task that involves user input, underline where a cast is needed; forgetting it turns 12 * 3 into a repeated string instead of 36.

Working through Expressions and Type Casting in COMPSCI4039? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI4039 Expressions and Type Casting question and get a clear, step-by-step explanation grounded in how COMPSCI4039 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 26 of your subjects at University of Glasgow, plus 1,000+ more Bibles across the AskSia library.
Sia - your COMPSCI4039 tutor, unlimited, worked the way the exam marks it
The full 4-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