The Hong Kong Polytechnic University · FACULTY OF INFORMATION TECHNOLOGY

MM1041 Chap.11 Python Programming Fundamentals

- one subject, every graph, every model, every mark
7 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 11 of 11 · MM1041

Python Programming Fundamentals

Why Python carries weight here

Fundamental Python programming skills are one of the course's five learning outcomes. The published schedule places two Python sessions in Weeks 12 and 13, the Python Exercises in Week 13 are a graded component, the exam rubric assesses Python skills, and the group project names Python applications as one possible analysis method.

The course recommends Google Colab and Kaggle notebooks, which run in a web browser, so no installation is needed.

Values, variables and operators

A Python program works on values: whole numbers, decimals, text strings and the truth values True and False. A variable is a name bound to a value by a single equals sign.

Arithmetic uses the familiar operators, but division comes in three forms: a single slash gives the exact quotient, a double slash gives the quotient rounded down, and the percent sign gives the remainder. The remainder is what tests whether a number is even.

Conditions and loops

An if statement runs a block only when its condition is True, with optional elif and else branches.

Conditions use comparison operators such as double equals and greater-than-or-equal, and combine with and, or and not. A for loop repeats a block once for each item in a sequence, which is how a script totals a list of sales or checks every order against a threshold.

Indentation marks which lines belong to each block.

Lists and functions

A list stores values in order, with positions counted from zero, and len gives its length. A function packages steps under a name, takes parameters and returns a result, so the same logic can be reused on new data.

These pieces are enough to write small analyses, such as counting large orders or averaging daily sales, that echo the data analytics ideas of the earlier chapters.

Working in a notebook

In Colab or Kaggle, code sits in cells run one at a time, and variables persist in the order cells were run. Restarting and running everything from the top is the first fix for a confusing result.

Comments after a hash sign explain each step to your group and to a marker.

In this chapter

What this chapter covers

  • 01

    Why Python appears in the learning outcomes

  • 02

    Values, types and variables

  • 03

    Division, floor division and remainder

  • 04

    If, elif and else

  • 05

    For loops over lists

  • 06

    Functions with parameters and return values

  • 07

    Notebooks in Colab and Kaggle

Worked example · free

Trace a short sales script

Q [6 marks]. AskSia-authored practice. Trace this logic and state what it prints: a list holds weekly sales of 90, 130, 110 and 70; a loop counts the weeks with sales of at least 100; the script prints the count and then prints Good month if the count is at least half the number of weeks, otherwise Weak month. The marks shown are an AskSia study allocation and are not the University's marking scheme.
What the loop does.What the loop does. Each pass takes the next value from the list and adds it to the running total, which ends at 410; the average is 102.5, so the script prints Target met.Tracing total = total + s through the listpass 1: s = 120total 120pass 2: s = 80total 200pass 3: s = 150total 350pass 4: s = 60total 410
What the loop does. Each pass takes the next value from the list and adds it to the running total, which ends at 410; the average is 102.5, so the script prints Target met.
  • 3Trace the loop and give the count.
  • 3Evaluate the condition and give the final output.
The loop visits each value in turn. 90 is below 100, so the count stays at 0; 130 meets the threshold, so the count becomes 1; 110 also meets it, so the count becomes 2; 70 does not, so the count ends at 2. The script prints 2. There are four weeks, and half of four is 2, so the condition that the count is at least half the number of weeks is True, because 2 is greater than or equal to 2. The script therefore prints Good month on the second line.
Sia tip — Trace a loop on paper with one row per pass and one column per variable; boundary values such as exactly 100 or exactly half are where mistakes hide.
Glossary

Key terms

Variable
A name bound to a value so that later lines of a program can use it.
Floor Division
Division that rounds the quotient down to a whole number, written with a double slash.
Remainder Operator
The percent sign, which gives what is left over after division.
For Loop
A structure that repeats a block of code once for each item in a sequence.
Function
A named block of code that takes parameters and returns a result.
List Index
The position of an item in a list, counted from zero.
Notebook Cell
A block of code in a notebook such as Colab or Kaggle that is run on its own.
FAQ

Python Programming Fundamentals FAQ

Do I need to install anything to program in Python for this course?

No. The course recommends Google Colab and Kaggle notebooks, which run in a web browser with nothing to install. Sign in, create a notebook and type code into a cell. Practising in the same tool you will use for the graded exercises removes one source of stress on the day.

Why does my code say a variable is not defined when I can see it?

In a notebook, a variable exists only after the cell that creates it has been run. If you edited or reordered cells, or restarted the session, the definition may not have run yet. Restart and run all cells from the top; if the error remains, check the spelling, since Python treats upper and lower case as different names.

How do I test whether a number is even in Python?

Use the remainder operator: number % 2 == 0 is True exactly when the number is even. Floor division, number // 2, answers a different question, the whole-number half, so comparing it with zero is True only for 0 and 1. Try any rule on a few numbers before trusting it in an exercise.

Study strategy

Exam move

Type every snippet from this chapter into a Colab notebook and change one value in each to see what happens. Then write three tiny functions of your own, a total, a count above a threshold and an average, and test each on a list of five numbers where you already know the answer.

Working through Python Programming Fundamentals in MM1041? Sia is AskSia’s AI Information Technology tutor — ask any MM1041 Python Programming Fundamentals question and get a clear, step-by-step explanation grounded in how MM1041 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 8 of your The Hong Kong Polytechnic University subjects - and 1,000+ Bibles across every Australian university.
Sia - your MM1041 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