Now, we step into the realm of variables. Variables are like containers that allow us to store and manipulate data within our programs. They provide a means to reference objects and work with values efficiently. Once an object is assigned to a variable, you can refer to the object by that name. The data of the object can be accessed or modified using the variable. Variables are fundamental to programming as they enable us to write flexible and dynamic code.

If you haven’t already, I recommend following along in your own Python interpreter or text editor to practice these concepts.

Assignment statements

An assignment statement is used to assign a value to a variable. This is done using the assignment operator, which is the equals sign (=). Assignment statements are straightforward yet powerful, as they allow you to initialize and update the values that variables hold.

Simple Assignment
The most basic form is assigning a single value to a single variable. For example:
x = 5

Here, the variable x is assigned the value 5.

Multiple Assignment
Python allows for multiple assignments in a single statement, which can save time and make the code more concise. For example:
x, y = 5, 10

In this case, x is assigned the value 5, and y is assigned the value 10.

Chained Assignment
You can also assign the same value to multiple variables simultaneously. This is known as chained assignment. For example:
x = y = z = 0

Here, x, y, and z are all assigned the value 0.

Assignment with Operators
Python supports combining arithmetic, bitwise, and other operations with assPython supports combining arithmetic, bitwise, and other operations with assignment. This can be handy for updating the value of a variable based on its current value. For example:
x = 5
x += 1 # equivalent to x = x + 1

After this operation, x will be 6

Note: You cannot assign to an expression in Python. For instance, 5 = x or (x + y) = z are invalid and will result in an error.

Using variables

Naming conventions

Characters
Variable names can include letters, numbers, and underscores, but must start with a letter or an underscore. For example:
my_var = 42
_private_var = "Hello"
Case Sensitivity
Variable names are case-sensitive, meaning myVar and myvar are considered different variables. For example:
myVar = 10
myvar = 20
Conventions
Typically, variable names should be lowercase with words separated by underscores (snake_case), especially for variable names with multiple words. For example:
user_name = "JohnDoe"
Reserved Words
Avoid using Python's reserved words or built-in identifier names as variable names. For example:
# Avoid
print = "Hello"

Using reserved words can lead to errors or unexpected behavior in your code.

Types of variables

There are two types of variables: local variables and global variables. Local variables are declared inside a function and are only accessible within that function. Global variables are declared outside any function and are accessible anywhere in the code.

Example of a local variable

def my_function():
local_var = "This is a local variable"
print(local_var)

my_function() # Output: This is a local variable

In this example, local_var is a local variable and can only be accessed within my_function.

Example of a global variable

global_var = "This is a global variable"

def my_function():
print(global_var)

my_function() # Output: This is a global variable

In this example, global_var is a global variable and can be accessed both inside and outside the function.

Variable scope

The scope of a variable is the area of the code where it is accessible. The scope is determined by where the variable is declared. Variables in the global scope are accessible anywhere in the code, whereas local variables are confined to their respective functions. Understanding scope is crucial for writing clean and error-free code.

For example:

def outer_function():
outer_var = "Outer variable"

def inner_function():
inner_var = "Inner variable"
print(outer_var) # Accessible
print(inner_var) # Accessible

inner_function()
# print(inner_var) Not accessible, will cause an error

outer_function()

Quiz Question

What is the purpose of an assignment statement in programming?

Quiz Question

Which of the following is true about variable naming conventions in Python?