Variables
Info
The structure of following sections is based on the official Python tutorial.
Many thanks to @jhumci for providing the initial resource materials!
Getting started
Important
We encourage you to execute all upcoming code snippets on your machine. You can easily copy each code snippet to your clipboard, by clicking the icon in the top right corner. By doing so, you will be prepared for all upcoming tasks within the sections. Tasks are indicated by a -icon.
We recommend to create a new notebook for each chapter, e.g. create
variables.ipynb
for this chapter. Doing so, your notebooks will follow
the structure of this crash course.
You will encounter multiple info boxes throughout the course. They provide additional information, tips, and tricks. Be sure to read them thoroughly.
Let's start with the first task.
Notebook & first code cell
Create a new Jupyter notebook and name it variables.ipynb
.
Paste the following code snippet into a new cell and execute it.
The output should be:
Info
The upcoming content contains a lot of code snippets. They are easily recognizable due to their colourful syntax highlighting, such as:
Code snippets are an integral part, to illustrate concepts, which are introduced and explained along the way. Commonly, these code snippets are accompanied by an output block to display the result, for instance:
Nevertheless note, output blocks can be missing as there is not always an explicit result.
Again, execute and experiment with all code snippets on your machine to
verify the results and get familiar with Python
!
Variable
Computers can store lots of information. To do so, in Python
we use variables. A variable is a name that refers to a value. The following code
snippet, assigns the value 4
to the variable number
. In
general, you pick the variable name on the left hand side, assign a value
with =
and the value itself is on the right hand side.
You can change the value of a variable in your program at any time, and Python will always keep track of its current value.
You will notice that none of the cells had any output. To display the value
of a variable we use the print()
function.
print()
Now, we can also verify that in the above snippet the value of number
was
actually changed.
Info
Within a notebook, the variables are stored in the background and can be overwritten at any time. Therefore, it is good practice to execute all cells from top to bottom of the notebook in the right order so that nothing unexpected is stored in a variable.
Comments
Comments exist within your code but are not executed. They are used to
describe your code and are ignored by the Python
interpreter. Comments
are prefaced by a #
.
Info
Comments help you and others understand what your code is doing. It is good practice to use comments as a tool for documentation.
Variable naming
When you’re using variables in Python
, you need to adhere to a few rules and
guidelines. Breaking some of these rules will cause errors; other guidelines
just help you write code that’s easier to read and understand. Be sure to keep
the following rules in mind:
- Variable names are lower case and can contain only letters, numbers, and
underscores.
They can start with a letter or an underscore, but not with a number.
For instance, you can call a variable
message_1
but not1_message
. - Whitespace is not allowed in variable names, but an underscores
_
can be used to separate words in variable names. For example,greeting_message
works, butgreeting message
won't. - Avoid using
Python
keywords and function names as variable names; that is, do not use words thatPython
has reserved for a particular programmatic purpose, such as the wordprint
. - Variable names should be short but descriptive. For example,
name
is better thann
,student_name
is better thans_n
, andname_length
is better thanlength_of_persons_name
.
Errors (NameError
)
Every programmer makes mistakes and even after years of experience, mistakes are part of the process. With time, you get more efficient in debugging (=process of finding and fixing errors).
Debugging tactics
byu/0ajs0jas inProgrammerHumor
Let’s look at an error you’re likely to make early on and learn how to fix it. We’ll write some code that throws an error message on purpose. Copy the code and run your cell.
Which should result in:
When an error occurs in your program, the Python
interpreter does its best to
help you figure out where the problem is. The interpreter provides a traceback
which is a record of where the interpreter ran into trouble when trying to
execute your code.
Here’s an example of the traceback that Python
provides, after you’ve
accidentally misspelled a variable’s name:
Traceback (most recent call last):
File "C:\\IPython\core\interactiveshell.py", line 3577, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-35-c8f2adeaed02>", line 2, in <module>
print(mesage)
^^^^^^
NameError: name 'mesage' is not defined. Did you mean: 'message'?
The output reports that an error occurs in line 2. The interpreter shows this
line to help us spot the error quickly and tells us what kind of error it
found. In this case, it found a NameError
and reports that
the variable mesage
has not been defined. A name error usually means we made
a spelling mistake when entering the variable’s name or that the variable
simply does not exist.
Your first fix
Fix the NameError
in your code cell.
Recap
In this section, we have covered variables in Python
.
You have learned (about):
- To create and assign a value to a variable
print()
to display the value of a variable- Comments
- Naming conventions for variables
- How to fix a
NameError