At a basic level, a function can be defined without parameters
Example:
def drwalker(): print("The best professor!")drwalker()# The best professor!
Functions and parameters
Parameters are components of your function that can vary based on user input. Example:
def make_big(x):print(x.upper())make_big('abcdefg')# ABCDEFGmake_big('the quick brown fox jumped over the lazy dog')# THE QUICK BROWN FOX JUMPED OVER THE LAZY DOGx ='hijklmn'make_big(x)# HIJKLMN
The return statement
The output of functions can be assigned to variables if a return statement is provided
Example:
def add_five(x):return x +5y = add_five(10)y# 15
Python and whitespace
Python code is organized by indentation and whitespace
After function definitions, code should be indented with four spaces. In Colab (and other Python development environments), the Tab key represents four spaces, and it will indent your code automatically
Code that is not indented properly will cause an error
def add_six(x):return x +6 File "<stdin>", line 2return x +6^IndentationError: expected an indented block
Comments
Comments, preceded by a hash (#), can be included in your code but are not run
Commenting your code is useful for describing what you are doing, or keeping experimental/old code you don’t want to run during the development process
# The function 'divide_by_two' divides any number by two <-- a commentdef divide_by_two(number):return number /2# This didn't work - remember whitespace issues# def divide_by_two(number):# return number / 2
Docstrings
Docstrings can be used to describe what functions do
Docstrings are enclosed in triple double quotes (""" """) and are placed on the line following the function definition
def concat_numbers(num1, num2):""" Return a concatenated string from two numbers. Parameters: ----------- num1: The first number you'd like to concatenate num2: The second number you'd like to concatenate """returnstr(num1) +str(num2)
Ordering and named arguments
Arguments can be supplied to functions in two ways:
“Unnamed” in the order specified
“Named” in any order. Be careful, however, if you mix the two!
When a coding agent does something for you - reads a file, runs your code, searches the web - it is calling a function that someone wrote ahead of time
The agent doesn’t look inside that function. It picks it by reading the docstring: what it does, and what parameters it takes
Look back at concat_numbers(). A name, two parameters, a docstring. That is exactly what a “tool” looks like to an agent
So docstrings aren’t just for other people anymore. A clear description is how you tell an AI when - and how - to use your work
Iteration
Iteration
At your jobs, you will often need to repeat the same task over and over!
From your textbook: “Repeating identical or similar tasks without making errors is something that computers do well and people do poorly.”
Solution: make the computer do it with iteration
Loops
Loops are operators that tell the computer to repeat an action a given number of times
Common loops:
for: Repeats an action over a series of items
while: Repeats an action until a given condition is satisfied
Important note: loops, like functions, must obey whitespace rules!
The for loop
for repeats an action for every element in an object
Example:
for character in"Kyle": print("Give me a "+ character +"!")Give me a K!Give me a y!Give me a l!Give me a e!
How for works
So what is going on here?
The for loop looks at what it will iterate through - in this case it is a string, "Kyle"
We are referring to each element of our string, "Kyle", as character.
The loop evaluates the expression passed to the print function for each character in "Kyle" successively
So the for loop is equivalent to:
print("Give me a K!")print("Give me a y!")print("Give me a l!")print("Give me a e!")
The while loop
while repeats an action until a given condition is satisfied
Example:
i =5while i >0: print(str(i) +"...") i = i -1print("Blast-off!")5...4...3...2...1...Blast-off!
How while works
We define a counter, i, that we set to 5 before running the loop
While the value of i is greater than 0, we tell Python to evaluate the print function
With each run of the loop, we subtract 1 from i
When i is equal to 0, we exit the loop and print "Blast-off" to the console
Beware of the infinite while loop!
Loops and AI: the “agentic loop”
What does a coding agent actually do when you give it a task?
It tries something, looks at the result, decides what to do next, and tries again - until it thinks the job is done
Sound familiar? That’s a while loop. The industry term for it is the agentic loop
The blast-off loop stopped when i hit 0. Guaranteed, every time
The agent’s loop stops when the agent decides it is finished
Beware the infinite loop… and beware the loop that exits too early. Both look like a finished job
Conditional logic
However - what if we don’t want to do the same thing every time we run a loop? Or a function, for that matter?
Answer: conditional logic
Conditional logic
Conditional statements in Python: if, elif, and else
Conditional operators:
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Is equal to
!= Is not equal to
Booleans: True and False
Boolean operators: and, or, & not
Conditional logic in Python
Example:
mylist = [2, 4, 6, 8, 10, 12]for number in mylist: if number >7: print(str(number) +" is greater than 7!")else: print(str(number) +" is less than 7!")2is less than 7!4is less than 7!6is less than 7!8is greater than 7!10is greater than 7!12is greater than 7!
Conditional logic in functions
When writing functions, you’ll rely heavily on conditional statements
def is_even(x): if x %2==0: return(True)else: return(False)is_even(8)Trueis_even(99)False
List comprehensions
A compact way to build a list from a loop
# A for loop that builds a list:doubled = []for n in [1, 2, 3, 4]: doubled.append(n *2)doubled# [2, 4, 6, 8]
The same thing, in one line
doubled = [n *2for n in [1, 2, 3, 4]]doubled# [2, 4, 6, 8]
With a condition
evens = [n for n in [1, 2, 3, 4, 5, 6] if n %2==0]evens# [2, 4, 6]
Deterministic programming
Code gives the same answer every time
def longest(words): best = words[0]for w in words:iflen(w) >len(best): best = wreturn bestwords = ["data", "code", "byte", "loop"] # all four are 4 letterslongest(words)# 'data', every single time
Ask an AI the same question…
“Which of these is the longest word: data, code, byte, loop?”
Let’s try another one
"September"[:4] +"."
Ask AI to abbreviate September for you. What do you get?
The decimal example from last week
“What is two-thirds as a decimal?”
2/3
or:
round(2/3, 2)
Building with AI and agents
Important distinction in AI / coding workflows: deterministic layer vs. orchestration layer
The deterministic layer runs every time; the orchestration layer can exercise judgment