AlgoMaster Logo

Variables

Last Updated: December 6, 2025

6 min read

One of the most important pieces in Python is the variable. It’s how we store and manage data in our programs.

Think of a variable as a container for holding information that can change over time. In this chapter we'll dive into the essentials of variables in Python.

What Are Variables?

At their core, variables are names that you give to data. This allows you to reference that data throughout your program without needing to remember the actual value.

For example, instead of writing 3.14 every time you need to use the value for pi, you could assign it to a variable named pi.

In Python, you create a variable by simply assigning a value to it using the equals sign (=). Here’s a quick example:

This line of code creates a variable called pi and assigns it the value of 3.14. The print() function then outputs that value.

Naming Variables

Choosing the right name for your variables is crucial. A good name makes your code more readable and easier to maintain.

Here are some key guidelines for naming variables in Python:

  • Descriptive Names: Use names that clearly indicate the purpose of the variable. Instead of a, use radius if it holds a circle's radius.
  • Lowercase with Underscores: Python convention suggests using lowercase letters and underscores to separate words (snake_case).
  • Avoid Reserved Keywords: Don’t use Python keywords like if, else, or while as variable names.
  • Case Sensitivity: Variable names are case-sensitive. myVariable and myvariable are considered different.

Variable Assignment

In Python, variables can be assigned values in several ways. Let’s explore some common methods.

Single Assignment

You can assign a single value to a variable:

Multiple Assignment

Python allows you to assign values to multiple variables in a single line, which can save time:

This is not just a neat trick; it’s a powerful feature that can make your code more concise.

Swapping Values

You can also swap values between variables using the same syntax:

This feature helps in scenarios where you need to reorganize values dynamically without using a temporary variable.

Variable Types and Mutability

Variables in Python are dynamically typed, meaning you don’t need to declare their type explicitly. The type is inferred from the value assigned. However, understanding how types relate to variables is important, particularly when dealing with mutability.

Mutable vs Immutable

  • Immutable Types: These are types that cannot be changed. Examples include int, float, str, and tuple. If you try to change them, a new object is created in memory.
  • Mutable Types: Lists, dictionaries, and sets are mutable. You can modify them in place without creating a new object.

Understanding this distinction helps avoid unexpected errors when manipulating data structures.

Scope of Variables

The scope of a variable refers to the context in which it is valid. It’s essential to understand how scopes work, especially when writing larger programs.

Local Scope

Variables defined inside a function are local to that function. They cannot be accessed outside of it.

Global Scope

Variables defined outside of any function are global. They can be accessed from anywhere in the code, including inside functions, though it’s not always good practice to rely on global variables.

Using global variables can lead to issues in larger applications, so try to limit their use.

Practical Use Cases

Now that we’ve covered the fundamentals, let’s discuss some practical scenarios where variables play a crucial role.

Configuration Settings

Variables can be used to store configuration settings for your applications.

You can easily modify these values without sifting through your code every time.

Looping and Iteration

Variables are vital when looping through data. You can use them to track the current state or count iterations.

Here, i is a variable that changes with each iteration.

User Input

You can utilize variables to handle user input and perform operations based on that input.

This simple example shows how variables can interact with users and store their input.

Common Pitfalls

Working with variables isn’t always straightforward. Here are some common issues that can trip you up:

Uninitialized Variables

If you try to use a variable before it has been assigned a value, Python will raise a NameError.

Always ensure your variables are initialized before use.

Shadowing Variables

If you define a variable inside a function with the same name as a global variable, it will shadow the global variable, which can lead to confusion.

Be mindful of variable scopes to avoid unintended shadowing.

In the next chapter, we will look at the various types of data you can work with in Python, from simple integers and floats to more complex structures like lists and dictionaries.