Week 02
Augment, don’t automate!
The big three: Gemini, ChatGPT, and Claude (These are general-purpose AI models that can help with programming.)
Four ingredients of a great prompt:
"Explain this Python dictionary comprehension step by step:
{word: len(word) for word in text.split() if len(word) > 3}"
"Write a Numpy-style docstring for this function that calculates the area of a circle given its radius."
"Convert this for loop into a more Pythonic equivalent using list comprehension or built-in functions."
"I'm getting a 'TypeError: unsupported operand type(s)' on line 15. Here's my code... What's the likely cause and how do I fix it?"
"I need to store student information (name, ID, grades). Should I use a list of dictionaries or create a Student class? Explain the pros and cons of each approach."
Our challenge: Create a Python function that takes a filename and returns the number of lines in the file, handling potential FileNotFoundError
exceptions.
Let’s give the exact same prompt to all three AIs:
“Write a Python function called
count_lines
that takes a filename as a parameter and returns the number of lines in the file. Handle the case where the file doesn’t exist by returning 0. Include a NumPy-style docstring.”
"""
), that occurs as the first statement in a module, function, class, or method definitionAn example of Numpy-style docstrings:
Why integration matters 🔗
How it works:
Best practices:
Clear comments are key:
# Calculate the factorial of a positive integer
Descriptive function names:
calculate_factorial
not calc_fact
Type hints help:
def factorial(n: int) -> int:
Your conversational partner 💬
Three powerful modes:
Your conversational partner 💬
When to use each mode:
Building a Python utility with AI assistance:
# A function that calculates compound interest
Why use terminal-based AI?
Common usage patterns:
# Start an interactive gemini session
gemini
# Quick code questions
gemini "How do I install [pytest](https://pytest.org/) in [Python](https://www.python.org/)?"
# Code generation
gemini "Write a [Rust](https://www.rust-lang.org/) function to read a CSV file"
# Debugging help
gemini "Explain this error: ImportError: No module named 'requests'"
What is it?
Why mention it?
AI-powered code review 🔍
Revolutionary approach:
AI-powered code review 🔍
Good for:
Required for this course (free):
Recommended for this course (also free):
Daily development:
Code review and submission:
/tests
commandThe golden rule: Never submit code you don’t understand!
labs/lab02/README.md
labs/lab02/README.md
in the course repository for step-by-step instructionsIS4010: App Development with AI