šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

TensorFlow Basics in Python

Learn about TensorFlow Basics in this comprehensive Python tutorial. Learn to initialize tensors, manage strictly typed data, and interact seamlessly with NumPy.

⚔ Total XP: 0|šŸ’» tensorflow XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core logic.

Quick Quiz //

What is the key difference between tf.constant() and tf.Variable()?


šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Listen up. If you're building deep learning models, understanding TensorFlow Basics in Python is non-negotiable. This is where graphs get compiled, gradients get computed, and raw data turns into intelligence.

1Tf basics Part 1

Every TensorFlow program starts with import tensorflow as tf — the tf alias is a near-universal convention across tutorials, official docs, and production code, so sticking to it keeps your code readable to anyone else who touches it.

Running print(tf.__version__) right after the import is a habit worth keeping, not just a sanity check. TensorFlow's API has shifted significantly between major versions — TensorFlow 1.x required you to build a static computation graph and run it inside a tf.Session, while TensorFlow 2.x defaults to eager execution, running operations immediately just like regular Python. Code written for one version can fail silently or behave differently on the other, so confirming the version up front avoids a lot of confusing debugging later.

Because TensorFlow 2.x is eager by default, the tensors you create behave like NumPy arrays you can inspect, print, and index immediately — there's no separate 'run the graph' step required to see a value, which is the biggest usability change from the TensorFlow 1.x era.

āœ•
—
+
import tensorflow as tf

print(tf.__version__)
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

2Tf basics Part 2

tf.constant() is how you create your first tensor — TensorFlow's equivalent of a NumPy ndarray or a PyTorch Tensor. Passing it a Python list, as in tf.constant([1, 2, 3, 4, 5]), wraps that data in a Tensor object with a defined shape and dtype, ready to be used in mathematical operations or fed into a model.

If you're coming from NumPy, the mental model transfers almost directly: a tensor is a typed, n-dimensional array, and TensorFlow's operations mirror NumPy's naming wherever it makes sense (tf.reshape, tf.reduce_sum, tf.matmul and so on). The difference is that tensors are designed to run on GPUs and TPUs and to participate in TensorFlow's automatic differentiation system, which plain NumPy arrays cannot do.

Printing a tensor, like print(x), shows you three things at a glance: its values, its shape, and its dtype — the three pieces of information you'll be checking constantly while debugging shape mismatches or type errors in a model.

āœ•
—
+
x = tf.constant([1, 2, 3, 4, 5])
print(x)
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

3Tf basics Part 3

The question of the moment: which function creates a basic, immutable tensor in TensorFlow? The answer is tf.constant(). It takes a Python list, tuple, NumPy array, or scalar and returns a Tensor whose values are fixed for its entire lifetime.

This is worth contrasting with tf.Variable(), which you'll meet later — variables are the mutable counterpart used to hold trainable weights that get updated during training. tf.constant is for values that shouldn't change: fixed input data, hyperparameters baked into the graph, or intermediate results you don't intend to reassign.

Getting this distinction right early pays off: reaching for tf.constant when you actually need a value to change during training (like a layer's weights) will cause TensorFlow to raise an error the moment you try to modify it — which is exactly the behavior we'll stress-test in the ADA Defense challenge later in this lesson.

āœ•
—
+
# Creating Tensors
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

4Tf basics Part 4

Every tensor carries a dtype — the data type of the elements it holds. Left unspecified, TensorFlow infers one from your input: a list of Python ints becomes tf.int32, a list of floats becomes tf.float32. In deep learning code, you almost always want to be explicit and force tf.float32, as in tf.constant([1.0, 2.0, 3.0], dtype=tf.float32).

The reason is precision. Neural network training relies on gradient descent, which repeatedly nudges weights by tiny amounts — sometimes on the order of 1e-5 or smaller. Integer tensors can't represent fractional updates at all, and lower-precision floats can round tiny gradients down to zero, silently stalling learning. float32 gives enough precision for these small updates while staying cheaper in memory and compute than float64.

Mixing dtypes is a common source of runtime errors: adding a float32 tensor to an int32 tensor, or feeding float64 data into a model built with float32 weights, raises an InvalidArgumentError rather than silently casting for you the way plain Python arithmetic would. TensorFlow is deliberately strict here to catch precision bugs before they corrupt a training run.

āœ•
—
+
# Specifying the datatype
x = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

5Tf basics Part 5

Why is tf.float32 the standard, rather than tf.float64 (higher precision) or tf.float16 (lower precision, less memory)? It's a balance. float32 gives enough decimal precision to represent the small weight updates gradient descent produces, while using half the memory and bandwidth of float64 — which matters enormously when a model has millions or billions of parameters that all need to fit in GPU memory and move across the memory bus on every training step.

float64 would technically be 'more accurate,' but the extra precision is largely wasted in deep learning — the noise introduced by mini-batch sampling and stochastic optimization already dwarfs the rounding error float32 introduces. float16 and bfloat16, on the other hand, are used deliberately in 'mixed precision' training to speed things up further, but only for parts of the computation that can tolerate the reduced precision, with float32 still used to accumulate gradients safely.

So float32 isn't the default because it's the fastest or the most precise option in isolation — it's the option that best balances both concerns for the majority of models, which is why frameworks default to it and why most tutorials never mention changing it.

āœ•
—
+
# Data Types
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

6Tf basics Part 6

TensorFlow tensors and NumPy arrays interoperate closely. Calling .numpy() on any eager tensor — as in np_array = x.numpy() — returns a standard NumPy array holding a copy of the tensor's data, which you can then pass to any NumPy-based library like Matplotlib, scikit-learn, or Pandas.

This works in the other direction too: you can pass a NumPy array directly into tf.constant(), or into most TensorFlow operations, without an explicit conversion step. TensorFlow handles the conversion for you internally, treating array-like objects — Python lists, NumPy arrays, even Pandas Series — as valid tensor inputs almost everywhere.

The convenience comes with a performance caveat worth knowing: if the tensor lives on a GPU, calling .numpy() forces a device-to-host memory copy, moving the data from GPU memory back to the CPU's regular RAM. That's a genuinely slow operation compared to GPU-resident math, so pulling values out with .numpy() inside a hot training loop — for logging or debugging, say — can quietly become a performance bottleneck.

āœ•
—
+
# Convert Tensor to NumPy array
np_array = x.numpy()
print(type(np_array))
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

7Tf basics Part 7

To convert a TensorFlow tensor into a standard NumPy array, call the .numpy() method directly on the tensor object: x.numpy(). There's no separate top-level function like tf.to_numpy() — the conversion is a method on the Tensor object itself, available on any tensor produced in eager mode.

This only works for eager tensors — tensors you can inspect immediately. Inside a @tf.function-decorated graph function, tensors are symbolic placeholders in a compiled graph rather than concrete values sitting in memory, so .numpy() isn't available there; you'd need to work with tf.print() or return the value from the graph function instead.

Knowing this one method unlocks a lot: it's how you hand tensor output off to matplotlib.pyplot.plot() for a training curve, feed predictions into a sklearn metric function, or just print a readable array instead of TensorFlow's more verbose Tensor(...) representation during debugging.

āœ•
—
+
# NumPy Integration
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

8Tf basics Part 8

Before the ADA Defense challenge, let's make sure the ground is solid: everything covered so far — the tf import, tf.constant(), dtypes, and NumPy interoperability — hinges on one property of tf.constant tensors that's easy to gloss over: they are immutable. Once created, the values inside a tf.constant tensor cannot be changed in place.

This isn't an arbitrary restriction. TensorFlow's graph compiler and its distributed execution system rely on being able to reason about tensors without worrying that some other part of the program silently mutated their contents mid-computation. Immutability makes tensors safe to pass around, cache, and optimize.

The challenge ahead will test exactly this: what actually happens when you try to assign a new value into a tf.constant the way you'd assign into a Python list.

āœ•
—
+
# SYSTEM WARNING:
# ADA Protocol initiating...
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

9Tf basics Part 9

A tf.constant is exactly what its name says: constant. Once you create one — x = tf.constant([1, 2, 3]) — there's no operation that changes the values stored inside that specific tensor object. Any operation that looks like it's 'modifying' a tensor, such as x + 1, actually creates and returns a brand-new tensor, leaving the original untouched.

This is a real departure from how Python lists behave, where my_list[0] = 5 mutates the list in place. It's also why tf.constant is the wrong tool for anything that needs to change over time — most importantly, a neural network's trainable weights, which are updated on every training step by the optimizer.

For values that do need to change, TensorFlow provides tf.Variable, which wraps a tensor but adds an explicit .assign() method for in-place updates. Keras layers use tf.Variable internally for every weight and bias; tf.constant is reserved for fixed inputs, labels, and hyperparameters that never change once the graph is built.

āœ•
—
+
# ADA initializing immutability checks...
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

10Tf basics Part 10

So what actually happens if you run x = tf.constant([1, 2, 3]) and then try x[0] = 5? TensorFlow immediately raises a TypeError, because tf.constant tensors do not support item assignment at all — there's no code path in the library for mutating a constant tensor in place, unlike a Python list where list[0] = 5 is perfectly normal.

This is a deliberate design choice, not a missing feature. If item assignment were allowed, any function that received a tensor as an argument could silently corrupt data the caller still expected to be intact elsewhere in the program — a class of bug that's notoriously hard to track down.

If your actual intent is to update a value, the fix is either to build a new tensor with the change applied (e.g., using tf.tensor_scatter_nd_update), or to use a tf.Variable in the first place and call .assign() on it — .assign() is explicit about intent, so it's much harder to trigger by accident.

āœ•
—
+
# DEFEND THE SYSTEM
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

11Tf basics Part 11

Immutability confirmed, dtypes locked in, and the bridge to NumPy understood — that's the full foundation TensorFlow's tensor system is built on. Every more advanced topic in this course, from tf.Variable weight updates to GradientTape-based custom training loops, assumes you're comfortable with these basics: create tensors with tf.constant(), keep dtypes explicit and consistent, and remember that operations return new tensors rather than mutating existing ones.

The next piece of the puzzle is tensor shape and dimensionality — how TensorFlow represents scalars, vectors, matrices, and higher-dimensional batches of data, and how mismatched shapes become the single most common source of runtime errors once you start building real models.

Getting shapes right is where the payoff of this lesson shows up: once you can reliably reason about what shape a tensor should have at each step of a model, debugging becomes dramatically faster.

āœ•
—
+
print("System secured.\
Constants locked.")
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Graph compiled successfully.

12Step-by-Step Breakdown

To use TensorFlow, you first need to import it. The global standard alias is tf.

Like NumPy and PyTorch, TensorFlow operates on Tensors. Let's create our first constant tensor.

Which function is used to create a basic, immutable tensor in TensorFlow?

  • →tf.tensor()
  • →tf.constant()
  • →tf.array()

Tensors have a specific data type (dtype). In Deep Learning, you almost always want 32-bit floats (tf.float32), not integers.

Why is tf.float32 the absolute standard data type for Neural Network weights in TensorFlow?

  • →It provides the perfect balance between high mathematical precision (for tiny gradient updates) and GPU memory efficiency.
  • →Because TensorFlow cannot calculate math with integers.
  • →Because it uses zero GPU memory.

Unlike PyTorch, TensorFlow integrates incredibly deeply with NumPy. You can convert a Tensor back to a NumPy array instantly.

How do you convert a TensorFlow object into a standard NumPy array?

  • →By calling tf.to_numpy().
  • →By calling the .numpy() method on the tensor object.
  • →You cannot convert a tensor to NumPy.

Now, prepare yourself. We are about to enter the ADA Defense Protocol. Ensure you understand tensor immutability.

A tf.constant is exactly what its name implies: constant. Once created in memory, its values can never be altered.

ADA DEFENSE: You write x = tf.constant([1, 2, 3]) and then try to change the first value by running x[0] = 5. What happens?

  • →The tensor becomes [5, 2, 3].
  • →TensorFlow immediately throws a TypeError because tf.constant tensors are strictly immutable and do not support item assignment.
  • →The tensor is deleted from memory.

Threat neutralized. Immutability confirmed. Proceeding to Tensor Shapes and Dimensions.

Build a Real float32 Array. Finish make_float32_array(): tf.float32 is the standard precision for neural network weights.

Level Up šŸš€

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Explicit Dtype Declarations

Writing `dtype=tf.float32` explicitly in every `tf.constant` call, rather than relying on type inference, makes it obvious to a reviewer (or a future you) exactly what precision a tensor was built with, without having to trace the value back through the code.

# Prefer: x = tf.constant([1.0, 2.0], dtype=tf.float32) # Over: x = tf.constant([1, 2]) # dtype silently inferred as int32

SEO Implications

  • 1

    High-Intent Beginner Search Queries

    Searches like 'tf.constant vs tf.Variable' and 'tensorflow float32 vs float64' are common early-stage queries from developers moving from NumPy or PyTorch into TensorFlow, making precise, example-driven coverage of these basics valuable for organic discovery.

Best Practices

Set dtype Explicitly for Model Inputs

Don't rely on TensorFlow's type inference for anything that will flow into a model — pass dtype=tf.float32 explicitly so a stray integer literal doesn't silently produce an int32 tensor that later breaks a matmul.

Reach for tf.Variable Only When a Value Must Change

Default to tf.constant for fixed data and use tf.Variable only for values the optimizer needs to update — treating everything as a Variable makes it harder to reason about what's actually trainable in a model.

Frequent Bugs

THE BUG

Passing a Python list of mixed types (or plain ints) into a model that expects float32, producing an int32 tensor that fails or silently misbehaves in later matrix operations.

THE FIX

Always pass dtype=tf.float32 explicitly when constructing input tensors for a model, instead of relying on TensorFlow's automatic type inference.

Real-World Examples

Debugging a Dtype Mismatch

A custom training loop feeds float64 NumPy data (the default for np.array with float literals) into a model whose weights were built as float32, and a matmul call raises InvalidArgumentError: cannot compute MatMul as input #1 was expected to be a float tensor but is a double tensor.

# Wrong: dtype mismatch
x = np.array([[1.0, 2.0]])  # float64 by default
model(x)  # InvalidArgumentError

# Correct: cast explicitly
x = tf.constant(x, dtype=tf.float32)
model(x)

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Mixing tensor dtypes in an operation (e.g., adding a float32 tensor to an int32 tensor)

# Wrong: raises InvalidArgumentError a = tf.constant([1, 2], dtype=tf.int32) b = tf.constant([1.5, 2.5], dtype=tf.float32) result = a + b # Correct: cast explicitly first result = tf.cast(a, tf.float32) + b

The Solution //

TensorFlow does not implicitly cast between dtypes the way Python arithmetic does. Operations between tensors of different types raise an InvalidArgumentError. Cast explicitly with tf.cast() before combining them.

The Error //

Trying to mutate a tf.constant tensor with item assignment

# Wrong: raises TypeError x = tf.constant([1, 2, 3]) x[0] = 5 # Correct: use a Variable x = tf.Variable([1, 2, 3]) x[0].assign(5)

The Solution //

tf.constant tensors do not support item assignment at all -- x[0] = 5 raises a TypeError. If a value truly needs to change over time (like a model weight), use tf.Variable and its .assign() method instead.

Lesson Glossary

[01]tf.constant

Creates a constant tensor from a tensor-like object (e.g., a Python list or NumPy array). Its values cannot be changed.

Code Preview
// tf.constant context

[02]dtype

The data type of the elements in a tensor, such as tf.float32, tf.int32, or tf.string.

Code Preview
// dtype context

Continue Learning