Search
Variables

Variables

Think of a variable as a name attached to a particular data object. In Python, you don't need to declare your variable or the type of data you'd like it to store at the start of your program, as is the case in many other programming languages. You can create and declare variables as the need arises. To create a variable, you assign it a value and then start using it in future instructions to the computer. Assignment is done with a single equals sign (=):

x = 5
print(x)
5

You can ask Python report what type of data is stored to a variable by using the type() function.

type(x)
int
a = 'hello'
type(a)
str
b = 3.1
type(b)
float

Using the type() function can be helpful to inspect what type of data you have stored to a variable if you are receiving error messages that state you are using the wrong data type.

Variable Names

The examples you have seen so far have used short and non-descriptive names like x, y, a, and b. Programming best practices will suggest use of more descriptive variable names that indicate what type of data the variable refers to.

Officially, variable names in Python can be any length and can consist of uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore character ( _ ). An additional restriction is that, although a variable name can contain digits, the first character of a variable name cannot be a digit.

In order to make longer, multi-word variables more readable in your code, there are a variety of capitalization protocols you could use. All of these are standard, so choose which one best suits your style and use it consistently.

  • Camel Case: Second and subsequent words are capitalized, to make word boundaries easier to see.

    Example: myFirstVariable

  • Pascal Case: Identical to Camel Case, except the first word is also capitalized.

    Example: MyFirstVariable

  • Snake Case: Words are separated by underscores.

    Example: my_first_variable

There are several (33 to be exact) variable names that are off-limits to you in Python, as they are reserved as keywords for other purposes. The list of keywords can be found by typing help("keywords") into the command line.

help("keywords")
Here is a list of the Python keywords.  Enter any keyword to get more help.

False               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not                 

Additional Information on Objects

A Python variable is a symbolic name that is a reference, or pointer, to an object stored in memory of the computer. Once an object is assigned to a variable, you can refer to the object by the name of the variable, but the data itself is still contained within the object.

For example the code:

x = 5

creates an integer object with the value of 5, and then creates a reference to that object with the name x.

If later in your program, the code

y = x

were to be run, it would not create a new integer object with value of 5, since once already exists. Instead, Python creates a new reference with the name y to the existing object.

This can be verified using the id() function, which returns the unique id number assigned to every object created by Python.

id(x)
140712949027776
id(y)
140712949027776

If you were to assign the value of 7 to the variable x, Python will need to create a new integer object, since there currently is not one with the needed value. This will not impact that value of y, which will still point back to the same integer object with value 5

x = 7
id(x)
140712949027840
print(y)
id(y)
5
140712949027776

If all references to an object are removed, the object is deleted from memory in a process called garbage collection.