City University of Hong Kong · FACULTY OF INFORMATION TECHNOLOGY

IS5312 Chap.1 Python Foundations and Numeric Expressions

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

Python Foundations and Numeric Expressions

This chapter builds the first dependable Python workflow: identify a literal and its type, convert user input at the boundary, apply arithmetic operators in the correct precedence order, and verify mixed-unit results by reconstruction. Integer division and modulus are treated as a paired method for extracting whole units and remainders.

The worked material also separates a calculated value from the way it is rounded or displayed.

In this chapter

What this chapter covers

  • 01

    Integer and floating-point literals

  • 02

    Input and explicit type conversion

  • 03

    Arithmetic operators and precedence

  • 04

    Integer division and modulus

  • 05

    Rounding and reconstruction checks

Worked example · free

Decompose a total number of seconds

Q [8 marks]. The marks shown are an AskSia study allocation and are not the University marking scheme. Convert 7,385 seconds into hours, minutes and seconds, then check the result.
The conversion boundary matters.The conversion boundary matters. User input is a string, so a numeric calculation begins only after the program converts a valid text value to the required numeric type.Input arrives as text until conversion succeedsinput()string valuefor example, "42"int()number 42Adding text to a number raises TypeError.Strings concatenate or repeat; numbers calculate.
The conversion boundary matters. User input is a string, so a numeric calculation begins only after the program converts a valid text value to the required numeric type.
  • 3Find hours and the remainder.
  • 3Find minutes and seconds.
  • 2Reconstruct the total.
The result is 2 hours, 3 minutes and 5 seconds because 7,385 // 3,600 is 2, the remainder is 185, and 185 splits into 3 minutes with 5 seconds left. Reconstruction returns 7,385. This score is an independent AskSia practice allocation rather than a University marking scheme.
Sia tip — Reconstruct every mixed-unit answer in the original unit.
Glossary

Key terms

Numeric Literal
A fixed numeric value written directly in source code.
Precedence
The rule that determines how an expression is grouped and evaluated.
Modulus
The remainder produced by integer division.
FAQ

Python Foundations and Numeric Expressions FAQ

Why does input need conversion?

The input function returns text, even when the characters are digits. Numeric calculation therefore requires an explicit conversion after validation. Validate the conversion before attempting arithmetic so the failure is diagnosed at the correct boundary.

When should I use modulus?

Use modulus when the remainder matters, especially after extracting whole units with integer division. Check that the remainder is smaller than the divisor and reconstruct the original total.

How do I test precedence?

Choose values with an obvious hand-computed result and add parentheses that state the intended grouping explicitly. Compare the grouped and ungrouped expressions with the same hand-calculated test values.

Study strategy

Assessment move

Use a seven-part practice cycle. First, copy one short numeric expression and place brackets around the group Python evaluates first. Write the intermediate result under that group, continue outward, and record whether each value is an integer, float or string. Second, create a companion expression in which one pair of parentheses changes the answer; calculate both versions by hand before running them.

Third, practise the integer-division and modulus pair with several totals. For each case, state the unit size, compute the whole-unit count, compute the remainder and verify that the remainder is at least zero and smaller than the unit size. Reconstruct the original total from the two outputs. Fourth, take a program that reads user input and mark the precise conversion boundary.

Test digit text, decimal text, empty text and a nonnumeric value so conversion and semantic validation are not confused. Fifth, keep calculation and presentation separate. Calculate with the unrounded value, then round only the displayed result unless the task explicitly requires rounded values in later steps. Compare both outcomes with a value that has several decimal places.

Sixth, build a small error log with four columns: input, predicted result, actual result and first divergent operation. Classify each divergence as type, precedence, divisor, quotient, remainder or formatting. This prevents random edits because every repair answers a named failure. Seventh, close the chapter by writing a mixed-unit converter from a blank cell.

Before coding, write the decomposition equations in words, choose variable names that contain the unit, and prepare three tests: zero, exactly one large unit, and one large unit plus a remainder. A correct program should pass all three and reconstruct every input. When revising for the lab test, repeat the same cycle with new numbers rather than memorising one finished program.

The transferable target is the reasoning chain: input has a type, conversion changes that type, precedence groups operations, integer division finds whole units, modulus preserves what is left, and reconstruction checks the whole result. Add a final oral trace after each session. Explain one expression without looking at the code, naming the type and value after every operation.

Then ask what would change if the input were negative, exactly divisible by the unit size, or entered as decimal text. These variations test whether the rule is understood beyond one friendly example. For a mixed-unit program, compare total zero, one less than a full unit, exactly one full unit and one more than a full unit. The quotient and remainder should change at the boundary in a predictable way.

Also practise reading error messages from the final line upward: identify the failed operation, inspect the values that reached it and change only the earliest incorrect assumption. End by rewriting one calculation with descriptive variable names and a separate display statement. This reinforces that a numeric answer, its type and its printed wording are related but distinct parts of a reliable program.

A+Everything unlocked
Unlocks this Bible + all 31 of your subjects at City University of Hong Kong, plus 1,000+ more Bibles across the AskSia library.
Sia - your IS5312 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