Python Variables
In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable is inferred based on the value assigned.
Variables act as placeholders for data. They allow us to store and reuse values in our program.
1. What is a Variable?β
A variable is like a container for storing data values. You donβt need to declare its type explicitly β Python handles it dynamically.
x = 5
y = "Hello"
Here:
xis of typeintyis of typestr
2. How to Declare and Assign Variablesβ
You simply write a variable name, use the assignment = operator, and assign a value.
a = 10
name = "GeeksForGeeks"
price = 99.99
is_active = True
Python automatically understands the type of variable.
3. Multiple Assignments in One Lineβ
Python allows assigning values to multiple variables in a single line.
x, y, z = 1, 2, 3
You can also assign same value to multiple variables:
a = b = c = 100
4. Variable Naming Rulesβ
- Must start with a letter (aβz, AβZ) or an underscore (_)
- Can contain letters, digits, and special character like underscore.
- Are case-sensitive (
nameandNameare different). - Cannot use reserved keywords like
if,class,def, etc.
# Valid
my_var = 1
_var = 2
var3 = 3
# Invalid
3var = 10 # starts with digit
my-var = 20 # hyphen not allowed
def = 30 # 'def' is a keyword
5. Standard Data Types in Pythonβ
Python variables can hold different types of data:
6. Type Checking with type()β
x = 10
print(type(x)) # <class 'int'>
7. Changing Variable Type Dynamicallyβ
Python allows dynamic typing:
x = 5
print(type(x)) # <class 'int'>
x = "Hello"
print(type(x)) # <class 'str'>
8. Deleting a Variable with delβ
You can remove a variable from memory using del.
x = 100
del x
print(x) # Raises NameError
9. Scope of Variablesβ
There are two scopes of variables:
10. Global Variableβ
Declared outside functions, accessible anywhere.
x = "global"
def show():
print(x)
show() # Output: global
11. Local Variableβ
Declared inside functions and accessible only inside them.
def greet():
msg = "Hello"
print(msg)
greet()
print(msg) # Error: NameError
π’ The global Keywordβ
Use global to modify global variables inside a function.
x = 10
def update():
global x
x = 20
update()
print(x) # Output: 20
12. Memory Management in Pythonβ
- Python variables are names bound to objects in memory.
- Use
id()to get the memory address (or reference ID) of a variable.
x = 5
print(id(x))
If two variables have the same immutable value, they may share the same memory.
a = 100
b = 100
print(id(a) == id(b)) # True
π’ Example Programβ
# Python Variable Example
name = "Dhruba"
age = 22
price = 49.99
is_valid = True
items = ["pen", "notebook"]
print("Name:", name)
print("Age:", age)
print("Price:", price)
print("Valid:", is_valid)
print("Items:", items)
Output:
Name: Dhruba
Age: 22
Price: 49.99
Valid: True
Items: ['pen', 'notebook']
Summaryβ
- Python variables store different types of data without explicit declaration.
- Variables are case-sensitive and follow naming rules.
- Scope determines where a variable is accessible.
globalanddelare important keywords for variable handling.- Python handles memory management internally but allows inspection via
id().
Highlightsβ
- Covers both global and local variables
- Explains
del,global, andid()functions - Includes formatted tables and output blocks
- Beginner-friendly explanation with examples