Day 2 - Jan 22 2026

Python Essentials for GenAI

1. The Core Data Structures

In GenAI engineering, we don't just use variables; we structure data to feed into models.

A. Lists (The Sequence)

  • What it is: An ordered collection of items.

  • GenAI Context: Lists are used to store Conversation History. An LLM needs to "remember" previous turns in a conversation, so we store messages in a list.

  • Code Snippet:

    # A list of conversation turns
    chat_history = [
        "User: Hello", 
        "AI: Hi there! How can I help?", 
        "User: Explain Quantum Computing"
    ]

B. Dictionaries (The Structure)

  • What it is: A collection of Key-Value pairs. This is the most critical structure for GenAI.

  • GenAI Context: LLM APIs (like OpenAI or DeepSeek) expect messages in a specific dictionary format, usually containing a role and content.

  • Code Snippet:

    Python

    # A single message object formatted for an LLM
    message = {
        "role": "user",
        "content": "Write a poem about Python."
    }

2. File Handling (Input/Output)

AI Agents need to read prompts from files and save their generated outputs. We avoid hard-coding text inside scripts.

  • The with Statement: We use with open(...) because it automatically closes the file even if errors occur. This is crucial for long-running AI agents to prevent memory leaks.

  • Modes:

    • 'r' (Read): To load prompt templates or configuration text.

    • 'w' (Write): To save logs or AI responses (overwrites existing).

    • 'a' (Append): To add new logs to an existing file without deleting history.

3. JSON: The Language of LLMs

JavaScript Object Notation (JSON) is the universal standard for data exchange. While we write in Python, LLMs speak JSON.

  • Why it matters: When you send a request to ChatGPT or Claude via code, you are sending a JSON payload. When the model replies, it sends back JSON.

  • Key Functions:

    • json.dumps(): Converts a Python Dictionary ➝ JSON String (for sending).

    • json.loads(): Converts a JSON String ➝ Python Dictionary (for parsing).

Tactical Example:

4. Modular Functions

Writing monolithic code (one huge block) is bad practice. In GenAI, we create Helper Functions to handle repetitive tasks.

  • Example Use Case: You might write a function called get_embedding(text) or save_to_vector_db(data). This keeps the main logic clean and readable.

Key Learnings:

  • Why JSON? Large Language Models (LLMs) interact best via JSON. Learning to parse JSON is critical for building AI Agents.

  • Data Structures: Used Lists to hold the collection of data and Dictionaries to represent individual data points (Key: Value).

  • File I/O: Implemented open() with with statements to safely read raw text and write structured data.

Status:

Last updated