Values and variables
In algebra, score = score + 5 has no solution. In Python it is an everyday line: if score was 10, it becomes 15.
The = sign does not mean "is equal to"
Show a maths student the line score = score + 5 and they will tell you it is false: no number equals itself plus five. They are right about algebra, and that is exactly why the line confuses people the first time they see it in a program. In Python, = is not a statement that two things are equal. It is an instruction with a direction. Work out the right-hand side first, then attach the name on the left to the result.
score = 10
score = score + 5
print(score)15Read it the way Python does. Line 1: the right side is 10, so the name score now refers to 10. Line 2: the right side is score + 5, which is 10 + 5, which is 15, so score now refers to 15. The old 10 is gone. Line 3 prints what score refers to at that moment. A name that refers to a value like this is called a variable, and the line that gives it a value is an assignment.
A trace table follows each variable line by line
When several variables change, keep a trace table: one row per line of code, one column per variable, and write the value each variable has after that line runs. It is slow, and it works. Most "why does it print that?" questions are settled by a trace table in under a minute.
score = 10
bonus = score * 2
score = 0
print(score, bonus)0 20| Line | score | bonus |
|---|---|---|
| score = 10 | 10 | (none yet) |
| bonus = score * 2 | 10 | 20 |
| score = 0 | 0 | 20 |
| print(score, bonus) | 0 | 20 |
Look at the last two rows. Changing score to 0 did not change bonus. When line 2 ran, Python worked out 10 * 2 = 20 and gave bonus that number. bonus holds the result, not the recipe, so it has no memory of where the 20 came from.
Every value has a type
Values come in kinds, called types. Whole numbers such as 42 are int. Numbers with a decimal point, such as 3.5, are float. Text in quotes is str. True and False are bool. The built-in function type tells you which one you have. The type matters because it decides what an operation does: 2 + 3 adds, while "2" + "3" joins two strings into "23".
print(type(42))
print(type(3.5))
print(type("42"))
print(type(True))
print(2 + 3, "2" + "3")<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
5 23Rules for names
A variable name can use letters, digits and the underscore _, but it cannot start with a digit and cannot contain spaces. Capital letters count as different letters, so Score and score are two separate variables. Some words, such as if, for and True, belong to the language and cannot be used as names. Python programmers usually write names in lower case with underscores between words, like lunch_price, and pick names that say what the value means. x is fine for a quick test, and seats_left is better in a program someone else will read.
a = "left"
b = "right"
a, b = b, a
print(a, b)right leftThe last sample swaps two values in one line. Python works out the whole right side, b, a, first, and only then gives the two results to a and b. Many other languages need a third variable to do the same job.
| variable | A name that refers to a value |
| assignment | name = value: work out the right side, then attach the name |
| int | Whole numbers: 7, -3, 2026 |
| float | Numbers with a decimal point: 3.5, 0.1 |
| str | Text in quotes |
| bool | True or False |
| trace table | A table of every variable's value after each line |
- What does this print?
x = 4 x = x + 3 print(x) - What does this print?
a = 3 b = a a = 7 print(b) - What does this print?
price = 1500 count = 3 total = price * count print(total) - What does this print?
n = 2 n = n * n n = n * n print(n)48162
- What does this print?
print(type(7.0)) - What does this print?
print("7" + "7") - Which of these is a valid variable name?
- 2nd_place
- seats left
- seats_left
- for
- What does this print?
a = "left" b = "right" a, b = b, a print(b) - What does this print?
Score = 5 score = 9 print(Score)5914An error
- After x = 10 and then x + 1 on its own line, what is x?
- 10
- 11
- x + 1
- An error
- "5 = x and x = 5 mean the same thing." The name must be on the left. 5 = x stops with SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
- "bonus = score * 2 links bonus to score for good." bonus gets the number that the calculation gave at that moment. Later changes to score do not touch it, as the trace table shows.
- "Writing x + 1 adds one to x." An expression on its own changes nothing. You need x = x + 1, or the short form x += 1.
- = means "work out the right side, then give it this name".
- A trace table records every variable after every line.
- Types: int, float, str, bool. type() tells you which.
- Names: letters, digits, _; no digit first; case matters.
Sources for this course (6)
- Python Software Foundation, The Python Tutorial for Python 3.12 (docs.python.org/3.12/tutorial: 2 Using the Python Interpreter, 3 An Informal Introduction to Python (numbers, text, lists), 4 More Control Flow Tools (if, for, range, break and continue, defining functions, default argument values, 4.10 coding style: 4-space indentation), 5 Data Structures (list methods, 5.5 dictionaries, insertion order), 8 Errors and Exceptions (syntax errors, exceptions, reading the traceback, handling exceptions), 15 Floating-Point Arithmetic (0.1 + 0.2))
- Built-in Functions (docs.python.org/3.12/library/functions.html: print, input, len, int, float, str, type, round (round half to even: round(0.5) and round(-0.5) are 0, round(1.5) is 2), range, sum, min, max, sorted)
- Built-in Types (str methods upper, lower, replace, strip; dict.get; list.append, pop, remove, sort returning None)
- General Python FAQ (docs.python.org/3.12/faq/general.html: Guido van Rossum began Python in the 1989 Christmas holidays, posted it to USENET in February 1991, named it after Monty Python's Flying Circus)
- Curriculum for the Korean pages: 교육부 고시 제2022-33호 [별책 10] 정보과 교육과정 (PDF read 2026-10-19: 중학교 정보 [9정03-05] 리스트 등 데이터를 순차적으로 저장하는 구조, [9정03-06] 논리 연산과 중첩 제어 구조, [9정03-07] 함수 활용과 디버거로 오류 수정, [9정03-08] 실생활 문제를 프로그래밍으로 해결, 적용 시 고려 사항 「학생의 수준을 고려하여 적합한 프로그래밍 언어를 선정」; 고등학교 정보 [12정03-04] 자료형, [12정03-05] 표준입출력, [12정03-07] 제어 구조)
- Bug story only: Smithsonian National Museum of American History, Log Book With Computer Bug, 1947, Harvard Mark II, "first actual case of bug being found", probably not Hopper's log book (americanhistory.si.edu/collections/object/nmah_334663, read 2026-10-19)
Checked: 2026-10-19