GeekHub Learn
Module
Lesson 5.11 of 8 in this module2 min read Module 5: Using AI APIs (OpenAI, Gemini, Anthropic)

Python for AI in 30 minutes

You do not need to be a Python developer to use LLM APIs. You need eight building blocks. This lesson is the shortest possible path to all eight.

Python for AI is like ordering food at a restaurant. You do not need to cook. You need to know the menu, point, and pay.

The eight building blocks you need:

  1. Variables and types: x = "hi", n = 5, flag = True
  2. Lists and dicts: [1,2,3], {"name": "Aman"}
  3. f-strings: f"Hello {name}"
  4. Functions: def greet(name): return f"Hi {name}"
  5. Conditionals: if/elif/else
  6. Loops: for x in items: ...
  7. Imports: from openai import OpenAI
  8. Environment variables: os.environ["OPENAI_API_KEY"]

That is it. Everything in this course uses just these.

# 1. Variables
name = "Aman"
age = 23

# 2. Dict (the most important Python structure for AI)
user = {"name": "Aman", "skills": ["python", "react"]}

# 3. f-string
msg = f"Hi {user['name']}, you know {len(user['skills'])} skills."

# 4. Function
def shout(text):
    return text.upper()

# 5. Conditional
if age >= 18:
    print("adult")
else:
    print("minor")

# 6. Loop
for skill in user["skills"]:
    print(skill)

# 7. Import
import os

# 8. Env var
api_key = os.environ.get("OPENAI_API_KEY", "not-set")

Visualize it

A "Python cheat card" infographic with the 8 blocks, each as a small code snippet.

Try it now

Open colab.research.google.com. Paste the code above into a cell. Run it. Edit each block. Re-run.

Hands-on lab

In Colab, write a function summarize_user(user_dict) that takes a dict like {"name": ..., "skills": [...]} and returns a one-sentence f-string. Run it on 3 sample dicts.

Try it now

What is the difference between a list and a dict?

Common mistakes

  • Confusing {} (dict) and [] (list)
  • Forgetting indentation matters in Python
  • Not using virtual environments

Debugging tip

If you see KeyError, you accessed a missing dict key. If IndexError, a missing list index. If NameError, a typo or missing import.

Challenge

Write a Python script that builds a messages list (as in Module 3) for a 3-turn conversation and prints it formatted.

Where this shows up

  • Every AI API call uses these building blocks
  • Data prep for LLM inputs
  • Quick scripts to test prompts

From the field

If you can write the 8 building blocks fluently, you can ship an AI app. Engineers waste months thinking they need "real Python" first. They do not.

Recap

Eight Python blocks. That is the prerequisite. You are ready.


Quick recall

3 prompts · think before you flip

Prompt 1 of 3

What is an f-string?

Quiz time

1 question · tap an answer to check it

  1. 1. `user["name"]` accesses

Finished lesson 5.1?

Mark complete to update your module progress and unlock the streak.

Loading