Learn
01.3 | VARIABLES AND DATA TYPES
01 | A NAME MAKES A VALUE REUSABLE
A welcome message becomes more flexible when the name is stored separately from the sentence. A variable is a name associated with a value. Assign a value using =, then use the name when you need that value.
The first line associates username with the string Maya. The second uses the current value of username. The third prints the word username because it is inside quotes.
A box is a useful first mental picture, but in Python names refer to objects holding values. For these simple values, concentrate on which value each name currently refers to.
username = "Maya"
print("Welcome,", username)
print("username")EXPECTED OUTPUT
Welcome, Maya username
02 | ASSIGNMENT IS AN ACTION
= assigns the evaluated right-hand value to the left-hand name. It is not a claim that two quantities remain mathematically equal forever. Python later uses == for an equality comparison; that belongs to the next section.
You can assign another value to the same name. The earlier value is no longer what that name refers to. Python does not remember a formula as a live spreadsheet link unless your code recalculates it.
score = 10 score = 15 print(score)
EXPECTED OUTPUT
15
03 | TRACE A CHANGE WITHOUT GUESSING
For score = score + 5, read the current score first, add five, then assign the result back to score. If score starts at ten, it becomes fifteen.
A numeric copy captures the value at that moment. Setting saved_score = score before the update gives saved_score the earlier ten. Changing score afterwards does not change saved_score.
When tracing, keep a small table of names and current values. Update it after each executed assignment instead of assuming every name follows every change.
score = 10 saved_score = score score = score + 5 print(score) print(saved_score)
EXPECTED OUTPUT
15 10
04 | CHOOSE NAMES A HUMAN CAN READ
Python identifiers can contain letters, digits and underscores, but cannot start with a digit. A name such as total_minutes describes its purpose better than t. A common style is lowercase words separated by underscores, called snake_case.
Names are case-sensitive: score and Score are different. Do not use a reserved keyword such as class as a variable name. Also avoid naming your values print, input, int or float: assigning those names can hide built-in functions you need later.
Python supports a wider range of identifier characters, but simple English letters, digits and underscores are a practical beginner convention. Consistency matters more than decorative names.
05 | STR STORES TEXT
The Python type str stores text. It can contain letters, digits, spaces and symbols, including an empty string. A code such as "0072" is text when its zeros and exact representation belong to an identifier.
"12" is a string even though it looks numeric. It is not the same value as the integer 12. The intended role of the data decides which representation is suitable.
Use quotes when writing a string literal, but use the unquoted variable name to retrieve its current value.
student_code = "0072" activity = "Robotics" print(student_code, activity)
EXPECTED OUTPUT
0072 Robotics
06 | INT AND FLOAT STORE NUMBERS
int represents whole numbers such as 0, 12 and -3. A count of complete items normally uses int. float represents floating-point numbers, useful for measurements and values with a fractional part, such as 2.5.
A float can have a whole-number mathematical value: 12.0 is still a float. The decimal point in a literal distinguishes it from the integer literal 12.
Floating-point representation can introduce tiny precision differences in calculations. It is suitable for these beginner measurement examples; exact financial arithmetic may need different tools later.
attempts = 3 distance = 2.5 print(attempts) print(distance)
EXPECTED OUTPUT
3 2.5
07 | BOOL REPRESENTS TWO STATES
bool has values True and False, with capital initial letters. Use it for a two-state condition such as whether a task is complete.
The string "False" is text, not the Boolean False. Quotes change the type and meaning. A three-state status such as pending, accepted and rejected cannot be represented directly by one Boolean value.
Later you will produce Booleans through comparisons and use them in decisions. For now, recognise their spelling and distinguish them from text.
finished = False message = "False" print(finished) print(message)
08 | ASK PYTHON FOR THE TYPE
type() returns the type of its argument. It helps explain why two similar-looking outputs behave differently. print displays a string’s characters without surrounding quotes, so visual output alone may not identify the type.
Python associates a type with the value, not a permanent declared type for the name. You do not write DECLARE before these assignments. A name can later refer to a value of another type, although unnecessary changes make a program harder to follow.
value = 12 print(type(value)) value = "12" print(type(value))
EXPECTED OUTPUT
<class 'int'> <class 'str'>
09 | PUT VALUES INTO A CLEAR MESSAGE
A comma-separated print call can display text and numeric values together. An f-string is another useful option: prefix a quoted string with f and place expressions inside braces.
Python evaluates the expression between each pair of braces when the f-string is created. It is not a permanent live connection to the variable. This is a concise way to make a report readable.
username = "Maya"
score = 15
print(f"{username} has {score} points.")EXPECTED OUTPUT
Maya has 15 points.
10 | STEP THROUGH THE ASSIGNMENTS
The fixed trace below uses the score example from earlier. Predict each name’s value before pressing Next step. Notice that saved_score keeps ten after score changes.
score = 10
saved_score = score
score = score + 5
print(score, saved_score)11 | CONSTANTS ARE A CONVENTION HERE
A value intended to stay fixed can be given an uppercase name such as MINUTES_PER_HOUR = 60. This communicates a convention to the reader. Ordinary Python assignment does not prevent you from later reassigning that name.
Use a named constant when its meaning matters in several places. Changing one definition is clearer than hunting for unexplained copies of the same number.
Be precise when explaining this: Python’s uppercase naming style expresses intent, rather than enforcing the immutability of a variable binding.
MINUTES_PER_HOUR = 60 hours = 2 total_minutes = hours * MINUTES_PER_HOUR print(total_minutes)
EXPECTED OUTPUT
120
12 | CREATE A SMALL PROFILE
Write profile.py with a first name, student code, whole-number project count, fractional practice time and Boolean completion flag. Use invented values. Print a readable report and the type of each stored value.
Then save a copy of the practice time before changing it. Explain why the saved numeric value remains unchanged. Finally improve any vague names in your code.
Challenge: add a named hours-to-minutes constant and calculate total practice minutes. Describe which values are text, which are numeric and why.