TensorFlow simple operations tensors vs Python variables
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
TensorFlow code often mixes Python variables, tf.Tensor objects, and tf.Variable objects in the same function. They are not interchangeable. Python variables are just names in your program, tensors are immutable values used by TensorFlow operations, and tf.Variable represents mutable state that can be updated during training.
Python Variables Are Names, Not TensorFlow State
In Python, a variable is simply a reference to an object. It does not tell TensorFlow anything about gradients, devices, or graph execution.
Here x is a Python variable whose value happens to be a tf.Tensor. Reassigning x in Python just changes what name points to what object.
That is normal Python behavior, not in-place mutation of the original tensor.
tf.Tensor Is an Immutable Value
Tensors are the primary data objects TensorFlow uses for computation. They support arithmetic and can flow through models, but they are immutable. An operation produces a new tensor rather than changing the old one.
t remains unchanged. This is important when reasoning about computation graphs and functional-style model code.
Use tf.Variable for Mutable Parameters
If a value should change over time, such as a model weight, use tf.Variable.
tf.Variable is what optimizers update during training. It behaves differently from a plain tensor because TensorFlow tracks it as mutable state.
Simple Operations and Gradient Tracking
TensorFlow operations work on tensors and variables, but only trainable variables are updated by optimizers. A plain Python number participates in math, yet it is not trainable state.
bias is just a Python float embedded into the computation. w is the tracked variable that can receive gradients and be updated.
If you need bias to be trainable, make it a tf.Variable too.
Eager Execution Makes This Easier to See
In modern TensorFlow, eager execution is on by default. That means tensors behave more like regular runtime values, and you can inspect them immediately with .numpy(). This makes the distinction easier:
- Python variable means a normal program reference
- '
tf.Tensormeans an immutable computed value' - '
tf.Variablemeans mutable TensorFlow state'
That separation becomes especially important when writing custom training loops, losses, or layers.
Common Pitfalls
- Calling a Python name a "TensorFlow variable" when it is only a reference to a tensor.
- Expecting a tensor to change in place after arithmetic.
- Using Python scalars for values that should be trainable model parameters.
- Forgetting that optimizers update
tf.Variable, not arbitrary Python objects. - Mixing TensorFlow state and Python control flow without understanding which parts are tracked.
Summary
- Python variables are names that reference objects.
- '
tf.Tensoris an immutable TensorFlow value used in computation.' - '
tf.Variableis mutable state used for trainable parameters and updates.' - Arithmetic on tensors creates new tensors rather than mutating old ones.
- The distinction matters most when training models, computing gradients, and managing state.

