Talk to the machine
Worked examples
Read the claim, then run it and check the machine agrees. One at a time — nothing here is taken on trust.
print("hello, world")
print(1, 2) # several things -> joined with one space
answer = print("side effect") # print shows text, then hands back None
print("print returned:", answer)
Line 1 shows hello, world. Line 2 shows 1 2 — the two numbers joined
by a single space, which is print's default. The last two lines are the
subtle part: print("side effect") displays its text, but the value it
returns is None, so answer becomes None. Text-on-screen and
value-returned are two different things.
print(2 + 3) print(10 / 3) # single slash: an exact-ish decimal (a float) print(10 // 3) # double slash: whole part only, remainder dropped
2 + 3 is 5. 10 / 3 prints 3.3333333333333335 — a / always gives
a decimal number, and those trailing digits are real (a later lesson
explains why the last one is a 5 and not more 3s). 10 // 3 prints
3: the // asks "how many whole 3s fit into 10?" and ignores the leftover.
shares = 8 price = 125 total_cost = shares * price # an expression's value, saved to a name ticker = "AAPL" banner = ticker * 3 # repeat the string note = "buy " + ticker # glue two strings print(total_cost) print(banner) print(note)
Each name = ... line is a statement: it runs the expression on the right,
then makes the name mean that value. total_cost becomes 1000, banner
becomes 'AAPLAAPLAAPL', and note becomes 'buy AAPL'. Notice *
multiplied a number in one place and repeated a string in another — same
symbol, different job. The three prints just show what we built.
Check the concept
One question at a time. Unsure? Revisit the lecture, then answer.
The challenge
Pass the quiz to unlock the challenge — your code will still be waiting here.