CS1602Introduction to Computation
Lab 1Part 1 Foundations of ComputationAI Level 0

Setting Up and Running Your First Program

Install Python and VS Code, learn to run a .py file from the terminal, and submit once. Part B shows you chaos in ten lines — shift the starting value by one millionth and twenty-five rounds later nothing looks the same.

Download PDFDue:Homework 1 · Sat 10 Oct, 23:59
Contents

What this session is for

  • Get Python 3.13 and VS Code installed on your own machine
  • Run a program two ways: the Run button in VS Code, and a command in the terminal
  • Go through the submission process once
  • See for yourself how a tiny difference in a starting value gets amplified
  • Meet a handful of basic terminal commands
  • Walk through a complete submission once

1. Install Python

The course site hosts a copy of the official Python 3.13.15 installers. The campus network reaches python.org at unpredictable speeds, and the first ninety minutes are too short to spend waiting, so download them from here:

Your machineDownloadSize
Windows (almost certainly this one)python-3.13.15-amd64.exe28 MB
Windows on ARM (Snapdragon laptops)python-3.13.15-arm64.exe27 MB
macOS (Intel and Apple Silicon alike)python-3.13.15-macos11.pkg69 MB

These are byte-for-byte the files from python.org/downloads, kept locally.

Windows

Run the installer and tick Add python.exe to PATH on the very first screen, then click Install Now.

macOS

Download the .pkg installer and click through it.

The python3 that ships with macOS is there for the system’s own use and is often an older release. Once you have installed from python.org, use that one.

Check it worked

Open a terminal (PowerShell or Terminal on Windows, Terminal on macOS) and type:

python --version

You should see Python 3.13.x.

If the command is not found, try python3 --version on macOS and Linux. On Windows it means PATH was not set — reinstall with the checkbox ticked.

2. Install VS Code

Download it from code.visualstudio.com.

Then add one extension:

  1. Click the Extensions icon in the left bar (the four squares), or press Ctrl+Shift+X (Cmd+Shift+X on macOS)
  2. Search for Python
  3. Install the official one — publisher Microsoft, by far the most downloaded

It provides syntax highlighting, error checking, the Run button and the debugger.

3. Write your first program

  1. Make a folder somewhere for this course’s code. Keep the path free of spaces and non-ASCII characters — some tools handle them badly. Something like D:\cs1602 or ~/cs1602.
  2. In VS Code, File → Open Folder, and open it.
  3. Create a file called hello.py.
  4. Put this in it:
print("Hello world")
  1. Click the ▶ Run button in the top right. A terminal panel opens below and shows Hello world.

Now run it from the terminal

The Run button is convenient, but you should know what it does for you.

Press Ctrl+` (the backtick, below Esc) to open a terminal inside VS Code, and type:

python hello.py

Same output. This is what running a Python program actually is — the Run button just types that line on your behalf.

4. Five small experiments

With Python installed, work through the five things below in the interactive interpreter. Type python (or python3) in a terminal to get in — the prompt is >>> — and exit() or Ctrl+D to get out.

Your answers go on the “Setup check and first observations” answer sheet on the platform. Each is a single line to type; the point is not the typing, it is whether the result matches what you expected.

  1. Two kinds of quote. Run print("SJTU") and then print('SJTU'), and compare.
  2. Python’s own creed. Run import this and read it. These nineteen lines by Tim Peters are the community’s statement of taste, and this course keeps coming back to them.
  3. A very large number. Evaluate 2 ** 100.
  4. Two kinds of division. Evaluate 10 / 3 and 10 // 3.
  5. One addition. Evaluate 0.1 + 0.2.

For fun (not marked)

Put this in star.py and run it:

import turtle

tur = turtle.Turtle()
tur.speed(6)
tur.getscreen().bgcolor("black")
tur.color("cyan")
tur.penup()
tur.goto(-200, 50)
tur.pendown()

def star(t, size):
    if size <= 10:
        return
    for _ in range(5):
        t.forward(size)
        star(t, size / 3)
        t.left(216)

star(tur, 360)
turtle.done()

A five-pointed star, with smaller stars growing out of each edge, and smaller ones out of those — a fractal. Under twenty lines of code draw it, and the trick is that star calls itself. That is recursion, and L7 is devoted to it.

You are not expected to follow it yet. Change 360 and size / 3 and watch what happens.

5. Submitting

Open the grading platform and register with your student number. There is no access code — if your number is on the course roster, you can register. Then open Homework 1.

That page has two kinds of entry:

EntryWhat you hand inHow it is marked
Code problemone functionthe platform runs the tests and scores it immediately
Answer sheetmultiple choice and written answersthe choices are marked immediately; the written answers go into the grading report for a TA

Part A’s programming exercises are code problems. This week’s five experiments and the questions in Parts B and C are answer sheets — you answer them on the page itself; there is no separate document to hand in. Everything you write is folded into the grading report, which you download and submit to Canvas at the end of term.

Start a habit this week: submit something before every lab session ends, even if it is incomplete. The platform keeps your best result, and answer sheets can be revised and resubmitted as often as you like.


Part A — required

1-1 Introduce yourself

Write a program that uses print() to output three lines: your name, your student number, and one sentence of your choosing.

Zhang San
525030910001
Hoping to write code I'm actually happy with this term

1-2 Change of base

Use bin(), oct() and hex() to show your student number in base 2, 8 and 16.

1-3 Predict, then run

Print the results of the following. Guess each one before you run it, and see how you did:

7 / 2
7 // 2
7 % 2
2 ** 10

Write them down first, then run and check. Whichever one you got wrong goes on the “Part A · Arithmetic and reading errors” answer sheet, along with what you had expected. We cover all four operators properly in L2.

1-4 Break it on purpose

Produce each of the three errors below and paste the complete message onto the answer sheet:

  1. Misspell print as pirnt
  2. Use full-width parentheses: print("hello")
  3. Leave a quote unclosed: print("hello)

Then answer: what is the first line of each error? Does the line number it points at match where the mistake actually is?


Part B — going deeper: chaos in ten lines

You are not expected to understand the syntax yet. Type it in, run it, watch what happens. What this shows you is a property of computation itself, not of Python.

Create chaos.py:

x = float(input("Enter a number between 0 and 1: "))

for i in range(25):
    x = 3.9 * x * (1 - x)
    print(f"{i + 1:3}  {x:.17f}")

The last two lines need four extra spaces of indentation — that is how Python marks “these lines belong to the loop”. L4 covers it properly.

B-1 Run it

Enter 0.25 and run it. Note the last line — the next question compares against it.

That formula is the logistic map, a model from population biology: x is the current population as a fraction of what the environment supports, and each round works out the next generation. The 3.9 is the growth rate.

B-2 Nudge the starting value by one millionth

Run it again with 0.250001.

The two starting values differ by 0.000001. Comparing the two runs:

  1. How far apart are the two values at round 10?
  2. How far apart at round 25?
  3. From roughly which round can you no longer see any relationship between the two sequences?

B-3 Same starting value, two ways of writing the same formula

Replace the line inside the loop with this, and enter 0.25 again:

x = 3.9 * x - 3.9 * x * x

3.9 * x * (1 - x) and 3.9 * x - 3.9 * x * x are algebraically identical — expand the brackets and check.

Answer:

  1. Do the two versions give the same value at round 25?
  2. From which round does a difference appear?
  3. Why do you think that is? (A guess is fine — the answer is in L2, part four.)

Part C — fundamentals: your first terminal session

Sooner or later you will run programs on a server with no graphical interface, where the terminal is all there is. Plenty of tools — installing libraries, version control, running tests — are command-line programs to begin with.

Four commands

CommandWhat it doesMnemonic
pwdwhich directory am I inprint working directory
lslist what is in this directorylist
cd namemove into a directorychange directory
cd ..go up one level.. means the parent

All four work in Windows PowerShell (ls and pwd are aliases there).

C-1 Walk through it

Do these in order and copy each command and its output straight out of the terminal onto the answer sheet (no screenshot needed):

  1. pwd — see where you are
  2. cd to the folder holding your course code
  3. ls — check that hello.py and chaos.py are both there
  4. python hello.py — run it
  5. cd .. to go up a level, then pwd again to confirm you moved

Checklist

Before you submit, go through this:

  • python --version prints 3.13.x
  • the Python extension is installed in VS Code
  • the Run button works on hello.py
  • python hello.py works on the same file
  • the five experiments are done and the “Setup check” answer sheet is submitted
  • all four Part A problems submitted
  • the “Part B · Chaos in ten lines” answer sheet is submitted
  • the “Part C · Your first terminal session” answer sheet is submitted
  • you have joined the course group

When it will not install

SymptomUsually meansFix
'python' is not recognizedPATH not set on WindowsRerun the installer → Modify → tick Add to PATH
zsh: command not found: pythondifferent command name on macOSTry python3, or check where it installed
Run button is greyed outPython extension missingInstall the official Microsoft Python extension
VS Code cannot find an interpreternone selectedCtrl+Shift+P → Python: Select Interpreter → pick 3.13
can't open file at runtimeterminal is in the wrong directorypwd to check, cd to the file’s folder

Still stuck? Put your hand up, or post in the course group. Include a screenshot of the full error and say which OS and version you are on.