
Python Variables
In Python, variables act as labeled containers that store data for your program to use and update. Think of a variable as a descriptive name assigned to a specific value, allowing you to easily reference or change that information later in your code.
Characteristics
- Dynamic Typing: You do not need to declare a variable’s data type explicitly. Python infers the type at runtime based on the assigned value and allows it to change through reassignment.
- Creation via Assignment: A variable is created the moment you first assign it a value using the
=operator (e.g.,x = 5). - Object References: Variables do not “contain” values directly; instead, they point to a memory location where the data object exists.
Naming Rules
- Characters: Must contain only letters (A-Z, a-z), digits (0-9), and underscores (
_). - Starting Character: Must start with a letter or an underscore; it cannot start with a digit.
- Case Sensitivity:
my_varandMY_VARare treated as two distinct variables. - Reserved Keywords: Names cannot be Python’s reserved keywords, such as
if,for,class, ordef. Probably I’ll write a separate post for this. - Snake Case: By PEP 8 standards, multi-word names should use lowercase letters separated by underscores (e.g.,
user_age).
Variable Scope
In Python, variable scope refers to the region of a program where a variable can be accessed or modified. Scope helps control the visibility and lifetime of variables, ensuring that data is used only where it is intended.
Global Scope
A variable declared outside of any function or class belongs to the global scope. Such variables can be accessed anywhere in the program, including inside functions. Global variables are useful when multiple parts of a program need to share the same data.
global_var = "I am a global variable"
def print_value():
print(global_var)
print_value() # Output: "I am a global variable"
But If you want to modify a global variable inside a function, you must explicitly use the global keyword:
global_var = "I am a global variable"
def print_value():
global global_var
global_var += " inside a function"
print(global_var)
print_value() # Output: "I am a global variable inside a function"
Local Scope
A variable declared inside a function has a local scope. This means it is only accessible within that specific function and cannot be used outside of it. Local variables are created when the function is called and destroyed when the function finishes execution.
def print_value():
local_var = "I am a local variable"
print(local_var)
print_value() # Output: "I am a local variable"
Assigning Values to Variables
Start up your python Interpreter, we’ll be going through some examples.
data = "Hello World!!"
print(data) #Output: Hello World!!
You can assign same value for multiple variables in a single line
a = b = c = 0
print(a) #Output: 0
print(b) #Output: 0
print(c) #Output: 0
You can assign different values for multiple variables in a single line
a, b, c = 0, 1, 2
print(a) #Output: 0
print(b) #Output: 1
print(c) #Output: 2
You can also reassign a variable to a different value at a later point in execution once the variable is defined
data = "Hello World!!"
print(data) #Output: Hello World!!
data = 10
print(data) #Output: 10
We’ll be seeing about Data Types, Operations in the upcoming blogs