Ungraded. You leave today having typed two short programs, predicted what they print, and been wrong at least once on purpose.
The next session is in two weeks (no class on 21 September). Today we make sure everything works, and we do by hand what the lecture did on the screen.
If you get stuck, raise your hand and say which part you are on.
✅ Part 1 done when you see the editor with the file list on the left.
A file is text with a name, saved on the computer. A folder is a list of files. A program is a file.
dsap — small letters, no spaces. Press
Enter.dsap so it is selected, then click the
first icon, New File. Name it hello.py.
Press Enter. The ending .py tells the computer: this
file is a Python program.print("hello")hello
You have written and run a program.
hello with your first name, keeping the
quotation marks. Click ▷ again. Your name appears. Change it once more,
run once more: what runs is always what is in the file right now.✅ Part 2 done when hello.py prints
your name.
In the lecture you voted on what this program prints. Now type it. Do not copy and paste: typing is the point of this course, and the lecture told you why.
dsap: ledger.py. Type these
ten lines (the blank lines too — they do nothing, they are for your
eyes):ticker = "AAPL"
shares = 10
price = 190.50
value = shares * price
print("value at", price, "is", value)
price = 200.00
print("price is now", price)
print("value is still", value)value at 190.5 is 1905.0
price is now 200.0
value is still 1905.0
If you wrote 2000.0: you read the program as Excel.
value = shares * price does not mean “value is always
shares times price”. It means “work it out now, and call the
answer value”. Then it is done. The price changed
afterwards; value did not look back.
price = 200.00 and paste it
above value = shares * price. Delete the
two print lines that no longer make sense, so the file
reads:ticker = "AAPL"
shares = 10
price = 190.50
price = 200.00
value = shares * price
print("value at", price, "is", value)Run:
value at 200.0 is 2000.0
Two versions. Both run without any error. Only one of them is your portfolio. Nothing on the computer checks which — that is what the lecture called runs and lies, and it is what you are here to learn to see.
shares = shares + 5,
then print(shares). Predict, run. Read the first one from
right to left: take what shares is, add five, put it
back under the name shares. It is an instruction, not
an equation.✅ Part 3 done when you have seen
1905.0 and 2000.0 from the same ten lines, and
predicted the last one correctly.
Look at what you have typed so far. Every line that does something has the same shape:
print("value at", price, "is", value)A name, then parentheses, and
inside them what you hand over, separated by commas. That is a
call. print is not a magic word; it is a
piece of code somebody else wrote, and the parentheses are how you run
it with your own values. Everything in Python that does work looks like
this. Libraries, which the lecture called the reason Python won, are
shelves of things to call.
There are two kinds. print does
something — text appears. Others give something back,
and that is where today’s problem hides.
dsap: portfolio.py. Type:aapl = 10 * 190.50
nesn = 4 * 102.30
voo = 2 * 512.00
total = aapl + nesn + voo
print("total:", total)
share = nesn / total * 100
print("NESN share, in percent:", share)Run:
total: 3338.2
NESN share, in percent: 12.258103169372717
That last number is correct and unreadable. Python has
round for this: hand it a number and how many decimals you
want, and it gives back the rounded number.
round(share, 2)
print("rounded:", share)Predict. Run:
total: 3338.2
NESN share, in percent: 12.258103169372717
rounded: 12.258103169372717
Nothing changed. No error, either.
round did its job and handed back 12.26 — and
nobody took it. A value that comes back from a call is not kept anywhere
unless you give it a name. The line did work and threw the result
away.
share = round(share, 2)
print("rounded:", share)rounded: 12.26
Same = as before: call round, take what
comes back, put it under the name share. This is the
second thing today that ran perfectly and lied. Code written by a
chatbot does this one all the time.
✅ Part 4 done when rounded: 12.26
appears, and you can say why the earlier version printed the long
number.
The lecture said code fails in three ways. You have already met the worst one — it runs and lies — twice. Now the other two, on purpose, because you will see them every week and they are the harmless ones.
hello.py, delete
the second quotation mark. Run. Red text. Do not read it all; read only
the last line:SyntaxError: unterminated string literal (detected at line 1)
Python could not understand line 1, so it did not run anything at all. Put the quotation mark back.
portfolio.py, change
the last line to print("rounded:", shar) — one letter
missing. Run:total: 3338.2
NESN share, in percent: 12.258103169372717
Traceback (most recent call last):
File "/files/dsap/portfolio.py", line 12, in <module>
print("rounded:", shar)
^^^^
NameError: name 'shar' is not defined. Did you mean: 'share'?
Two lines printed before it stopped: the program ran top to bottom until line 12, then hit a name it had never seen. The last line says what went wrong and even guesses the fix. Fix it.
Every error you ever see will look like this: some lines, then a last line that says what is wrong. Read the last line first. Always.
✅ Part 5 done when you have seen both messages and both files run again.
The lecture said a file is bytes plus a name, and the name lives in a tree. Look at yours.
hello.py and hold it
there. A small label appears: /files/dsap/hello.py. That is
the absolute path: start at the root /, go
into files, then dsap, and there is the file.
Every file in this workspace has one, and it means the same thing from
anywhere.hello.py, press F2, and
rename it to hello.txt. Press Enter. Look: the colours in
the text are gone, and the ▷ button is gone. Nothing inside the file
changed — every byte is the same. The ending only tells the editor what
to expect. This is the lecture’s “Windows hides extensions” trap, seen
from the other side.hello.py. Colours and ▷ return.✅ Part 6 done when you can say the full path of
hello.py, and hello.py runs again.
The lecture showed them on the screen. Now your computer says them.
New file in dsap: float.py. Type these
three lines and run:
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(round(0.5), round(1.5), round(2.5), round(3.5))0.30000000000000004
False
0 2 2 4
Line 1: 0.1 cannot be written exactly in binary, so the
computer stores a very close neighbour and adds those. Line 2: which is
why you never compare money with ==. Line 3:
round breaks ties toward the even number — the banking
standard. Not a bug, not a beginner problem; this happens to everyone,
forever.
One more line, then run again:
print(0.1 + 0.2 + 0.3, 0.3 + 0.2 + 0.1)0.6000000000000001 0.6
Same numbers, different order, different answer. Session 2 shows what to do about it.
✅ Part 7 done when all four lines have printed and nothing surprised you the second time.
Come back once, on any day, for ten minutes: open Nuvolos, open
VSCode, open portfolio.py, change a number, run. If it
works, you are ready for session 2. If it does not, write down what the
screen said and bring it.
portfolio.py, add a fourth position —
msft = 3 * 410.00 — and put it into total.
What happens to the NESN share? Predict first.me.py: print three lines — your name, your
programme, and one number you calculated, for example the days until the
end of the semester.