TensorFlow
Tensors
Python Variables
Machine Learning
Deep Learning

TensorFlow simple operations tensors vs Python variables

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

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.

python
1import tensorflow as tf
2
3x = tf.constant([1.0, 2.0, 3.0])
4y = x + 1.0
5
6print(type(x))
7print(y.numpy())

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.

python
x = tf.constant([10.0, 20.0, 30.0])
print(x.numpy())

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.

python
1import tensorflow as tf
2
3t = tf.constant([1, 2, 3], dtype=tf.int32)
4u = t * 2
5
6print(t.numpy())
7print(u.numpy())

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.

python
1import tensorflow as tf
2
3w = tf.Variable([1.0, 2.0, 3.0])
4print(w.numpy())
5
6w.assign([4.0, 5.0, 6.0])
7print(w.numpy())

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.

python
1import tensorflow as tf
2
3w = tf.Variable(3.0)
4bias = 2.0
5
6with tf.GradientTape() as tape:
7    y = w * 5.0 + bias
8    loss = (y - 20.0) ** 2
9
10grad = tape.gradient(loss, w)
11print("gradient:", grad.numpy())

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.Tensor means an immutable computed value'
  • 'tf.Variable means 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.Tensor is an immutable TensorFlow value used in computation.'
  • 'tf.Variable is 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.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.