In programming, a variable is a storage location in the computer’s memory that is associated with a name (identifier) and used to store data that can be modified during program execution. The value of a variable can change, and it allows the program to hold data for later use.
Think of a variable as a labeled box that can hold a value. You can change the value inside the box, but the label (the variable name) remains the same.
In most programming languages, variables must be declared before they can be used. In Python, variables are dynamically typed, meaning you don't have to specify their type when declaring them. The type is determined automatically based on the assigned value.
age = 25
name = "Alice"
is_student = True
In this example:
age is an integer variable holding the value 25.name is a string variable holding the value "Alice".is_student is a boolean variable holding the value True.In Python, variable names must follow certain rules:
age and Age are different variables).class, if, else, etc.) as variable names.user_name = "John" # Valid variable name
_123abc = 10 # Valid variable name
class = "Programming" # Invalid variable name (reserved keyword)
Variables can hold different types of data. The type of data a variable holds defines what operations can be performed on it. Below are common types of variables:
age = 30price = 19.99name = "Alice"is_active = Truenumbers = [1, 2, 3]person = {"name": "John", "age": 25}In Python, assigning a value to a variable is simple. You use the assignment operator =. This operator assigns the value on the right to the variable on the left.
height = 5.7 # Assigning a float value
name = "Alice" # Assigning a string value
is_active = True # Assigning a boolean value
Once assigned, you can use the variable in your program for calculations, decisions, or displaying the value.
Variables are dynamic in Python, meaning you can change the value of a variable at any time. This is called reassigning a variable.
age = 25
print(age) # Output: 25
age = 30 # Reassigning a new value
print(age) # Output: 30
Here, we first assign age the value 25 and print it. Then, we reassign age to 30 and print the new value.
Variables can be used in expressions to perform operations on the stored data. You can combine variables, use arithmetic operators, and more.
x = 10
y = 5
sum = x + y # Adding two variables
product = x * y # Multiplying two variables
print(sum) # Output: 15
print(product) # Output: 50
In this example, we use variables x and y in an addition and multiplication expression to compute their sum and product.
The scope of a variable defines where it can be accessed in the program. There are two main types of scope:
x = 10 # Global variable
def my_function():
y = 5 # Local variable
print(x) # Can access global variable
my_function()
print(y) # Error: 'y' is not defined outside the function
In this example, the global variable x is accessible inside the function my_function, but the local variable y cannot be accessed outside the function.
Good variable names make your code more readable and maintainable. Here are some best practices for naming variables:
age for a variable that stores a person's age, instead of just x.student_age) for variable names.x or y are fine for temporary values, use meaningful names for clarity.Variables are fundamental in programming, allowing data to be stored, manipulated, and reused throughout your program. By understanding how to declare, assign, and use variables effectively, you can start building more complex programs and solving real-world problems.
As you continue your learning, remember to follow good practices for naming variables and manage their scope appropriately to write clean and efficient code.