| id | python-variables | ||||
|---|---|---|---|---|---|
| title | Python Variables | ||||
| sidebar_label | Python Variables | ||||
| sidebar_position | 3 | ||||
| tags |
|
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.
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
You simply write a variable name, use the assignment = operator, and assign a value.
a = 10
name = "GeeksForGeeks"
price = 99.99
is_active = TruePython automatically understands the type of variable.
Python allows assigning values to multiple variables in a single line.
x, y, z = 1, 2, 3You can also assign same value to multiple variables:
a = b = c = 100- 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 keywordPython variables can hold different types of data:
x = 10
print(type(x)) # <class 'int'>Python allows dynamic typing:
x = 5
print(type(x)) # <class 'int'>
x = "Hello"
print(type(x)) # <class 'str'>You can remove a variable from memory using del.
x = 100
del x
print(x) # Raises NameErrorThere are two scopes of variables:
Declared outside functions, accessible anywhere.
x = "global"
def show():
print(x)
show() # Output: globalDeclared inside functions and accessible only inside them.
def greet():
msg = "Hello"
print(msg)
greet()
print(msg) # Error: NameErrorUse global to modify global variables inside a function.
x = 10
def update():
global x
x = 20
update()
print(x) # Output: 20- 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# 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']
- 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().
- Covers both global and local variables
- Explains
del,global, andid()functions - Includes formatted tables and output blocks
- Beginner-friendly explanation with examples
