Friday, December 19, 2025

Week 8 Learning Summary

Week8 subject 1: Install PyGame

python –m pip install pygame

Week8 subject 2: The game class

A class must defined for each game, which contains:

  • an initialiser which calls pygame.init();
  • a method .run_game() which is called to run the game.  It contains:
    • a while True loop for the game,
    • an event loop which process player events
    • pygame.display.flip() to draw the screen

Week8 subject 3: The ship class

A Ship class is added to manage your ship.  Then the game class must be updated to include controls related to the ship.

Friday, December 5, 2025

Week 7 Assignments

1. The Zoo

You've recently quit the school library job because you get bored of the routines.  It so happens that the PB2EZ (Python Beginner to Expert Zoo) has an opening for the development of a software system called ZOO (Zoo Objective Organiser).

The PB2EZ gives you a manual of how animals are classified in the zoo. 

Animal Classification for Children: Classifying Vertebrates and  Invertebrates for Kids - FreeSchool 

They know that you can program Python.  They want you to first build this classification into your program by creating a hierarchy of Classes.  In addition to the picture, they've specified:

  • Each animal has a name and an age.
  • Each animal can move.
  • Each vertebrate can 'strechback'.
  • Each vertebrate has a number of sections of their spine.
  • Each fish has a number of gills.
  • Each fish can 'blub'.
  • Each bird can 'chirp'.
  • Each bird has a feature colour.
  • Each mammal can milk.
  • Each amphibian has a preferred habitat.
  • Each reptile can crawl.
Please build the Python program for the ZOO that defines all the types of animals in GSPLZ as classes, with their data members and methods defined.  

Week 7 Learning Summary

Week 7 subject 1: Classes

  • 'Objects' are grouped into Classes.  Classes define the common properties and capabilities of objects.  Objects are 'instances' of their respective Classes.  Example: 'Cars' is a Class, and my 2020 BMW 320i is an object of this Class, my friend's 2018 Honda CRV is another object of this Class.
  • Instances of Classes are accessed in Python by declaring variables of a Class (objects).  A Class is essentially a type, like int or string.
  • Just like there are different levels of group objects in real life, there can be hierarchies of Class definitions.  Objects can be instances of different Classes.

Inheritance and Polymorphism in Python

  • Parent Classes / Child Classes; Base Classes / Sub Classes.  
  • There's inheritance between parent and child classes.
  • Define Classes, use pass to indicate you don't define the contents of the Class at this moment;

Class Car:
    pass
 
Class Car(Vehicle):
    define drive():
        pass
 
    define park():
        pass

  • You can define variables and functions inside Classes. Variables are called Attributes, functions are called Methods of Classes.
  • Note: because of inheritance, instances of child classes can access members of parent classes.

Week 7 subject 2: Attributes

  • Attributes of Classes are created when they are assigned values.
  • Attributes can be dynamically added or deleted (with del statement).
  • Parent and child Classes can have Attributes with the same names, when referred to, child will override parent.
  • ADVANCED: Attributes can be attached to the Class as well as the Instance.  In the former they are called Class Attributes, latter Instance Attributes.  Class Variable values are common across all instances of the Class, while Instance Attributes values are local to the exact instance.

class Car(vehicle):
    NumOfWheels = 4
    def move(self, direction, distance):
        pass
 
myCar.NumOfWheels = 5
print(Car.NumOfWheels, myCar.NumOfWheels)
myCar = Car()

Week 7 subject 3: Methods

  • Methods are functions attached to Classes.  Like functions they need to be defined before being used.
  • Methods have 1 implicit parameter: self.  self means the Instance itself.  methods use the self variable to access other members of the Instance.

class Car(vehicle):
    NumOfWheels = 4
    def move(self, direction, distance): 
         pass
    def showwheels(self):
        print(self.NumOfWheels)   

  • Like Attributes, parent and child classes can define methods with the same names.  When called, child will override parent.
  • ADVANCED: Methods can also be added to Classes after initial definition of Classes.  Methods can be attached to both the Class and the Instance.  This is too complex, don't try.

Week 7 subject 4: Python built-in functions

  • abs(x): find out absolute values
  • bool(x): returns truth value
  • dir(object): returns all information(attributes) of the object
  • help(request): evoke the online help system
  • eval(expression): run and return result
  • exec(object): run expression or program(!)
  • float():
  • int():
  • len():
  • min() and max():
  • range():
  • sum():
It is essential skill to be able to look up reference websites to continually learn Python.  Example: Python Docs.

    • constants defined to be false: None and False

    • zero of any numeric type: 00.00jDecimal(0)Fraction(0, 1)

    • empty sequences and collections: ''()[]{}set()range(0)

Friday, November 21, 2025

Week 6 Assignments

1. Mind reader:

Your computer tells you it has mind reading capabilities.  You certainly don't buy that because you go to the Opportunity Class.  Your computer suggests a game with which it will show you that it reads your mind.

You give it a range, like 1..100, and the computer guesses an integer within that range.  All you need to do is tell it 'too large' or 'too small'.  You two repeat this Q&A until the computer guesses the correct number you have in mind.  By doing so you are able to tell if the computer can really read your mind, or it's simply doing maths.

Input 2 integers that indicate the range.  For each integer the computer guesses, input 'too large''too small''correct', or 'quit'.  The game ends when the input is 'correct' or 'quit'.  The computer outputs the integer you have in mind, and the number of guesses taken.  If the computer finds out you are trying to cheat, it also ends guessing and outputs 'you can't fool me!'.

2. Kitty:

Follow the definition of the Dog class, create a Cat class which describe this adorable animal which:

  • has a name
  • an age
  • a breed
  • a colour
  • once first adopted (instantiated), can populate its properties based on input
  • can output it's own properties, by saying like 'my name is Kitty'
  • can purr, and say 'I'm happy that's why I'm purring'
  • can curl, and say 'zzz...'

3. Cat breeder:

You started a cat breeder business.  You want to keep track of all your cats by using Classes in Python.  Yesterday a batch of new kitties arrived, together with a file describing them.  File name is kitties.txt, format is

            name     age    breed    colour
            name     age    breed    colour
            name     age    breed    colour
            name     age    breed    colour
            ...

Please read from the text file, use the Cat class created in the previous problem to capture all the information.  (*think: now you have more than one cat, you'll have more than one Cat class variables.  Where should you put all these variable?)  

Note that your kitties, i.e., variables of this Cat class, in addition to what they can already do in the previous problem, can also to the following:

  • Get into the front of a line
  • Get into the end of a line
Once you get them all in a line, please print their names in their current order.

Week 6 Learning Summary

Week 6 subject 1: Scope of variables

  • Variables have scopes, i.e., parts of the program where they can be referenced.
  • There are 2 types of variables based on their scopes:
    • Global variables: those defined outside any function, and
    • Local variables: those defined inside a function
  • Global variables are valid inside functions, but local variables aren't valid outside functions.
  • All variables must be first defined before referenced, within their respective scopes.
  • Functions are like global variables, they must be defined before being called.
  • Expand your knowledge by doing research of the concept Namespace.

Week 6 subject 2: Python is an OOPL

  • Traditionally programming languages are divided into 2 wide categories: procedural programming languages and object-oriented programming languages.  Earlier languages are typically procedural languages, including BASIC, C, Fortran and many others.  More recent languages are often OOPLs, among which there are C++, Java and Python.
  • Programming in procedural languages is like writing a cook book, which includes a series of steps that are executed either sequentially or according to defined control flows.
  • On the other hand OOPLs mimic the real world by defining 'objects' and how they interact with each other, so that we can stay reasonably far away from the tedious work of recreating all the 'steps' each time we need to describe the interactions, and let the 'objects' do it according to their definitions.

Procedural Programming and Object-Oriented Programming in C++ - Scaler  Topics

Friday, November 14, 2025

Week 4 Assignments

1. LED

You have a friend, who sells LED displays.    


He heard you learned Python and seeks your help with developing a driver program for the programmable LED display.  Basically you'll output a 'dot matrix' for any input number 0-9.  For example for number 369, you output:

*** *** ***
  * *   * *
*** *** ***
  * * *   *
*** *** ***

Input a positive number.  Output the 'dot matrix' format suitable for the LED display.

Extension 1:
If the number is very long, it'll have to be wrapped onto multiple lines.

Extension 2:
There are different kinds of LED displays which take different kind of characters as input.  Please get the user to input the character to use, and then output the 'dot matrix' using that character.  Example, the input character is 'O', then output becomes:

OOO OOO OOO
  O O   O O
OOO OOO OOO
  O O O   O
OOO OOO OOO

2. Everything has a price

Johnny English (2003) - IMDb

You are a secret agent.  You buy and sell classified information for a living.  Such classified information has several properties:

From country: a string of letters;
To country: a string of letters;
- Classification level: a number between 1 and 5;
Financial value: a number between 1 and 100.

When a piece of information is being traded, its price is calculated as follows:

1. You add values of all the letters in the from country, 'a' being 1 and 'z' being 26, to get a FC value;
2. You add values of all the letters in the to country, to get a TC value;
3. You use the Classification level C and Financial value F to calculate the CF value as follows: add up the squares of numbers from 1 to C, then add up the square roots of numbers from 1 to F, then add these 2 to get a CF value;
4. get the Price = (FC + TC) * CF

Get a list of classified information from an input file: jamesbondsclassifiedinformationforsaledontshowanybodybecauseitsstillnotencrypted.txt.  Each line contains the from country, to country, classification level and the financial value.  For each piece, write the calculated price to prices.txt.

First try to write the program without using functions.  Then rewrite your program using functions.  See if that simplifies your life. 

3. Scopes of variables

Write some simple functions for example adding 2 numbers.  Try different scenario like accessing values of local variables within / outside the functions, and accessing values of global variables within / outside the functions.

Also try changing values of variables within the functions, then see what happens to them outside the functions.

Week 3 and 4 Learning Summary

Week 3 and 4 subject 1: Functions

  • Function is a block of code with:
    • A name,
    • Zero or more parameters,
    • Zero or more return values.
  • Functions are useful for:
    • Eliminating repetition, and
    • Simplifying code, making it easier to read.
  • The same number of parameters need to be provided when calling a Function, as the number of parameters in the definition of the function.
  • You can specify default values of parameters in the definition.  When there are default values, less parameters can be provided when calling.
  • Use the return statement to return values to the caller.  You can return multiple values:
    • In the form of a tuple, i.e., multiple values separated by commas, or
    • In a list.