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.
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 machine | Download | Size |
|---|---|---|
| Windows (almost certainly this one) | python-3.13.15-amd64.exe | 28 MB |
| Windows on ARM (Snapdragon laptops) | python-3.13.15-arm64.exe | 27 MB |
| macOS (Intel and Apple Silicon alike) | python-3.13.15-macos11.pkg | 69 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:
- Click the Extensions icon in the left bar (the four squares), or press
Ctrl+Shift+X(Cmd+Shift+Xon macOS) - Search for
Python - 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
- 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:\cs1602or~/cs1602. - In VS Code, File → Open Folder, and open it.
- Create a file called
hello.py. - Put this in it:
print("Hello world")
- 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.
- Two kinds of quote. Run
print("SJTU")and thenprint('SJTU'), and compare. - Python’s own creed. Run
import thisand read it. These nineteen lines by Tim Peters are the community’s statement of taste, and this course keeps coming back to them. - A very large number. Evaluate
2 ** 100. - Two kinds of division. Evaluate
10 / 3and10 // 3. - 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:
| Entry | What you hand in | How it is marked |
|---|---|---|
| Code problem | one function | the platform runs the tests and scores it immediately |
| Answer sheet | multiple choice and written answers | the 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:
- Misspell
printaspirnt - Use full-width parentheses:
print("hello") - 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:
- How far apart are the two values at round 10?
- How far apart at round 25?
- 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:
- Do the two versions give the same value at round 25?
- From which round does a difference appear?
- 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
| Command | What it does | Mnemonic |
|---|---|---|
pwd | which directory am I in | print working directory |
ls | list what is in this directory | list |
cd name | move into a directory | change 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):
pwd— see where you arecdto the folder holding your course codels— check thathello.pyandchaos.pyare both therepython hello.py— run itcd ..to go up a level, thenpwdagain to confirm you moved
Checklist
Before you submit, go through this:
-
python --versionprints 3.13.x - the Python extension is installed in VS Code
- the Run button works on
hello.py -
python hello.pyworks 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
| Symptom | Usually means | Fix |
|---|---|---|
'python' is not recognized | PATH not set on Windows | Rerun the installer → Modify → tick Add to PATH |
zsh: command not found: python | different command name on macOS | Try python3, or check where it installed |
| Run button is greyed out | Python extension missing | Install the official Microsoft Python extension |
| VS Code cannot find an interpreter | none selected | Ctrl+Shift+P → Python: Select Interpreter → pick 3.13 |
can't open file at runtime | terminal is in the wrong directory | pwd 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.