Python basics: your first line of code

Advanced

Python is a typed language whose lines run top to bottom. print() displays things; a variable is a named box that stores data.

For:Secondary 1 Information & Communication Technology · Unit:Coding basics · Lesson 9 of 13 in the learning path

After this lesson, you can:
  • Python runs top to bottom, line by line
  • print() displays; quote text, not numbers
  • A variable is a named box: x = 5 means 'put in', not 'equals'
資訊及通訊科技Information & Communication Technology
Before this lesson you should know: Scratch basics: your first project

§Text Passage

1Python is one of the most popular text-based programming languages in the world: its syntax reads almost like English, it is concise and readable, and its uses range from artificial intelligence and data analysis to web development. The traditional first line is print("Hello"), which commands the computer to print text on the screen — a learner's first conversation with the machine.

2In practice, after installing Python or opening an online editor, start with print exercises, then learn how variables store data and how input() reads what the user types — do not copy long examples without understanding them. Save the program with the .py extension and run it from the command line to see the result; when an error message appears, reading the line number and the hint carefully is the first lesson in debugging.

3Hong Kong example: banks in Central use Python to analyse market data, weather apps rely on it to process huge volumes of figures, and local start-ups and research teams adopt it just as widely. Building a solid foundation in junior secondary prepares you for senior-secondary ICT electives, university studies and a future career in information technology, giving you a clearer direction when choosing subjects.

1Summary 重點摘要

Python is a typed language whose lines run top to bottom. print() displays things; a variable is a named box that stores data.

2Vocabulary cards Vocabulary

P
PythonPython 語言

A typed programming language close to English, common in junior ICT.

p
print()顯示指令

Displays what is inside the brackets, e.g. print('Hello').

V
Variable變數

A named box that stores data, fetched later by its name.

S
String字串

Text data wrapped in quotes, e.g. 'Hello'.

I
Integer整數

A whole number like 7 or -3; written plainly, no quotes.

i
input()輸入指令

Waits for the user to type and returns it — always as a string.

S
Syntax語法

The writing rules of a programming language; break them and the computer reports an error and refuses to run.

F
File extension (.py)副檔名(.py)

The ending of a filename showing its type; Python programs end with .py.

3Step by step Steps

  1. How a program runsPython runs a program from the first line down to the last, one line at a time, never skipping forward or back. This is exactly how Scratch blocks run from top to bottom — a sequence structure. The idea matters because if line three changes a variable, line four reads the new value, not the old; the result depends on the order of the lines, and swapping two lines can change everything. When reading someone else's program, trace it line by line with a finger, imitating the computer — the most effective way for a beginner to understand what happens.
  2. The first line of codeBy tradition the first program in any language prints a greeting, and in Python it is print('Hello, world!'). The word print is the command to show output on the screen; what goes in the brackets is what will be shown. Rule one: text (a string) must be wrapped in quotes, single or double — print('Hello') and print("Hello") behave identically. Rule two: numbers need no quotes; print(7) shows 7. Without quotes, print(Hello) raises an error, because the computer takes Hello as a variable name. Everything inside the quotes displays exactly as typed, spaces included; outside the quotes is where code lives.
  3. A variable is a labelled boxA variable is like a labelled box storing a piece of data. Writing age = 12 means: put the value 12 into the box named age; later print(age) opens the box and shows 12. The contents can change at any time: after age = 12, writing age = 13 leaves the box holding 13, with the old 12 overwritten. The crucial idea: in programs, the equals sign means 'put the right-hand value into the left-hand box', not mathematical equality. That is why x = x + 1 is absurd in mathematics yet perfectly normal in code — 'take x's value, add 1, and store it back in x'.
  4. Getting input from the userThe input() command lets the program talk to the user. Writing name = input('What is your name?') shows the question in the brackets, and whatever the user types is stored in the box name; type Ming and print(name) shows Ming. One essential caveat: input() always returns a string, even when the user types digits. After age = input('Age?'), the box age holds the text '12', which cannot be used in arithmetic; convert it with int(): age = int(input('Age?')), after which age + 1 computes correctly. Forgetting the conversion raises an error — one of the commonest beginner mistakes.
  5. Putting it togetherThree instructions combine into one complete mini-program:

    name = input('What is your name?')
    age = int(input('How old are you?'))
    print('Next year you will be', age + 1)

    The order of running: line one asks the name and stores it; line two asks the age and converts the text to a whole number; line three joins text with the computed result, so entering 12 displays 'Next year you will be 13'. This tiny program shows the skeleton of every program: input first, then processing (computing age + 1), then output (print) — matching the hardware jobs of input, processing, output and storage one to one.
  6. Running and commentingA finished program must be run to show results. Method one: save the file as hello.py (the extension must be .py), open a command line and type python hello.py. Method two: press the Run button in an online editor — beginners should start there, with no installation needed. A line beginning with # is a comment, such as '# work out next year's age'. Comments are notes for humans that the computer skips entirely; they label what each part of the code does so that you or a classmate can read it later. A good habit is to comment as you write, especially on the harder calculation lines, so the code stays clear three months on.
  7. Common errors and debuggingThree errors dominate a beginner's first week. First, unbalanced quotes or brackets: print('Hello) with no closing quote shows SyntaxError; the fix is to type the closing partner the moment you open one. Second, mixing strings and numbers: after age = input(), writing age + 1 raises TypeError because text cannot be added to a number; write age = int(input()) instead. Third, mistyped names: having defined name, printing nam raises NameError; Python names are case-sensitive, and Name and name are two different things. When an error appears, do not panic: read the error type first, then the line number, and the cause is usually obvious at once.

4Diagrams Diagrams

Python 第一行:print() Python basicsprint("你好,世界!")螢幕輸出:你好,世界!print()顯示括號入面嘅內容引號 ""包住文字(字串)括號 ()話俾 print 執行咩任務Python 係文字程式語言:寫錯一個引號都會 error——細心係基本功try it:改入面嘅字,印自己個名——第一個作品就係咁簡單
Fig. 1: Python basics

5Worked example Example

Example: what does this output? Line 1: x = 5; line 2: x = x + 3; line 3: print(x)

  1. Line 1: box x holds 5
  2. Line 2: take 5, add 3 to get 8, put it back in x (the old 5 is overwritten)
  3. Line 3: display x's current value
  4. Key point: = means 'put in' — the right side is worked out first, then stored in the left box
  5. Answer: It prints 8. Variables can overwrite old values — the last one in wins.

6Common mistakes Mistakes

✗ Writing print(Hello) without quotes.
✓ Text needs quotes: print('Hello'); without them Python treats Hello as a variable name and raises a NameError.
✗ Reading x = 5 as 'x equals 5'.
✓ In Python, = means 'put the right side into the left box'; testing equality uses == (more next lesson).
✗ Adding numbers straight from input().
✓ input returns a string: '5' + 3 is an error; convert with int() before calculating.
✗ Typing full-width Chinese brackets or quotes, e.g. print('Hello').
✓ Code must use half-width English symbols: print('Hello'); full-width punctuation raises a SyntaxError.

7Quick check Quick check

Q1What is the difference between print('5') and print(5)?

Q2Write one Python line storing 10 in a variable called score.

Q3What type does input() return, and what must you do to calculate with it?

Q4Write one Python line reading the user's age and storing it as an integer in age.

Q5What is a line starting with #? Does the computer run it?

8Recap Recap

  • Python runs top to bottom, line by line
  • print() displays; quote text, not numbers
  • A variable is a named box: x = 5 means 'put in', not 'equals'
  • input() gives strings; convert with int() before maths
  • Input → processing → output: programs follow the same idea as hardware

Other subjects in this grade

Same subject, other grades