COMPSCI4039 Chap.1 Values, Variables and Types
Values, Variables and Types
Values, Variables and Types sets up the mental model every later chapter depends on. A program is a precise sequence of instructions, and the machine carries them out literally, even when they make no sense. The opening lecture frames this as modelling: a task from some application area is represented with the building blocks of a language. Those blocks start small.
print writes text to the screen, input collects what the user types and hands it back as a string, and assignment attaches a name to a value. Python is dynamically typed, so the type travels with the value rather than with the name, and reassigning a name can change its type. Four types appear immediately: str for text, int for whole numbers, float for numbers with a decimal point and bool for True and False.
Literals are those values written directly into code. The chapter also separates two families of fault. Syntax or context errors, such as an unclosed quote or a misspelt function name, stop the program, while logic errors run happily and produce the wrong answer, which only testing can reveal.
What this chapter covers
- 01
The ABC view of programming as modelling
- 02
Output with print, input and the input-process-output style
- 03
Variables as name and value associations
- 04
Dynamic typing and reassignment
- 05
Literals of type str, int, float and bool
- 06
Naming rules and comments
- 07
Syntax errors, name errors and logic errors
Worked example · free
Trace three assignments and one print
- 1qty is bound to 8, the value price holds at that moment. The marks shown are an original practice weighting, not an official university scheme.
- 1Rebinding price to 2.5 leaves qty attached to 8.
- 1qty * price multiplies an int by a float, giving 20.0.
- 1print separates its two arguments with one space: Total 20.0.
Key terms
- Dynamic typing
- A language rule in which types belong to values, so one name may refer to different types over time.
- Logic error
- A fault that lets a program run but makes it produce the wrong result.
- Comment
- Text after a hash symbol that the machine ignores and the reader uses as a note.
Values, Variables and Types FAQ
Why does Python not ask for a variable type?
Python is dynamically typed: each value carries its own type, and a name simply points at whichever value was assigned last. That makes short programs quick to write but means a careless reassignment can silently replace a number with text.
What is the difference between a syntax error and a logic error?
A syntax or context error, such as a missing closing quote, stops Python from running the faulty line at all. A logic error runs without complaint and produces output that is simply wrong, so it is found only by testing against expected results.
Does print add spaces between arguments?
Yes. When print receives several arguments separated by commas, it displays each one with a single space between them and finishes with a newline. Joining strings with the plus operator adds no space, so you must type it yourself.
How should I name my variables?
Use names made of letters, digits and underscores that never begin with a digit, and remember that case matters, so total and Total are different. The house style is lower-case words joined by underscores, chosen to say what the value means. Names such as a and b work, but they make code harder for anyone else, including an examiner, to follow.
Exam move
Build the habit this chapter is really about: reading assignments precisely. For every fragment you meet, draw a state table with one column per variable and update it after each line, crossing out replaced values rather than erasing them. Say the type aloud as you write each value, because 4, 4.0 and "4" behave differently later.
Rehearse the error families with deliberate mistakes in the interpreter: remove a closing quote, misspell print, change the case of a variable name, and read the last line of each message to see which error type Python names and where. Then write tiny programs that use input and print together, such as a greeting that echoes a typed name, and predict the exact output, including spaces, before running them.
Finish by explaining in a sentence why b keeps its old value after a = b followed by a change to a; if you can explain it plainly, the idea is secure.
Working through Values, Variables and Types in COMPSCI4039? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI4039 Values, Variables and Types question and get a clear, step-by-step explanation grounded in how COMPSCI4039 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.