Knowledge for the World

Using variables in Python

  • by tyler (203)
  • 10 minutes

This guide is an introduction to Python variables. Variables are where programming starts to get interesting. Technically speaking, a variable is a location in memory that is given a name. The actual data stored in memory is referred to as the value. Variables are helpful because it gives us an easy way to access data by name, and depending on the type of variable, that value can be almost anything!

1

Python variables can contain many different data types, but we will only cover the most basic (and most commonly used) in this guide.

When you want to store a value in a variable, you use the basic assignment operator =:

>>> meaning_of_life = 42

Notice, the variable name is on the left side of the assignment operator, while the value is on the right.

Once assigned, you can use the variable name to access the value:

>>> print('The meaning of life is: {0}'.format(meaning_of_life))
The meaning of life is: 42
2

A "type" simply refers to the kind of value being stored. In the previous example we stored a number, or an integer. However, there are many different types. The Python docs identify six principal built-in types: numerics, sequences, mappings, classes, instances and exceptions.

Types are important because different types support different operations.

For instance, adding integers makes sense:

>>> x = 1
>>> y = 2
>>> x + y
3

but adding an integer and a string does not:

>>> x = y
>>> x = 1
>>> y = 'two'
>>> x + y
...
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Data types are covered in more details elsewhere on the site, but for the purpose of this guide, you should simply keep in mind that variables have types, and different types support different operations.

3

Regarding variables in Python, there are a few rules and many conventions. At this stage, learning the rules takes priority but you'll eventually want to learn Python naming conventions as well.

Rules

Variable names must start with a letter or an underscore:

  • _result
  • result
  • 1result is invalid!

Variables names must consist of only letters, numbers, or underscores

  • _result_
  • result_1
  • a_b
  • some_other_result
  • some-result is invalid!

Names are case sensitive

  • result, Result, and RESULT are all different.
4

The scope of a variable refers to where in the program the variable can be accessed, and the scope is determined by where the variable is defined.

For this guide we are going to cover kinds of scope, global and local. Local variables are defined within a function and are accessible only within the function itself. Global variables are defined in the main body of a file and are accessible throughout.

Take the following code snippet as an example:

#!/usr/bin/env python3

result = 55

def print_result():
    print(result)

The function print_result has access to the result variable because it's defined in the main body of the file. So when we call the function, it will print exactly what we expect. In this case, result is a global variable.

In the following example, result is a local variable.

#!/usr/bin/env python3

def get_result():
    result = 55
    return result

One might be tempted to try and access this variable outside of the function, but it cannot be accessed as it's scoped locally, meaning it's accessible only to the function that defines it.

#!/usr/bin/env python3

def get_result():
    result = 55
    return result

print(result)

This will result in the following traceback, because result is not defined in the scope in which it's being accessed.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'result' is not defined
Series: Python Basics
Show all in this series