Over the course of teaching for many years, I have seen learners make the same patterns of errors. I collect these common misconceptions and use them to create scaffolded exercises.
An example exercise I have created about naming variables in Python is here
Find out if each of the following is either an acceptable or not acceptable Python identifier.
Do this by trying to assign them as an identifier for a variable.
If you get a SyntaxError: can't assign to operator, fix the error by changing the identifier.
The activity starts with a "low bar", relatively straightforward examples with clear errors. Then builds in complexity to common edges cases. Then the activity crescendos with thinking about the deeper structure of the programming language.
After the learners have attempted the problems, I walk through the solutions as guide pointing out interesting stops along the way. The solutions are here.
# Solutions
sumTotal=42# Valid
# sum-total = 42 # Invalid. Dashes are a no-no.
sum_total=42
# sum total = 42 # Invalid. Spaces are a no-no. White space is often meaningful in Python.
sum_total=42
sum_total=42# This is valid. The best example because it follows pep8 style guidelines.
# sum = 42 # Valid but a terrible idea. Never name a variable or function with the same name as a built-in.
total=42# Better alternative. This is my preference for naming - clarity is primary and beverity is secondary.
# 2x = 42 # Invalid
x2=42
# -4 = 42 # Invalid
neg_4=42
# if = 42 # Invalid. Reserved keyword.
if_variable=42
# while = 42 # Invalid. Reserved keyword.
while_variable=42
Private=42# Valid
public=42# Valid
_static=42# Valid. Creates a "hidden" variable.
_4=42# Valid. Creates a "hidden" variable.
_=42# Valid. Creates a "hidden" variable. This is common for throw-a-away variables - for i, _ in enumerate(["one", "two", "three"]): print(i)
___=42# Valid. More of the same "hidden" variable hack in Python.
# $16 = 42 # Invalid
dollars_16=42
U$O=42# Invalid
USO=42
# 10% = 42 # Invalid
ten_percent=42
maçã_da_beira_alta="🍎"# Valid. Python tries to be inclusive and support as many human languages as possible.
I live coding everything so we can explore the concepts together.
Hopefully by the end, the students understand both the surface appearance (valid and invalid variable names in Python) and deeper structure (the logic behind the language constraints).
Along the way the students will start to developing their "taste", the ability make choices based on their own experience and preferences.