Lab: M1: write your first Python program
You'll need: your own laptop, a web browser, and a free Google account. Nothing to install. Time: ~30 minutes • Work in your breakout pair: compare your screens at each step.
Heads up: you'll almost certainly see a red error message at some point. That's not you failing, every programmer sees them constantly. They're safe, and they tell you exactly what to fix. Note it, read the last line, and keep going.
Two words for today: - Program: a list of instructions a computer follows, in order, top to bottom. - Python: the language we write those instructions in. It reads almost like English.
Where we'll run it: Google Colab: a free website that runs Python for you, so there's nothing to set up today. (We install Python on your own machine later, in M3, once you need it.)
flowchart LR
Y["You type Python<br/>in a Colab cell"] --> R["Press ▶ Run"]
R --> C["Google's computer<br/>runs your code"]
C --> O["The answer appears<br/>under the cell"]
Step 1: Open a Colab notebook
In your browser go to https://colab.research.google.com and sign in with your Google account. Click File → New notebook in Drive.
You should now see: a page with an empty grey box (a cell) and a ▶ play button to its left. That cell is where your code goes.
Step 2: Run your first line of code
Click inside the cell and type exactly:
print("Hello, World!")
You should now see: the words Hello, World! appear just below the cell. You ran a
program. (print means "show this on the screen." The text in quotes is what gets shown.)
Step 3: Add a comment (a note to humans)
Make a new cell with the + Code button at the top. Type:
# This line is a comment: Python ignores it. It's a note for people.
print("Comments are for humans; code is for the computer.")
You should now see: only the sentence Comments are for humans; code is for the computer.
The # line produced nothing: Python skipped it. Comments let you leave notes in your code.
Step 4: Store something in a variable
New cell. Type and run:
name = "Ada"
print(name)
You should now see: Ada. The line name = "Ada" put the text "Ada" into a labelled box
called a variable named name. Whenever you write name, Python swaps in Ada. Change
"Ada" to your own name and run it again: the output changes.
Step 5: Meet the three basic types
New cell. Type and run:
name = "Ada" # text, called a string
age = 36 # a whole number, called an integer
is_learning = True # a true/false value, called a boolean
print(name, age, is_learning)
You should now see: Ada 36 True. Those are the three building-block types: text
(in quotes), numbers (no quotes), and true/false. The type tells Python what it's allowed
to do with the value, you can do maths on a number, but not on text.
Step 6: Do a little maths
New cell. Type and run:
bill = 42.50
tip = bill * 18 / 100 # 18% of the bill
print(tip)
You should now see: 7.65. Python did the maths. * means multiply and / means divide.
Notice bill had no quotes, it's a number, so maths works on it.
Step 7: Ask the user a question with input()
New cell. Type and run:
your_name = input("What's your name? ")
print("Nice to meet you, " + your_name)
You should now see: Nice to meet you, <your name>. input() shows your question, waits
for the person to type, and hands back whatever they typed (always as text). The + glued two
pieces of text together.
Step 8: Combine it into a sentence with an f-string
New cell. Type and run:
your_name = input("What's your name? ")
print(f"Hello {your_name}, welcome to Python!")
You should now see: Hello <your name>, welcome to Python! The f before the quotes makes
an f-string: anything inside { } gets replaced by that variable's value. It's the clean way
to drop variables into a sentence.
Step 9: Build your tip helper
Now put it together. In a new cell, type this (read each line, you've met all of it):
name = input("What's your name? ")
bill = float(input("How much was the bill? ")) # float() turns the typed text into a number
tip = bill * 18 / 100
total = bill + tip
print(f"Thanks, {name}! Your tip is ${tip:.2f} and your total is ${total:.2f}.")
42.50.
You should now see: something like Thanks, Sam! Your tip is $7.65 and your total is
$50.15. You just wrote a program that takes input, does maths, and answers a person by name.
(float(...) turns typed text into a decimal number; :.2f rounds to 2 places, like money.)
Stuck on Step 9? A finished, fully-commented version is in
../solution/greeter.py, and a fill-in-the-blanks starter is in../starters/. Peek only after you've tried.
Your win
You wrote and ran your own Python program, it asks questions, does maths, and replies by name, and you can read every line.
Post it to the chat wins board: paste your program's last line of output, e.g. "Thanks, Sam! Your tip is \$7.65 and your total is \$50.15., I wrote this on day one "
Take-home (optional)
Change your program to ask for the tip percentage too (add one more input() and use that
variable instead of 18). Run it with a few different tips. Bring one result to next session.