Skip to main content

3.3) Variables: Important Rules And Advice


The case of a variable name matters. In the following example, print Pi will give an error:

>>> pi = 3.14
>>> print Pi
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    print Pi
NameError: name 'Pi' is not defined

The variable names can’t have spaces in, so use the underscore character (i.e. '_') instead:

>>> my variable = 3
SyntaxError: invalid syntax
>>> my_variable = 3

You might have noticed already that in the rest of these lines of code, for example, before and after the equal sign (=), you can add spaces as you wish.