A Crash Course in Python Variables

Ivan Leo
5 min readDec 10, 2018

--

A Quick Recap

When you think about a Python variable, what comes to mind? Most of us, including myself, are often unaware of what actually goes on under the hood.

For instance, see the code below.

x = 2y = 2if x == y:
print("X and Y have the same value")
if x is y:
print("X is Y")
if type(x) == type(y):
print("X and Y have the same type")
else:
print("X and Y have nothing in common")

What do you think the answers are? Better yet, what are we testing for in each individual conditional statement? Consider your answer to this question before continuing down the article! :)

Let’s examine the three conditional statements one at a time. ( If you’re unsure of what’s happening with the if statement,Check out my article on control flow . )

Statement 1

if x == y:
print("X and Y have the same value")

If you thought that the message was printed out, you’re right! X and Y have the same value and therefore this statement will evaluate to True. This means that the code contained within the conditional statement will be executed!

Statement 2

if x is y:
print("X is Y")

If you guessed that the message was also printed out, you’re unfortunately wrong. This is because X and Y do not share the same identity. Therefore when the two are evaluated, the statement does not evaluate to True.

Statement 3

if type(x) == type(y):
print("X and Y have the same type")

For Statement 3, the print statement will be executed! This is because the two variables share the same type, they are both integers. Therefore when the two are evaluate, the statement evaluates to True.

Variables in Greater Detail

All Variables have 3 components. A type, identity and value. (Highlight this!)

I’ll try to explain what these 3 components mean. Let’s start with some idea of what a variable is.

Identity

Your Variable isn’t really an object. It’s more like a road map. Variables, in python are pointers to specific locations in the computer memory. This is why we can redeclare the value of a variable.

Say for instance we have the following code

variable = 1
print(variable) #we get 1
variable = 3
print(variable) #we get 3

The variable hasn’t shifted its position in memory but it is now pointing to a different reference value. This is what the identity of a variable is— its location in memory!

Let’s go back to our previous code

if x is y:
print("X is Y")

When we evaluate whether x is y, we aren’t asking if x and y have the same value. We’re asking if x and y share the same location in memory.

Type

Variables are a huge bunch. There’s around 12 categories that your variable could fall into.

  1. Integers
  2. Floats
  3. Complex Numbers
  4. Boolean
  5. Strings
  6. Lists
  7. Tuple
  8. Range
  9. Dictionaries
  10. Mutable Sets
  11. Immutable Sets
Everyone’s feeling it too :/

I’m going to pull up the code for a quick reference

x=2
y=2
if type(x) == type(y):
print("X and Y have the same type")

Since X and y are both integers ( 2 is a real numbers ), they have the same type and therefore the statement evaluates to true.

Some types you will be encountering a bit more frequently will be

  1. Strings ( Eg. “Anna” , “John”)
  2. Lists (Eg. [1,2,3,4] OR [1,2, “Anna” , “John”] )
  3. Floats (1.02 — Note the addition of decimal places!)
  4. Dictionaries ({“brand”: “Nissan”, “model”: “GTX1000”, “year”: 2018}

Value

Lastly, we’ve got the value of an integer.

x=2
y=2
if x == y:
print("X and Y have the same value")

This is what you initialise your variable to be. When you declare a variable, you are assigning a value to it. ( Eg. x = 2 , you declare the value of x to be 2 )

So, to recap, a variable is an object with a type, a value and an identity! Once you’ve gotten the hang of these three components of a variable, you’re good to go! You’re just left with understanding the scope of an object :)

Scope

Each time a function is executed, a new local namespace is created.

For instance, imagine this code

#Redeclaring Variables
x = 3 #X is now declared as 3 at memory location x
x = 5 #X is now declared as 5 by shifting the reference pointer of x to another object 5
#Scope Wise
x = 3
def function():
x = 2
#These are all different instances of X. Each X lives within each individual scope, the first at a global level, the second at a function specific level and the last in the conditional loop it is nested within

There are two namespaces within this code block

  1. The Global namespace where x = 3 exists within
  2. The Function namespace where x=2 exists within

If I were to create an additional function within the function() function, it would then create an additional local enviroment.

Here are the steps that the Python Interpreter takes to find code

  1. It first searches the local namespace to find the variable the code is referencing.
  2. If no match is found, it searches the global namespace. This can be accessed by using a global keyword.
a = 10
b = 20
def myfunction():
global a
a = 11
print(a) #A is printed out as 11

The global keyword allows your function to search the global namespace. Otherwise it will cause an error in your program.

Recap

Here’s a quick tl;dr of what we covered today.

  1. Variables have a type, an identity and a value
  2. Scope matters and functions can only access variables within their scope/local namespace

--

--

Ivan Leo

Yale-NUS Computer Science Student, Full-Time Nerd and part-time podcaster/writer on Medium and some other platforms.