AI-Guided Tutorial: Python Functions

Learn about Python functions through interactive AI guidance. This hands-on approach will help you understand how to create, use, and optimize functions in Python.

Learning Objectives

By the end of this tutorial, you should understand:

Tutorial Instructions

Part 1: Understanding Functions

  1. Open VS Code and create a new Python file called functions_tutorial.py
  2. Open GitHub Copilot Chat and ask:
    What are functions in Python and why are they useful? Show me a simple example of defining and calling a function.
    

Part 2: Creating Your First Functions

Ask Copilot Chat:

Show me how to create Python functions with different types of parameters: no parameters, one parameter, multiple parameters, and default parameters. Include examples I can try.

Practice Task: Create 4 different functions based on Copilot’s examples.

Part 3: Return Values and Function Output

Ask Copilot Chat:

How do return values work in Python functions? Show me examples of functions that return different types of data (numbers, strings, lists, boolean values).

Practice Task: Write 3 functions that return different types of values.

Part 4: Function Scope and Variables

Ask Copilot Chat:

Explain variable scope in Python functions. What's the difference between local and global variables? Show me examples with potential issues and how to avoid them.

Practice Task: Create examples that demonstrate local vs global scope.

Part 5: Practical Function Examples

Ask Copilot Chat:

Show me practical examples of Python functions that solve real problems: calculating areas, processing strings, working with lists, and mathematical calculations.

Practice Task: Implement at least 3 practical functions suggested by Copilot.

Part 6: Function Best Practices

Ask Copilot Chat:

What are the best practices for writing good Python functions? Include naming conventions, documentation, and code organization tips.

Assessment Challenge

Create a Python file with:

Reflection Questions

Ask Copilot Chat to help you understand:

  1. “When should I create a function vs writing code directly?”
  2. “How do I know what parameters my function should take?”
  3. “What makes a function ‘good’ vs ‘bad’?”

Take a look at the Functions lab and make sure you are prepared to work on this in class.