How to Create a Tensor in TensorFlow with tf.constant
A tensor is TensorFlow's grid of numbers: a single number, a row of numbers, a table or a grid with more sides. tf.constant turns ordinary Python values into one. This guide creates four tensors with it in TensorFlow 2.22.0, and reads what TensorFlow prints about each.
Syntax
tf.constant(<value>, dtype=<data type>)
| Part | What it means |
|---|---|
tf | TensorFlow, imported with import tensorflow as tf |
<value> | A number, a list, or a list of lists of the same length |
dtype=<data type> | Optional. The type of every value in the tensor, such as tf.float32. Leave it out and TensorFlow picks one from the values |
The tf.constant API page says what it does, "Creates a constant tensor from a tensor-like object", and how the type is chosen: "If the argument dtype is not specified, then the type is inferred from the type of value."
The Example
Four tensors about the weather and a game: one temperature, a week of temperatures, the scores of two players in three games, and three counts stored as decimal numbers.
Prerequisites
- A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.
Step 1: Write the Script
import tensorflow as tf
temperature = tf.constant(21.5)
week = tf.constant([21.5, 23.0, 19.8, 22.1])
scores = tf.constant([[7, 9, 8], [6, 10, 9]])
counts = tf.constant([1, 2, 3], dtype=tf.float32)
print(temperature)
print(week)
print(scores)
print(counts)

Each call turns one Python value into a tensor. The list of lists becomes a table with two rows, one per player. dtype=tf.float32 asks for decimal numbers even though 1, 2, 3 are whole numbers.
Step 2: Run It
uv run python make_tensors.py
tf.Tensor(21.5, shape=(), dtype=float32)
tf.Tensor([21.5 23. 19.8 22.1], shape=(4,), dtype=float32)
tf.Tensor(
[[ 7 9 8]
[ 6 10 9]], shape=(2, 3), dtype=int32)
tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32)

Each tensor prints its values, its shape and its dtype:
| Tensor | shape | dtype | Why |
|---|---|---|---|
temperature | () | float32 | A single number has no sides, so its shape is empty |
week | (4,) | float32 | One row of 4 values |
scores | (2, 3) | int32 | 2 rows of 3 values each, all whole numbers |
counts | (3,) | float32 | dtype asked for it, so 1, 2, 3 became 1. 2. 3. |
The tensor guide explains the two types TensorFlow picked by itself: "TensorFlow converts Python integers to tf.int32 and Python floating point numbers to tf.float32". The command works unchanged in PowerShell.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Rows of different lengths: tf.constant([[1, 2, 3], [4, 5]]) | ValueError: Can't convert non-rectangular Python sequence to Tensor. A tensor is a full grid, so every row needs the same number of values | Give every row the same length |
| Expecting to change a value later | A tensor made with tf.constant can't be changed: the tensor guide says "All tensors are immutable like Python numbers and strings" | Build a new tensor from the old one |
FAQ
Is a tensor the same as a NumPy array?
Close. The tensor guide says: "If you're familiar with NumPy, tensors are (kind of) like np.arrays." Both hold a grid of values of one type, and a tensor's .numpy() gives you the same values as a NumPy array.
Why does print show tf.Tensor(...) and not just the numbers?
That's how a tensor describes itself: its values, then its shape and its data type, so one print tells you all three.
Conclusion
tf.constant turned a number, a list and a list of lists into tensors with the shapes (), (4,) and (2, 3). TensorFlow picked float32 for decimals and int32 for whole numbers, and dtype overrode that choice.
Additional Resources
- tf.constant — every argument.
- Introduction to tensors — TensorFlow's guide to tensors.
