Basics
Variables
Python variables are names bound to objects. Assignment creates or rebinds a name; it does not require a declaration and it does not permanently attach a type to the name.
Rebinding changes which object a name refers to. Augmented assignment such as += is the idiomatic way to update counters and accumulators.
Use clear names for values that matter later. Python's flexibility makes naming more important, not less.
Use assignment when a value needs a name for reuse or explanation. Prefer a direct expression when naming the intermediate value would add noise.
Assignment binds a name to a value. Once bound, the name can be used anywhere that value is needed.
Source
message = "hi"
print(message)Output
hiAssignment can rebind the same name to a different value. The name is not permanently attached to the first object.
Source
message = "hello"
print(message)Output
helloAugmented assignment reads the current binding, computes an updated value, and stores the result back under the same name.
Source
count = 3
count += 1
print(count)Output
4Notes
- Python variables are names bound to objects, not boxes with fixed types.
- Rebinding a name is normal.
- Use augmented assignment for counters and accumulators.
See also
- related: Values
- next depth: Mutability
- related: Object Lifecycle
- related: Constants
Run the complete example
Expected output
hi
hello
4
Execution time appears here after you run the example.