AlgoMaster Logo

Python Essentials for AI

7 min readUpdated June 22, 2026
Listen to this chapter
Unlock Audio

Python is the working language for much of AI engineering. You will use it for data preparation, evaluation scripts, model-serving code, RAG pipelines, and the small tools that connect systems together.

This chapter covers the Python features that show up repeatedly in AI codebases: collections, comprehensions, unpacking, formatting, slicing, built-in iteration tools, text handling, and a few everyday idioms. The goal is not to memorize every Python feature. It is to build habits that keep your code clear when you are working with real data and external APIs.

Choosing the Right Data Structure

Python's core collection types are simple, but choosing the right one matters. A good choice means fewer conversions, fewer hidden assumptions about ordering, and fewer bugs around mutation.

Here is how they compare:

Scroll
StructureOrdered?Mutable?Duplicates?AI Use Case
listYesYesYesStoring embeddings, token sequences, batch results
dictYes (3.7+)YesKeys: NoModel configs, API responses, token-to-id mappings
setNoYesNoVocabulary deduplication, stopword filtering
tupleYesNoYesImmutable coordinates, function return values, dict keys

Here is how each one commonly appears in AI projects.

Lists: Ordered Sequence

Lists are ordered, mutable sequences. In AI code, lists hold everything from raw token sequences to batches of embeddings.

main.py
Loading...

Dicts: Configuration and Mapping

Dicts are key-value stores. They are everywhere in AI code: model settings, API payloads, token vocabularies, lookup tables, and parsed JSON responses.

main.py
Loading...

Sets: Deduplication and Fast Lookup

Sets are unordered collections of unique elements. Average-case membership checks are constant time, which matters when you filter thousands of tokens or document IDs.

main.py
Loading...

If you need both uniqueness and order, a common pattern is list(dict.fromkeys(items)). This preserves insertion order while removing duplicates.

Tuples: Immutable and Hashable

Tuples are like lists but immutable. You cannot change them after creation. This makes them useful as dict keys (lists cannot be dict keys because they are mutable) and as return values from functions.

main.py
Loading...

Returning multiple values and unpacking them at the call site is routine Python. Use it for small, tightly related results. Once the return value has more structure or survives beyond a few lines, prefer a dataclass or Pydantic model with named fields.

Comprehensions: Data Processing in One Line

Comprehensions let you transform or filter a collection in a single expression. In AI work, they are common in preprocessing, feature extraction, evaluation summaries, and batch assembly.

List Comprehensions

The basic pattern is [expression for item in iterable if condition].

main.py
Loading...

Dict Comprehensions

Same idea, but the result is a dictionary. This is useful for building lookup tables and inverting mappings.

main.py
Loading...

Set Comprehensions

Set comprehensions are useful when you want unique values from a collection.

main.py
Loading...

Do not turn comprehensions into puzzles. A single filter or transform is usually clear. A double-nested comprehension can be fine when it reads naturally. If you need three levels, side effects, or exception handling, use a regular loop. Preprocessing code should be easy to review.

Tuple Unpacking and Multiple Returns

Unpacking appears everywhere in Python AI code: evaluation loops, dataset iteration, metric returns, and batch processing.

Basic Unpacking

main.py
Loading...

Star Unpacking

The * operator captures "the rest" into a list.

main.py
Loading...

Functions Returning Multiple Values

Python functions commonly return tuples, and callers unpack them directly. This pattern is common in training loops, evaluation code, and small utility functions.

main.py
Loading...

F-Strings: Clean Formatting

F-strings, short for formatted string literals, are the standard way to interpolate values into strings in modern Python.

main.py
Loading...

Older approaches like % formatting and .format() still work and appear in older code. For new application code, f-strings are usually clearer and easier to scan.

Slicing: Working with Sequences

Slicing extracts portions of lists, strings, arrays, and tensors. The syntax is sequence[start:stop:step], where start is inclusive and stop is exclusive.

main.py
Loading...

Why Slicing Matters for AI

When you work with NumPy arrays and PyTorch tensors, slicing becomes essential. The same basic syntax carries over:

main.py
Loading...

You do not need to fully understand NumPy yet. The slicing syntax you learn here transfers directly to the numerical computing libraries you will use later.

String Slicing

Strings support the same slicing syntax. This is useful for truncating prompts, extracting prefixes, or working with fixed-format text.

main.py
Loading...

The Walrus Operator :=

The walrus operator (:=), introduced in Python 3.8, assigns a value as part of an expression.

Use it sparingly. It is helpful when it removes duplicated work without making the condition harder to read.

main.py
Loading...

Without :=, the last example would need to compute the score before the if and then use it again inside the block. That is fine too. Use the walrus operator only when it improves readability.

Essential Python Idioms

Python has built-in functions that replace manual indexing and repetitive loops. These idioms make data-processing code shorter without hiding the work.

enumerate: Loop with Index

Instead of maintaining a separate counter variable, use enumerate:

main.py
Loading...

zip: Iterate in Parallel

zip pairs up elements from two or more iterables.

main.py
Loading...

any and all: Bulk Boolean Checks

These short-circuit through an iterable and return a single boolean. Think of any as "does at least one item satisfy this?" and all as "do all items satisfy this?"

main.py
Loading...

sorted with key: Custom Sorting

The key parameter lets you sort by derived values without changing the data itself.

main.py
Loading...

dict.get with Defaults

Using .get() with a default value is the standard pattern when a missing key has a legitimate fallback.

main.py
Loading...

String Methods for NLP and Text Processing

When you work with text data, you will lean on a small set of string methods. They are useful for preprocessing inputs, parsing outputs, and preparing prompts.

split and join: Tokenization Basics

split breaks a string into a list. join does the reverse. They are not a replacement for a model tokenizer, but they are useful for basic text processing.

main.py
Loading...

strip, replace, lower: Cleaning Text

Messy text is the norm in real data. These methods handle the most common cleaning tasks.

main.py
Loading...

startswith and endswith: Pattern Matching

These are cleaner than slicing for checking prefixes and suffixes, and they accept tuples for checking multiple patterns at once.

main.py
Loading...

Ternary Expressions

Python's ternary expression is value_if_true if condition else value_if_false. It is useful for simple value selection.

main.py
Loading...

Keep ternary expressions short. If the logic is more complex, use a regular if/else block. Nested ternaries are legal, but they are usually harder to read than they are worth.

None Checks and Truthiness

Python's None represents an explicit missing value. Python also has a broader concept of "truthiness", which you need to understand to avoid subtle bugs.

What is Falsy in Python?

These values all evaluate to False in a boolean context:

Scroll
ValueTypeNote
NoneNoneTypeThe explicit "nothing" value
FalseboolThe boolean false value
0intZero is falsy
0.0floatZero float is falsy
""strEmpty string is falsy
[]listEmpty list is falsy
{}dictEmpty dict is falsy
set()setEmpty set is falsy

Most other values are truthy. This is convenient for empty-collection checks, but it can misfire when 0, "", or [] are valid inputs.

The Difference Between is None and Truthiness

main.py
Loading...

The Common Pattern: Default Mutable Arguments

This is a common Python mistake. Do not use a mutable default argument:

main.py
Loading...

The problem with the first version is that the default [] is created once when the function is defined, not each time it is called. Every call without an explicit collection argument shares and mutates the same list. This comes up often in AI code when you are accumulating documents, messages, examples, or results.

Summary

Key takeaways:

  • Lists are your default collection for ordered, mutable data. Use them for token sequences, embeddings, and batch results.
  • Dicts map keys to values. Use them for model configs, vocabularies, and API payloads. Use .get() when a missing key has a real fallback.
  • Sets provide O(1) membership testing. Use them for stopword filtering and deduplication.
  • Tuples are immutable ordered values. Use them for small multi-value returns and dict keys.
  • Comprehensions (list, dict, set) are a concise way to transform and filter data when the logic stays simple.
  • Tuple unpacking lets you assign multiple values at once. Combined with enumerate and zip, it eliminates most index-based loops.
  • F-strings handle all string formatting. Use format specifiers like :.2f for floats and :.1% for percentages.
  • Slicing with [start:stop:step] works on lists, strings, and (later) arrays and tensors. The same syntax transfers to NumPy and PyTorch.
  • Python idioms like enumerate, zip, any/all, sorted(key=...), and dict.get() reduce boilerplate in everyday data-processing code.
  • None checks should use is None, not truthiness, when 0, "", or [] are valid values. And never use mutable default arguments.

Quiz

Python Essentials for AI Quiz

10 quizzes

References