Python Variables: How to Store and Work with Data


Variables are fundamental to every programming language, and Python isn’t an exception to that. They allow you to store, manage, and manipulate data throughout your code. In this article, we will go over how variables work in Python, how to assign data to them, and how they interact with different data types.


📦 What is a Variable?

A variable is a named container for storing data values. That may sound convoluted, but trust me it isn’t so bad. In a nutshell, this means that you give a name to a corresponding value, that’s all! For instance, if you wanted to add 3 to a bunch of different numbers you could just type it out like below:

1 + 3
2 + 3
3 + 3

However, what if you wanted to change 3 to something else later on? This is where variables make life so much easier. Consider changing that example to the following:

myNumber = 3
1 + myNumber
2 + myNumber
3 + myNumber

Now, let’s change 3 to something else:

myNumber = 5
1 + myNumber
2 + myNumber
3 + myNumber

All we had to do is change the value of myNumber and everytime we call that variable name its value changes, too!

Those aren’t great examples of using variables in python. But you should have the idea of how they work now! Variables are for much more than just storing a number, let’s dive into the different types of variables you can have in Python.


📑 Variables and Data Types

In the example below, we are going to create a bunch of different variables and put their data type beside them in a comment:

myInteger = 1 # Integer
myFloat = 1.1 # Float
myString = "Hello" # String
myList = [1, 2, 3] # List
myBoolean = True # Boolean
myDict = {"name": "John", "age": 20} # Dictionary

As you can see, we have integers (numbers), floats (decimal numbers), strings, lists (arrays), booleans, dictionaries (objects), etc. that we can create and use in Python. If you take a closer look, you will see that we don’t need a declaration for a variable such as let, int, String, etc. that you may see in other programming languages. Python handles all of that for us, so make sure you use a good name for your variable so you don’t accidentally change it in your code!

Example:

myNumber = 3
myNumber = "3"

As you can see, myNumber is declared twice here. Python allows for us to reuse variables however we see fit. That also means changing the data types! The first declaration is an integer, where the last one is a string. When you do mathmatical calculations you might not get the answer you are hoping for… or you might just get an error!

Just make sure you are careful with your naming and variable declarations and you should be right as rain!


➕ Concatenation and Arithmetic

String concatenation is the process of combining two or more strings. In Python, we can use variables to get different strings back by concatenating them with each other.

Example:

userOne = "John"
userTwo = "Jen"
userThree = "Mary"
userGreeting = "Hello, "

print(userGreeting + userOne)
print(userGreeting + userTwo)
print(userGreeting + userThree)

As you can see below, we are able to combine these strings. This type of operation is normal when you are working with dynamic text in applications like dashboards that use user profiles.

Hello, John
Hello, Jen
Hello, Mary

Arithmetic, otherwise known as mathematics, are done easily as well. Check out the example below:

myNumOne = 1
myNumTwo = 2
myNumThree = 3
myBaseNum = 4

print(myBaseNum + myNumOne)
print(myBaseNum - myNumTwo)
print(myBaseNum * myNumThree)
print(mybaseNum * myNumTwo / myNumOne)

I wanted to include one using different operators to show that Python can do long algebraic equations. Just remember that it does follow the order of operations (PEMDAS)! The output is as follows:

5
2
12
8

Don’t forget not to use non-number data types (string, etc.) when performing calculations as that can lead to errors or incorrect answers.


🎈 Tip

I just wanted to throw this here, even though it might be a little more advanced than beginner. You can always check the type of the variable anywhere in your code by using the following function:

myInt = 3

type(myInt)

The type() function lets you put the variable name in and it return the type of variable it currently is. The above example would return the following:

<class 'int'>

This let’s you know that it is of type int in this circumstance. Try it out with your own code and get acquainted with it, you will be surprised how often that function will come in handy for validation!


📄 Summary

Variables are essential for writing dynamic, flexible Python code. By udnerstanding how to assign, use, and manage them you will be able to write more powerful programs with a lot less effort. You will also be able to validate that information is correct by checking the type of data using the type() function. It doesn’t matter if you are building a calculator, a web app, or even a machine learning model, variables will be at the core of everything you create. I hope you enjoy your development journey and thanks for including me in it!