Skip to article frontmatterSkip to article content
from __init__ import install_dependencies

await install_dependencies()

To access the course homepage, visit:

If you have not yet registered the course, visit the static site:

Notebooks

Lecture and lab materials are written in the form of Jupyter notebooks, which provide an interactive literate programming experience, which is a significant benefit in making programs easy to understand:

  • Jupyter notebooks allow programs to be mixed with other contents, including figures, videos, and formatted textual explanations.
  • The notebooks can also be opened in a Jupyter server, which enables users to edit and run the programs in addition to other contents.

How to run notebook cells?

If you already have your notebook opened in a Jupyter server, let’s run some code. Otherwise, jump to the next section to learn how to fetch and open notebooks.

To run a cell, select the cell and press Shift + Enter. The cell prompt with change from [ ] to [*]. When the run completes, the asterisk will be changed to a number like [1]. Try running the following cell to import the Mathematical Animation Engine (Manim).[2]

import os

if not os.getenv(
    "NBGRADER_EXECUTION"
):  # Skip the code or auto-grading may take too long to complete.
    import manim

After the import is complete, run the following cell to create an animation:

%%manim -qm --progress_bar=none --disable_caching --flush_cache -v ERROR Welcome
class Welcome(manim.Scene):
    def construct(self):
        self.play(manim.Write(manim.Text("Welcome to CS1302!")))

As you can see, the code creates a scene that displays the message Welcome to CS1302! with a writing animation.[3] To verify that the program runs live, feel free to modify the message to anything you want.

How to fetch and open notebooks?

To fetch the notebooks, follow the links to the notebooks on the course homepage such as the one in Table 1.

After clicking a notebook link, you may be asked to

  • login with your CityU account if you are not yet logged into Canvas, and
  • specify a server option if you do not yet have a running Jupyter server.

Select the Default option from the list of available Jupyter servers, and click Start to begin your session.

If the server is spawned successfully, the JupyterLab interface should appear.

As you start your programming journey, you will inevitably encounter software bugs, especially in highly sophisticated software such as JupyterLab. Be aware of their existence, and develop a positive attitude towards bugs, as well as cultivate a proper skill set to inspect and debug them effectively.

As an example to inspect a computer program, we will explain how the notebook link works. First, run the following code to start the notebook link generator app.

from notebook_link_generator import setup_notebook_link_widget

display_widget = setup_notebook_link_widget(canvas_id="60298")
display(display_widget)

Change the Notebook path above to Lecture1/Introduction_to_Computer_Programming.ipynb and observe the live update to the generated link.

Now, run the following two cells to see how a notebook link is generated stey-by-step:

%load_ext divewidgets
%%optlite -r
from urllib.parse import quote

# Course parameters
course_id = "cs1302_24a"
notebook_path = "Lecture1/Introduction_to_Computer_Programming.ipynb"
canvas_id = "60298"

# Base URLs
notebook_repo = f"https://github.com/dive4dec/{course_id}"
course_server = "https://dive.cs.cityu.edu.hk"
course_homepage = f"https://canvas.cityu.edu.hk/courses/{canvas_id}"

# Notebook URL generation
# 1. Construct the URL for git-pull service to clone or pull the notebook repository
gitpull_url = (
    f"{course_server}/{course_id}/hub/user-redirect/git-pull?"
    + f"repo={quote(notebook_repo)}"
    + f"&urlpath={quote(f'lab/tree/{course_id}/{notebook_path}')}"
)
# Construct the LTI (Learning Tools Interoperability) launch URL for JupyterHub
lti_url = (
    f"{course_server}/{course_id}/hub/lti/launch?" + f"custom_next={quote(gitpull_url)}"
)
# Construct the LTI external tool URL to open the notebook
notebook_url = (
    f"{course_homepage}/external_tools/retrieve?display=borderless&"
    + f"url={quote(lti_url)}"
)
notebook_url

After running the code, you can:

  • see the generated notebook link for Lecture1/Introduction_to_Computer_Programming.ipynb in the output cell;
  • inspect parts of the code in the input cell by placing the cursor there and press Shift + Tab; and
  • visualize the execution step-by-step by clicking Next > (< Prev) to go the the next (previous) line.

Assignments

How to complete an assignment?

The notebooks can be edited in JupyterLab to include your answers for submission. If this is your first time using JupyterLab, take a look at the official video tutorial:

For more advanced features:

  • Checkout the Help menu items
    • Show Keyboard Shortcuts to try some of the shortcuts to see their effect, and
    • Jupyter Reference to open the user guide in a new tab.
  • Try also the MyST Markdown syntax to format your notes.

In learning a new programming language, the first program to write is often the “Hello, World!” program, which says Hello to the world. As your first lab exercise, you will write such a program in python.

The following code cell is a solution cell. Since you are not expected to know python yet, you can simply expand the hint above and copy the answer to the solution cell instead.

def say_hello():
    # YOUR CODE HERE
    raise NotImplementedError

say_hello()

To test your program:

  • Run your program by selecting the solution cell and press Shift + Enter.
  • Then, run the following visible test to check whether your program prints the correct message.
# Run this test cell right after running your "Hello, World!" program.
import io
import sys

old_stdout, sys.stdout = sys.stdout, io.StringIO()
say_hello()
printed = sys.stdout.getvalue()
sys.stdout = old_stdout
assert printed == "Hello, World!\n"

The test returns an assertion error if your program does not print the correct message.

How to submit?

You normally have at least 5 days to work on the lab after your lab session.

  • You can check the due dates of all the labs from the course homepage.
  • You may seek help from us or your classmates. However, you must write your own solution and indicate who your collaborators are by including their fullnames or EIDs such as
    COLLABORATORS: xfwong2, mklee3, GPT-4o
    The policy applies to LLM as well.

Before you submit, it is a good idea to make sure everything runs as expected:

  1. Git-pull the notebooks: Follow any one of the link on the course homepage to a notebook, which will git-pull any updates/corrections to (all) your notebooks.
  2. Save the notebook: Unsaved changes are not submitted, even though they are in the memory.
  3. Restart the kernel: Kernel\to Restart Kernel... to have a clean state before running visible tests.
  4. run all cells: Run\to Run All Cells to double check the results of the visible tests are as expected.

Re-executing cells in order in a Jupyter Notebook ensures that all dependencies and state changes are correctly applied, preventing misleading results from out-of-order execution.

To submit your notebook:

  1. Select the menu item Nbgrader\toAssignment List.
  2. Expand the Lab folder and click the validate button next to the notebook(s) to check if all the visible tests pass.
  3. Click Submit to submit your notebook.

References

Reading is important in learning a new language. This is especially so for learning a programming language, whose syntax has a very precise structure and interpretation. Each topic will have some required readings listed on the course homepage.

Table 2:An example of required readings on the course homepage.

The textbook by Halterman in Table 2 teaches Python programming using Python version 3.4.[5] As a programming language can evolve quickly over times, especially for a popular language like Python, it is important to be aware of the version you are using, and supplement your readings with the documentations of the newer versions. In particular, check out the documentations for:

If you have more time, further supplement your learning with materials from similar introductory python programming courses at other universities:[1]

As a student at CityU, you also have access to a wide range of library resources. A useful e-learning resource to help you dive deeper into the cutting-edge computing technologies is LinkedIn Learning:

  1. Open the LinkedIn Learning page.
  2. Click LinkedIn Learning Login to login with your CityU account.

After logging in, take a look at the following relevant online courses:

Footnotes
  1. Importing a library in programming is like bringing a toolbox into your workshop, giving you access to all the tools you need to complete your tasks.

  2. If you are interested in what the first line does, it sets the video quality to medium while disabling caching and progress bars, and setting the verbosity to reporting only ERROR but not other critical information.

  3. The VSCode interface is actually code-server, so some VSCode extensions may not be listed under the extension panel. You may still try to install them using the command install-vscode-extension in a terminal. This is only for advanced users who know what they are doing, or the risk of breaking things can be rather high...

  4. See the list of python versions at https://www.python.org/doc/versions/

  5. Note that these assume students have no prior programming experience but they may different topics than our course does.

  6. After clicking the link, CityU students can further link to the full text from the View Online section of the opened CityU library page.