Skip to content
Skip to main content

About this free course

Download this course

Share this free course

Learn to code for data analysis
Learn to code for data analysis

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

1.3 The art of naming

Python is relatively flexible about what you name your variables but rather picky about the format of names.

An image of blank name labels headed, 'Hello my name is'
Figure 3

I could have chosen deaths_in_Brazil_in_2013, deathsBrazil,DeathsBrazil, dB or even stuff for my variables. If a box in your attic were labeled dB or stuff though, would you know what it contains a year later? So, although you can, it’s better not to use cryptic, general, or very long names.

You can’t use spaces to separate words in a name, you can’t start a name with a digit and names are case-sensitive, i.e. deathsBrazil and DeathsBrazil are not the same variable. Making one of those mistakes will result in a syntax error (when the computer doesn’t understand the line of code) or a name error (when the computer doesn’t know of any variable with that name).

Let’s see some examples. (Remember that you’re not expected to write any code for this step.) The first example has spaces between the words, the second example has a digit at the start of the name, and the third example changes the case of a letter, resulting in an unknown name. The kind of error is always at the end of the error message.

In []:

deaths In Portugal = 140

File "", line 1

deaths In Portugal = 140

^

SyntaxError: invalid syntax

In []:

2013deathsInPortugal = 140

File "", line 1

2013deathsInPortugal = 140

^

SyntaxError: invalid syntax

In []:

deathsinPortugal

---------------------------------------

NameError Traceback (most recent call last)

in

----> 1 deathsinPortugal

NameError: name ‘deathsinPortugal’ is not defined

Note that Jupyter doesn’t write any Out[] because the code is wrong and thus doesn’t generate any output.

In this course, to make names shorter to help fit lines of code on small screens, we’ll use capitalisation instead of underscores to separate the different words of a name, as shown in the code so far. Such practice is called camel case independently of the name having oneHump (‘dromedary case’ just doesn’t sound good, does it?) or moreThanTwoHumps . The convention in Python is to start variable names with lower case and we’ll stick to it.

In the next section, download the notebook for this week and work through the first exercise – your first line of code!