Back to Python
2026-04-095 min read

variable (Python Programming)

Learn variable (Python Programming) step by step with clear examples and exercises.

Title: Mastering Data Storage in Python - Understanding Variables and Assignment

Why This Matters

Python variables play a crucial role in programming as they serve as containers to store data, perform calculations, and create complex programs. A strong understanding of variables is essential for acing coding interviews, debugging real-world issues, and building robust applications.

Prerequisites

Before diving into Python variables, it's important to have a good grasp of the following topics:

  • Basic Python Syntax
  • Data Types in Python (Numbers, Strings, Booleans)
  • Operators (Arithmetic, Comparison, Logical)
  • Input and Output Operations
  • Control Structures (Conditional Statements and Loops)
  • Functions and Modules

Core Concept

What are Python Variables?

In Python, variables are containers used to store data. They allow you to name data and use it later in your code. Each variable has a unique identifier, which can be any combination of letters, numbers, underscores (_), and the single character $. The first character cannot be a number.

Assigning Values to Variables

To assign a value to a variable, you use the equal sign (=). Here's an example:

x = 10
y = "Hello"
z = True

In this case, we have created three variables x, y, and z, and assigned them values 10, "Hello", and True, respectively. Variables can store different data types, making Python a dynamically typed language.

Naming Conventions

When naming your variables, follow these conventions:

  • Use meaningful names that describe the data stored in them (e.g., age, radius, or total_sales)
  • Avoid using reserved keywords as variable names (e.g., if, for, while)
  • Variable names are case-sensitive
  • Python recommends using lowercase letters with underscores for readability (snake_case)
  • It's also common to use camelCase or PascalCase for variable names in some cases, especially when working with libraries and frameworks

Multiple Assignments

You can assign multiple variables in a single line by separating them with commas:

a, b, c = 1, 2.5, "World"

In this example, we have assigned values 1, 2.5, and "World" to the variables a, b, and c, respectively.

Variable Scope

Python has two types of variable scopes: global and local.

  • Global variables are defined outside functions or modules and can be accessed from anywhere within that module.
  • Local variables are defined inside a function and are only accessible within that function. To access or modify global variables inside a function, you can use the global keyword before the variable name:
x = 10 # Global variable

def increment_x():
global x
x += 1

increment_x()
print(x) # Output: 11

Changing Variable Values

You can change the value of a variable by reassigning it with a new value:

x = 10
x = 20
print(x) # Output: 20

In this example, we first assigned x the value 10, then reassigned it the value 20.

Constants in Python

Python does not have a built-in constant keyword like some other languages. However, you can create constants by using all uppercase letters for variable names:

PI = 3.14159
MAX_TRIES = 10

While it's not enforced by Python, it is a good practice to treat these variables as constants and avoid changing their values throughout your code.

Worked Example

Let's create a simple program that calculates the area of a circle using variables:

radius = 5
area = 3.14 * radius ** 2
print("The area of the circle is:", area)

In this example, we have created a variable radius and assigned it the value 5. Then, we calculated the area of the circle using the formula A = πr², where A is the area, π is approximately 3.14, and r is the radius. Finally, we printed the result.

Common Mistakes

Forgetting to Assign a Value

One common mistake is forgetting to assign a value to a variable:

x

In this example, we have created a variable x, but we haven't assigned it any value. When you run this code, Python will throw an error because x has no value.

Using the Wrong Data Type

Another common mistake is using the wrong data type for a variable:

age = "25"
print(type(age)) # Output: <class 'str'>

In this example, we have created a variable age and assigned it the string value "25". However, since age is supposed to store an integer, we should assign it an integer value instead:

age = 25
print(type(age)) # Output: <class 'int'>

Using Reserved Keywords as Variable Names

Using reserved keywords as variable names can lead to errors or unexpected behavior:

for = "Not a valid variable name"
print(for) # Outputs an error because `for` is a reserved keyword

In this example, we have created a variable named for, which is a reserved keyword in Python. This will cause an error when you try to print the value of for. Instead, choose a different name for your variable (e.g., loop_variable).

Practice Questions

Question 1:

What will the following code output?

x = "Hello"
print(type(x))

Answer 1:

The output will be ``.

Question 2:

What is the difference between a local variable and a global variable in Python?

Answer 2:

A local variable is defined inside a function, while a global variable is defined outside functions or modules. Local variables are only accessible within their respective function, whereas global variables can be accessed from anywhere within the module.

FAQ

Can I change the data type of a variable?

Yes, you can change the data type of a variable by reassigning it with a new value of a different data type. However, this might require explicit conversion using functions like int(), float(), or str().

How do I check the data type of a variable in Python?

You can use the built-in type() function to check the data type of a variable:

x = 10
print(type(x)) # Output: <class 'int'>

Can I have variables with the same name in different scopes?

Yes, you can have variables with the same name in different scopes. As long as they are not in the same function or module, they will be treated as separate variables.

What is the difference between mutable and immutable data types in Python?

In Python, some data types like lists, dictionaries, and sets are mutable, meaning their values can be changed after being created. On the other hand, numbers, strings, and tuples are immutable, meaning their values cannot be changed once they are created.

variable (Python Programming) | Python | XQA Learn