1. python
  2. /basics
  3. /variables

The Basics of Variables in Python

Getting Started

You might be noticing a pattern here. If you're someone who spent time learning and dabbling with different programming languages, variables are one of the first concepts you'll get exposed to. Python is no different. At its core, the concept is similar in a lot of languages but there are always nuances involved. So, let's dive into an overview of how we can create and use variables.

Assignment of Variables in Python

First things first. Unlike some of its predecessors, in Python, we don't need to specify the data type of the variable before we can use it. Conveniently enough, we can assign a value to a variable and Python will automatically determine the data type based on the value we provided.

# Assign the value 10 to the variable x
x = 10
print(x) # Output: 10

# Assign the value "Hello, World!" to the variable message
message = "Hello, World!"
print(message) # Output: "Hello, World!"

Notice the equal sign (=). Now, intuitively we might think of it as something that "equals to". A better approach would be to shift our mindset into thinking of it as "is set to". Simply put, this means that when we write x = 10, we are setting the value of x to 10, not checking if x is equal to 10.

Variable and Types

We already touched upon this, so let's expand it a bit more. In various programming languages, variables are statically typed. So, if we declare a variable with a specific data type, the value we assign to it must be of that type during its entire lifetime. Considering that Python is dynamically typed, we're free of such restrictions, so in our variables, we can assign a value of one type and then re-assign a different one later on.

# Assign an integer value to the variable x
x = 10
print(x) # Output: 10

# Re-assign a string value to the same variable x
x = "Hello, World!"
print(x) # Output: "Hello, World!"

Object Referencing and Identity

In the examples, we saw several variables and assigned values to them. As a beginner one should be aware of the following:

In Python, variables do not store values directly. Instead, they store references to objects that contain the values.

Let's break that down.

Variables act as symbolic labels that reference, or simply put, point to an object. When an object is assigned to a variable, the variable serves as a means to access the data type stored within the object. However, the actual data remains contained within the object itself.

# Assign an integer to x
x = 7
print(x)

# Output: 7

# X will now point to a string
x = "I'm a string reference now"
print(x)

# Output: "I'm a string reference now"

So what would happen if we did the following?

x = 7

y = x

In our example, x is assigned the value 7, which creates an integer object. When we assign y to x, both variables will now reference the same integer object.

To prove our claims, we can use the built-in id() function in Python. As a result, it will return a handy unique identifier for the object.

id(x), id(y)
# Output: 8461136, 8461136

Let's give y a new value.

y = 14

When y is reassigned to a new value, in this case, 14, a new integer object will be created, and y will reference this new object.

Now, let's check the identifier for y:

id(y)
# Output: 8461304

Naming Conventions in Python Variables

Naming variables is an exhaustive topic by volume and is usually a good enough reason for hot debates between developers. So, we'll cover more details sparsely in other articles as the need arises.

In Python, naming variables can consist of the uppercase letters "A" through "Z", lowercase letters "a" through "z", the underscore _, and digits 0 through 9, with the exception of the first character where we can't use a digit. As of Python 3.x(version 3 and onward), variable names and identifiers can also contain Unicode characters.

You'll come across several naming conventions. We commonly use the snake case, where each word is separated by an underscore. Then there's Pascal case, where each word is capitalized, and lastly, we have the camel case which you might already be familiar with, where the first word is in lowercase and subsequent ones are capitalized.

Let's observe them closely:

# Snake Case
first_name = "Guido van Rossum"

# Pascal Case
FirstName = "Guido van Rossum"

# Camel Case
firstName = "Guido van Rossum"

Bear in mind that Python is a case-sensitive language, so variable names such as player_age and Player_Age are considered to be different. Lastly, there are a set of reserved keywords we should avoid. We'll look to cover those in more detail in a separate article.

Final Thoughts

Variables are the bread and butter of many programming languages and so it's the case with Python. We illustrated how variables work by going through the basics of variable assignment, the different variable types, object referencing and identity, and their naming conventions.

With this knowledge, we encourage you to try out some examples on your own and connect your existing skills to adjacent Python topics. Remember, the prerequisite to writing complex code is hours of writing simple logic first.

Useful Resources

Official Python Documentation

PEP 8 – Naming Conventions According to the Style Guide for Python Code

The Unicode Consortium