Python Variables

A Python variable lets us store a value in memory. We can do this for different data types like strings, numbers, etc. Assigning a value to a variable is called a declaration. You give a variable a name, then use the equal symbol (=) to declare the value. Variables are useful when you want to re-use values in your code.

In this lesson, I’ll give you some examples of how we can use variables in Python.

Strings

Let’s say we write a script that we use to connect to multiple network devices. When we attempt to connect, we want to show a message to the user. For example, the following string:

It’s likely that I use this exact string multiple times in my program. I could copy and paste this string multiple times, but that’s not efficient. It’s easier to use a variable. Let me show you:

Once you declare a variable, you can use it multiple times. Every time I use variable “a”, it shows our string. Great!

Numbers

We can also use this for numbers. Let’s declare a number to variables “x” and “y”:

We now have a variable named “x” with number 10 and variable named “y” with number 5. You can now use these variables everywhere. For example, some math:

Re-declare Variable

You can also re-declare a variable. Here is an example:

We can easily overwrite the value of the variable.

Variables

You can even refer from one variable to another. Let’s take a look:

I create the variable “z” by referring to variable “x”. What exactly is variable “z” though? Is it a copy or a shortcut to variable “x”? Let’s find out:

I re-declare the variable “x” to another number. This is no problem in Python. As you can see, the variable “z” does not change. When you refer from one variable to another, it’s not a shortcut but Python copies the data.

Functions

We can also declare a variable to a Python function. For example, the “input” function asks the user for an input. When you execute the code, Python waits until you enter a value. Here’s how it works:

In the code above, I declare the output of the input function to the “hostname” variable.

Variable Names

Before we continue, I’d like to give some advice about variable names. In the example above, I used variables named x, y, and z. This looks clean, but these variables don’t tell me anything by looking at them. Sometimes, developers use these variable names because they are in the middle of something and don’t want to stop to think about a decent variable name.

When I use a variable like “hostname”, at least it gives me a clue what the variable is about. On the other hand, a variable like “hostname_from_user_input_function” is long and difficult to read if you use it many times in your code.

Code is read more than it is written so it pays off to use decent variable names. What you write today might not make sense to you six months later. Try to use short variable names that mean something to you.

Conclusion

In this lesson you learned how to use Python variables:

  • Declaration means we assign a value to a variable.
  • Variables allow you to re-use values in your code.
  • We can use variables for many things including strings, numbers, functions, and more.
  • Re-declaring a variable is no problem.
  • Stick to short, meaningful variables names if possible to make your code easy to read.

Ask a question or start a discussion by visiting our Community Forum