Basics

Variables

Names are bound to values with assignment.

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

hi
A name is a label that points at an object. Assignment binds the label; the object exists independently.

Assignment 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

hello

Augmented 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

4

Notes

See also

Run the complete example

Example code

Expected output

hi
hello
4

Execution time appears here after you run the example.