The right way to use AI
The right way to use AI
This morning I had a breakthrough regarding how to use AI for my Python learning goal. AI is a wonderful tool. I’ve been messing around with it since the release of ChatGPT 3. On the surface it is amazing because it has all this knowledge and can present it to you via prompts. Many people complain how AI will take their jobs away and that is a possible future state of AI and human relationships. I’d argue it’s not there yet. Besides the fact that our current AI implementations have a tendency to “hallucinate” they also don’t always respond with a correct answer. If you’re unfamiliar with a subject and AI presents you with an answer you’re well advised to double check that information for accuracy.
Now specifically with Python you can ask AI to write code for you which it will do and actually does a decent job at it. The problem is that the output may or may not be what you asked it to produce. Earlier in the year I was asking for AI to convert a table of numbers into a graph. The first challenge was getting AI to produce the table…accurately. Once I had the table correct I simply asked for a graph. AI returned a Python script. Not what I expected but none the less I was able to run the Python code and it did draw a graph but not the graph I wanted. Probably with some more tinkering with the prompt I might have been able to get what I wanted. Instead I used excel and manually created the graph I wanted.
Today’s session with AI was amazing. I ran across a Python puzzle in a Facebook user group. I decided to pick apart the code and attempt to answer the puzzle. I quickly ran into my limits as a novice. So I decided to check with Copilot about the code.
def my_func(a,b):
c=a.count(b)
my_list = list()
for i in a:
my_list.append(i//c)
return set(my_list)
a = [10,20,10,20,30,40,50]
b = 10
print(my_func(a,b))
I submitted the code to Copilot and asked what certain function calls were doing.
Certainly! Let’s break down the code snippet step by step:
- Function Definition:
- You’ve defined a function called
my_func
that takes two arguments:a
andb
.- Inside the function, you calculate the count of occurrences of
b
in the lista
and store it in the variablec
.- Creating an Empty List:
- You initialize an empty list called
my_list
.- Looping Through List
a
:
- The
for
loop iterates over each elementi
in the lista
.- For each element, you perform the operation
i // c
.i // c
:
- The expression
i // c
uses the floor division operator (//
).- Floor division divides two numbers and rounds down to the nearest integer.
- Specifically,
i // c
calculates the integer division ofi
byc
.- Returning a Set:
- After the loop, you convert the
my_list
to a set (usingset(my_list)
).- A set is an unordered collection of unique elements.
- The purpose of returning a set is to eliminate duplicate values.
- Example Input:
- You’ve provided the list
a = [10, 20, 10, 20, 30, 40, 50]
and the valueb = 10
.- Execution:
- Let’s calculate:
c
(count of occurrences ofb=10
ina
) is 2.- For each element
i
ina
:
i // c
:
- For
i=10
,10 // 2
equals 5.- For
i=20
,20 // 2
equals 10.- For
i=30
,30 // 2
equals 15.- For
i=40
,40 // 2
equals 20.- For
i=50
,50 // 2
equals 25.- The resulting set is
{5, 10, 15, 20, 25}
.- Output:
- When you print the result of
my_func(a, b)
, you’ll see the set{5, 10, 15, 20, 25}
.In summary, your function calculates the floor division of each element in the list
Copilot’s summary of the codea
by the count of occurrences ofb
. The resulting set contains unique integer values resulting from this operation.
For example (i // c). What on Earth is that double slash operator? Copilot explained that in an instant. It uses floor division where it divides two numbers down to the nearest integer and discards any remainder. Having AI break down the code in a step by step fashion was helpful to understand the context.
But that experience paled in comparison to the next conversation. In this case the code was using a module called random. I haven’t worked with random and again I quickly reached my capabilities so I posted the code to Copilot.
import random
def lottery():
# returns 6 numbers between 1 and 40
for i in range(6):
yield random.randint(1, 40)
# returns a 7th number between 1 and 15
yield random.randint(1, 15)
for random_number in lottery():
print("And the next number is... %d!" %(random_number))
I asked about the yield command. What is the yield command. Copilot went on to explain Iterables and Generators. I won’t bore you with the detailed response but it made me think about how this yield command is different from the return command and that’s were it got interesting. Here is the snippet of conversation:
Great observation! While both
yield
andreturn
are used within functions, they serve different purposes. Let’s explore their differences:
return
:
- Function Result: The primary purpose of
return
is to provide a result or value back to the caller.- Function Termination: When a
return
statement is encountered, the function immediately exits, and control returns to the calling code.- State Loss: Any local variables within the function are discarded after
return
.- Example:
def add(a, b): result = a + b return result
yield
:
- Generator Function:
yield
is used within a function to create a generator function.- Lazy Evaluation: Instead of computing all values upfront, a generator produces values on-the-fly as requested.
- Function State Preservation: When a generator function yields a value, it pauses its execution and retains its state (local variables).
- Example:
def lottery(): for i in range(6): yield random.randint(1, 40) yield random.randint(1, 15)
- Use Cases:
return
:
- Used for regular functions that compute a result and terminate.
- Suitable for functions with a fixed set of return values.
- Commonly used for functions that perform calculations, transformations, or data processing.
yield
:
- Used for creating generators that produce a sequence of values.
- Ideal for large datasets or infinite sequences.
- Enables lazy evaluation and memory efficiency.
- Summary:
- In short,
return
is about providing a final result, whileyield
is about producing values incrementally without losing function state.Remember:
- Use
return
for regular functions.- Use
yield
for generators and lazy evaluation. 🚀🐍
After reading that response that’s when I realized the true power of using Copilot to learn Python happens when I’m asking questions of it just like I would ask questions from a person trying to teach me. It is those moments that learning truly happens. I understand the difference at the same time I’m trying to apply the knowledge. So it’s not about AI writing the code for me, it’s about me understanding the code. I intend to use the ‘Why’ technique from this point out.