DO NOT COPY OR SHARE - A3Tech / NCTI course material for enrolled students only
A3Tech logo

Introduction to Python Programming · A3Tech

Module 1

Today you will

Set up your coding environment, run your first programs, and learn to read error messages.

  1. Set up the course environment by typing the commands yourself (House Cleaning).
  2. Run a Python file from the terminal and from Thonny.
  3. Use print(), text in quotes, comments, and maths inside print.
  4. Read the last line of an error and fix SyntaxError, IndentationError and NameError.

House Cleaning: set up your environment

Every programmer sets up their workspace before they code. Type each command yourself in the terminal, inside your course folder. Stuck for more than 5 minutes? Double-click SETUP-Windows.bat in the Starter Kit and ask for help.

Windows (PowerShell)
Type thisWhat it does
py --versionChecks Python is installed. You want 3.12 or newer.
py -m venv $HOME\python-course-envBuilds your private Python environment. WHY? A mini sandbox: it contains our code and protects global settings and other apps/code
& $HOME\python-course-env\Scripts\Activate.ps1Switches it on. The prompt starts with (python-course-env). activate the Virtual Environment
python -m pip install --upgrade pipUpdates pip. pip is like the Playstore for Python: it downloads and installs packages (extra tools) for us. pip stands for "Pip Installs Packages".
pip install streamlitDownloads Streamlit. Streamlit will be our shortcut for our apps to run in the browser, its free and cheaper for now. later courses that is focused on app making will introduce other options, such as pyqt, TKinter, React etc
python setup_check.pyEvery line should say OK. - test that streamlit and python is installed. we can just do the python --version or the streamlit --version instead to show the version confirming the installed status of the packages.
mkdir $HOME\.streamlit -ForceMakes a folder for Streamlit's settings.
notepad $HOME\.streamlit\config.tomlOpens a new settings file. Type the settings below and save. you can make this manually inside the folder.
streamlit run app.pyStarts the test app. Click the button: YAY, it works!

If Activate.ps1 says running scripts is disabled, skip activating and run: & $HOME\python-course-env\Scripts\python.exe -m streamlit run app.py --server.address 127.0.0.1

Mac and Ubuntu Linux (Terminal)
Type thisWhat it does
python3 --versionChecks Python is installed.
python3 -m venv ~/python-course-envBuilds your private Python environment. (Ubuntu first: sudo apt install python3-venv)
source ~/python-course-env/bin/activateSwitches it on.
pip install --upgrade pipUpdates pip.
pip install streamlitDownloads Streamlit.
python setup_check.pyEvery line should say OK.
mkdir -p ~/.streamlitMakes a folder for Streamlit's settings.
nano ~/.streamlit/config.tomlOpens a new settings file. Type the settings, then Ctrl+O, Enter, Ctrl+X.
streamlit run app.pyStarts the test app. Click the button: YAY, it works!

The settings file

config.toml
[server]
address = "127.0.0.1"

[browser]
gatherUsageStats = false

address = "127.0.0.1": only your computer can open your app, at http://127.0.0.1:8501. gatherUsageStats = false: Streamlit doesn't send usage information.

You're done when the test app shows YAY, it works!

1. Your first program

Open 01_hello.py in Thonny and run it with the green Run button (or press F5). You can also run it from the terminal with python 01_hello.py.

01_hello.py
# My first Python program
# Lines that start with # are comments: notes for people. Python ignores them.

print("Hello, world!")
print("I am learning Python")
example.py
# give it a variable (a name that stores a value - more on variables in Module 2)
my_math = 3 + 5
print(my_math)

#print the results directly
print(3 + 9)

# printing a prefix to the calculation
print("4 + 2 =", 4 + 2)
Output
8
12
4 + 2 = 6

2. The shop sign

The start of the shop program we'll build all course.

02_shop_sign.py
# shop.py - Module 1: the shop's welcome sign
# We'll keep adding to this program every module.

print("=" * 32)                      # "=" repeated 32 times makes a border
print("    Welcome to Mama J's Bakery")
print("    Fresh patties every morning")
print("=" * 32)
print("Beef Patty        EC$5.00")
print("Cheese Patty      EC$5.00")
print("Coconut Tart      EC$4.00")
print("-" * 32)
print("2 beef patties cost EC$", 2 * 5)   # Python can do maths inside print
print("Open 7am - 6pm, Kingstown")
print("=" * 32)

Words to know

WordMeaning
print()Shows something on the screen
StringText inside quotes: "Hello"
CommentA note starting with #. Python ignores it. WHY we use comments? For other people to understand your code faster, for you to remember what you wanted the code to do especially if you run into errors later on. 
ErrorPython's message that something is wrong. Read the last line first
Case-sensitiveprint and Print are different to Python
Virtual environmentA private Python folder for one project

Bug hunt

This program has 4 mistakes. Run it, read the last line of the error, fix that one mistake, and run it again.

03_bug_hunt.py
# BUG HUNT - this program has 4 mistakes.
# 1. Run it.  2. Read the LAST line of the error.  3. Fix that one mistake.
# 4. Run it again. Repeat until the sign prints.

print("Welcome to the shop)
print("Today's special: Coconut Tart")
print("Patties are EC$5.00"
Print("Thank you for visiting")
    print("Come again!")
Hints (open only if stuck)
  1. Every piece of text needs a quote at the start and the end.
  2. Brackets come in pairs.
  3. Lines don't start with spaces (yet).
  4. Python is case-sensitive.

Practice

Homework 1 (5%): my_sign.py

A sign for your own business, a family business, or one you'd like to start. It must include:

Submit your .py file and a screenshot of it running, before the next module.

MarkMeans
5Runs and has everything
3Runs with parts missing, or has errors
1Started
0Not submitted

Quiz 1

Check yourself here, then take the graded quiz in the quiz section of robotics.a3tech.uk (two tries).

1. What does this print?
print("EC$", 2 * 5)
2. What does this print?
print("=" * 3)
3. Why do we use a virtual environment for the course?
4. The last line of an error says: SyntaxError: unterminated string literal. What is the problem?
5. This line gives NameError: name 'Print' is not defined. Why?
Print("Thank you")

ADDITIONAL - STREAMLIT APP - SNEAK PEEK

shop_app.py
# shop_app.py - the shop's welcome sign, as a Streamlit app
# Run it with:  streamlit run shop_app.py

import streamlit as st          # bring in Streamlit and call it "st"

st.title("Welcome to Mama J's Bakery")       # big heading at the top
st.subheader("Fresh patties every morning")  # smaller heading
st.divider()                                 # a line across the page (our border)

st.write("Beef Patty: EC$5.00")
st.write("Cheese Patty: EC$5.00")
st.write("Coconut Tart: EC$4.00")
st.divider()

st.write(f"2 beef patties cost EC${2 * 5}.00")   # the f and { } let you put maths inside text

st.caption("Open 7am - 6pm, Kingstown")      # small grey text at the bottom
Output
See the browser for the Streamlit app