Python AI Agents
Python Basics
Storing and Naming Information
Think of a variable as a labeled box where you can store a piece of information. You give the box a name, and you can put something inside it. Later, you can look inside the box by using its name, or even replace what's inside with something new.
# We create a variable named 'player_score' and store the number 100 in it.
player_score = 100
# We can print the value by using the variable's name.
print(player_score)
# We can also update the value.
player_score = 150
print(player_score)
The kind of information you store determines its data type. Python has several basic types, but a few are essential to get started.
| Data Type | Description | Example |
|---|---|---|
| Integer | Whole numbers | 10, -5, 0 |
| Float | Numbers with decimals | 98.6, 3.14, -0.5 |
| String | Text, enclosed in quotes | "Hello, world!", 'Python' |
| Boolean | Represents truth values | True, False |
Choosing clear, descriptive names for your variables makes your code much easier to understand.
player_scoreis better thanps.
Making Decisions and Repeating Actions
Programs often need to make decisions. Just like you check the weather before deciding what to wear, a program can check a condition and run different code based on the outcome. This is done with control structures.
The if statement is the most basic decision-making tool. It checks if a condition is True. If it is, a specific block of code runs. You can add elif (else if) for more conditions and else for a default action if none of the conditions are met.
temperature = 75
if temperature > 80:
print("It's a hot day!")
elif temperature < 60:
print("Better bring a jacket.")
else:
print("The weather is perfect.")
Sometimes you need to repeat an action. Instead of writing the same code over and over, you use a loop.
A for loop repeats a block of code for each item in a sequence. A while loop repeats as long as a certain condition remains true.
# A for loop that prints numbers 0 through 4
for number in range(5):
print(number)
# A while loop that counts down from 3
countdown = 3
while countdown > 0:
print(countdown)
countdown = countdown - 1
print("Liftoff!")
Creating Reusable Code Blocks
If you find yourself writing the same piece of code in multiple places, it's a good idea to package it into a function. A function is a named, reusable block of code that performs a specific task. Think of it like a recipe: you define the steps once, and you can use it to make the same dish whenever you want.
Defining a function uses the def keyword, followed by the function's name and parentheses. Any information the function needs to do its job, called parameters, goes inside the parentheses.
# This function takes a name and prints a greeting.
def greet(name):
print(f"Hello, {name}!")
# Now we can 'call' the function to use it.
greet("Alice")
greet("Bob")
Functions can also perform a calculation and send a result back. This is done with the return keyword.
def add_numbers(x, y):
return x + y
# Call the function and store the result in a variable
sum_result = add_numbers(5, 3)
print(sum_result) # This will print 8
Blueprints for Creating Things
Object-Oriented Programming, or OOP, is a way of thinking about and organizing code. The central idea is to bundle data and the functions that work on that data together into "objects."
To create objects, you first define a class, which is like a blueprint. A class describes the properties (data) and behaviors (functions) that all objects of that type will have.
For example, we could create a Dog class. The blueprint would state that every dog has a name and a breed (properties) and can bark (a behavior).
class Dog:
# This is the 'initializer' method. It runs when a new object is created.
def __init__(self, name, breed):
self.name = name
self.breed = breed
# This is a method (a function inside a class).
def bark(self):
print(f"{self.name} says: Woof!")
# Now we create 'instances' of the Dog class. These are our objects.
my_dog = Dog("Fido", "Golden Retriever")
another_dog = Dog("Lucy", "Poodle")
# We can access their properties and call their methods.
print(my_dog.name) # Prints "Fido"
another_dog.bark() # Prints "Lucy says: Woof!"
This approach helps organize complex programs by modeling real-world things. For an AI agent, you might have classes for Agent, Memory, or Tool, each with its own data and behaviors.
Now, let's test your understanding of these core concepts.
In programming, what is the primary role of a variable?
Consider the following code snippet:
temperature = 20
if temperature > 25:
print("It's hot.")
elif temperature > 15:
print("It's pleasant.")
else:
print("It's cold.")
What will be printed?
With these fundamentals—variables, control structures, functions, and classes—you have the essential building blocks for writing Python programs and, eventually, sophisticated AI agents.
