Python has no separate declaration statement for ordinary variables. A name is created when you bind it to an object, usually with assignment: count = 3. The key is that assignment binds a name to an object; it does not automatically copy the object.
How do you declare a variable in Python?
Use an assignment to bind a name to a value:
count = 3
message = "Hello"
items = ["pen", "notebook"]
Python creates the name in the current namespace when the assignment runs. There is no separate declaration line required before you use an ordinary variable. A name can later be rebound to a different object, and Python does not require you to specify its type in a declaration.
For example, this assignment binds count to an integer object. The name is not a container with a permanently fixed type; it is a reference that can be rebound.
Does assigning a variable copy its value?
No. Assignment binds a name to an object. The Python Tutorial puts it this way: “Assignments do not copy data — they just bind names to objects.” Python Tutorial: lists and assignment.
#1 Best Overall
That distinction matters with mutable objects such as lists. If two names refer to the same list, changing the list through one name is visible through the other:
first = ["red", "blue"]
second = first
second.append("green")
print(first) # ['red', 'blue', 'green']
second = first did not create a separate list; both names refer to one list object. Rebinding one name, however, does not rebind the other:
Rank #2
second = ["black"]
print(first) # ['red', 'blue', 'green']
Here second now refers to a different list, while first still refers to the original.
What happens when you assign a variable inside a function?
By default, a name assigned anywhere in a function is treated as local throughout that function. This can surprise you when the function also reads a same-named outer variable:
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →message = "outer"
def update():
print(message)
message = "inside"
update()
This raises UnboundLocalError at the print. Because update assigns to message, Python treats that name as local in the function; the local binding has not happened when the earlier read occurs. It does not first print the module-level value.
If the function only reads the outer name and never assigns to it, ordinary name lookup can find it:
message = "outer"
def show():
print(message)
show() # outer
For function code, lookup proceeds through local, enclosing function, global, and built-in scopes. Class bodies have special name-resolution rules, so class attributes should not be treated as ordinary function locals. See the Python language reference on name resolution.
When should you use global or nonlocal?
Use these declarations when a function needs to rebind a name in an outer scope rather than create a local binding.
Best Value
Use global for a module-level name
global tells Python that assignments to the name refer to the module-level binding:
message = "outer"
def update():
global message
message = "changed"
update()
print(message) # changed
The declaration affects rebinding, not merely reading. A function can read a global name without writing global; declare it when you intend to assign to that module-level name.
Use nonlocal for an enclosing function’s name
In a nested function, nonlocal directs rebinding to a name already bound in the nearest enclosing function scope:
def counter():
value = 0
def increment():
nonlocal value
value += 1
increment()
return value
print(counter()) # 1
The enclosing function must already bind value; nonlocal does not create a new name in an enclosing scope. The Python programming FAQ explains the local and global variable rules.
Where to learn more
For the full language context, consult the official Python tutorial, which is written for programmers new to Python, and the language reference on name resolution. The concepts here are stable across modern Python versions; the cited reference surfaced for Python 3.14.7.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

