Everything so far in this section used AI through a chat interface or a website. This lesson is the entry point into using AI programmatically — from your own code — starting with the language nearly all of it is built in.
Google Colab (colab.research.google.com) gives you a ready-to-use Python environment in your browser — no installation, common AI libraries already available, and free access to more computing power than a typical laptop provides. All you need is a Google account, which makes it the easiest way to start writing and running real Python today.
If you already know a web language, most of Python's syntax maps directly onto ideas you already have — lists work like arrays, dictionaries work like objects or associative arrays, and indentation replaces curly braces:
# Variables — no var/let/const, just assign
name = "Riya"
age = 25
# Print (like console.log)
print(f"Hello, {name}! You are {age} years old.")
# Lists (like arrays)
courses = ["Graphic Design", "Web Dev", "AI for Beginners"]
courses.append("Python")
print(courses[0]) # "Graphic Design"
# Dictionaries (like objects)
student = {"name": "Anit", "age": 22, "courses": ["Web Dev", "AI"]}
print(student["name"]) # "Anit"
# Functions
def greet(person_name):
return f"Hello, {person_name}!"
print(greet("Priya")) # "Hello, Priya!"
# Loop
for course in courses:
print(f"- {course}")
# Conditional
if age >= 18:
print("Adult")
else:
print("Minor")This is the moment Python and AI actually connect. After installing the relevant library, calling a real AI model is genuinely this short:
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY_HERE")
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is artificial intelligence?"}
]
)
print(response.choices[0].message.content)This Is the Whole Trick
This is what every AI-powered app — the chat interfaces you already use included — is doing underneath: calling an API with a message, and getting text back. There is no more hidden machinery than this.
Extending the single call above into a real back-and-forth conversation just means keeping a running list of messages and sending the whole list back each time:
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY_HERE")
messages = [
{"role": "system", "content": "You are a friendly course advisor. Ask clarifying questions before recommending a course."}
]
print("Type 'quit' to exit\n")
while True:
user_input = input("You: ").strip()
if user_input.lower() == "quit":
break
messages.append({"role": "user", "content": user_input})
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
max_tokens=200
)
bot_reply = response.choices[0].message.content
messages.append({"role": "assistant", "content": bot_reply})
print(f"\nAdvisor: {bot_reply}\n")The "memory" isn't magic — it's just a growing list, sent in full with every request, exactly the same pattern the chatbot website lesson used in JavaScript.
You can now call an AI model from your own code. The next lesson builds on this directly — connecting AI to real data and automating multi-step tasks, rather than just chatting.