Variables

code guest
Python Journey
Published in
2 min readSep 1, 2019

Descriptive names and Variables

When we look at a piece of python code, we can often find descriptive names such as price, data, query, array or result.

These descriptive names are variables — and a variable is a name that refers to a data value.

Let’s say you are looking at code that prints the price of something (integer or float data type) to the screen, that code probably looks like this:

print(price)
  • The descriptive name price is a variable that actually refers to an integer or float value. In programming, we call these descriptive names variables
  • in the code above — print(price), the print() function displays on our screens the integer or float value referred to by the variable called price
A variable is a name that refers to a data value (credits: undraw.co)

Referenced data values

Variables are something like GPS trackers on the data values they refer to.

  • Think of data as information — like the number of animals on Macdonald’s farm.
  • And that data can change — like when the number of animals on Macdonald’s farm increases or decreases
  • When we assign a data value (eg. integer count of number of animals) to a variable (called numberOfAnimals for instance), that variable is like a name given to that data value
  • Whenever we need find out what the data value is or update its value, we reference the variable that tracks it eg. print(numberOfAnimals)
  • For example, when we add or remove an animal, we let the variable numberOfAnimals reference a new integer data value reflecting the increment or decrement (Note: we associate a new integer data value because integer data type is immutable)
  • Then when we want to find out the total number of animals on the farm, we check the variable to get the data stored.

Fun fact: Variables are called variables because we can modify their associated data values.

References

Summary

  1. Every variable has a name and an associated data value (even None, empty string “” or empty list [])
  2. We can modify a variable’s data value by working with the variable in our code

Quiz

Which of the following options are incorrect?

a) Variables look like names

b) Variables are generally descriptive

c) Variables are just plain text

d) Variables can refer to operators

Quiz explanation

a) Variables do look like names — because we can give our variables names and work with variables by using their names in our code

b) Variables are generally given names that describe the data values they contain

c) Variables are not just plain text — they give us access to the data values that they refer to

d) Variables must refer to data values. Also, operators are not stand-alone — they need operands around them

--

--