Software Engineering Certificate Course

Exam & Certification

Variables in Programming

What are Variables?

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.

How to Declare Variables

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.

Example in Python:

age = 25
name = "Alice"
is_student = True

In this example:

Variable Naming Rules

In Python, variable names must follow certain rules:

Examples:

user_name = "John"   # Valid variable name
_123abc = 10            # Valid variable name
class = "Programming"   # Invalid variable name (reserved keyword)

Types of Variables

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:

Assigning Values to Variables

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.

Example in Python:

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.

Reassigning Variable Values

Variables are dynamic in Python, meaning you can change the value of a variable at any time. This is called reassigning a variable.

Example of Reassigning:

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.

Using Variables in Expressions

Variables can be used in expressions to perform operations on the stored data. You can combine variables, use arithmetic operators, and more.

Example:

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.

Scope of Variables

The scope of a variable defines where it can be accessed in the program. There are two main types of scope:

Example 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.

Best Practices for Naming Variables

Good variable names make your code more readable and maintainable. Here are some best practices for naming variables:

Conclusion

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.