Understanding Variables

An introduction to the use of Variables in Python

Understanding Variables

Variables are an important programming concept to master. They are essentially symbols that stand in for a value you’re using in a program.

This tutorial will cover some variable basics and how to best use them within the Python 3 programs you create.

In technical terms, a variable is assigning a storage location to a value that is tied to a symbolic name or identifier. The variable name is used to reference that stored value within a computer program.

You can think of a variable as a label that has a name on it, which you tie onto a value:

Value

Let’s say we have an integer, 103204934813, and we want to store it in a variable rather than continuously retype the long number over and over again. Instead, let’s use something that’s easy to remember like the variable my_int:

my_int = 103204934813

If we think of it like a label that is tied to the value, it will look something like this:

Value

The label has the variable name my_int written on it, and is tied to the integer value 103204934813.

The phrase my_int = 103204934813 is an assignment statement, which consists of a few parts:

  • the variable name (my_int)
  • the assignment operator, also known as the equal sign (=)
  • the value that is being tied to the variable name (103204934813)
Together, those three parts make up the statement that sets the variable my_int equal to the value of the integer 103204934813.

As soon as we set a variable equal to a value, we initialise or create that variable. Once we have done that, we are set to use the variable instead of the value. In Python, variables do not need explicit declaration prior to use like some programming languages; you can start using the variable right away.

As soon as we set my_int equal to the value of 103204934813, we can use my_int in the place of the integer, so let’s print it out:

my_int = 103204934813
print(my_int)
Show Output
103204934813

Using variables, we can quickly and easily do math. With my_int = 1040, let’s subtract the integer value 813:

print(my_int - 813)
Show Output
103204934000

In this example, Python does the maths for us, subtracting 813 from the variable my_int to return the sum 103204934000.

Speaking of maths, variables can be set equal to the result of a maths equation. Let’s add two numbers together and store the value of the sum into the variable x:

x = 76 + 145

The above example may look like something you’re already familiar with: algebra. In algebra, letters and other symbols are used to represent numbers and quantities within formulas and equations, just like how variables are symbolic names that represent the value of a data type. For correct Python syntax, you’ll need to make sure that your variable is on the left side of any equations.

Let’s go ahead and print x:

x = 76 + 145
print(x)
Show Output
221

Python returned the value 221 because the variable x was set equal to the sum of 76 and 145.

Variables can represent any data type, not just integers:

my_string = 'Hello, World!'
my_flt = 45.06
my_bool = 5 > 9 #A Boolean value will return either True or False
my_list = ['item_1', 'item_2', 'item_3', 'item_4']
my_tuple = ('one', 'two', 'three')
my_dict = {'letter': 'g', 'number': 'seven', 'symbol': '&'}

If you print any of the above variables, Python will return what that variable is equivalent to. For example, let’s work with the assignment statement for the list data type above:

my_list = ['item_1', 'item_2', 'item_3', 'item_4']
print(my_list)
Show Output
['item_1', 'item_2', 'item_3', 'item_4']

We passed the list value of ['item_1', 'item_2', 'item_3', 'item_4'] to the variable my_list, and then used the print() function to print out that value by calling my_list.

Variables work by carving out a little area of memory within your computer which accepts specified values that are then associated with that space.

Introduction

Write a program that stores a simple string variable and then prints it on screen.

Write a program that stores an integer calculation as a variable and then prints it on screen.

Write a program that stores each of a String, Number and List variable. It should then output them on screen explaining what they are.

8.1 Understanding Variables Task
Submit your completed task...
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.