Week 2: Functions & Modules¶
Welcome to Week 2! This week focuses on writing reusable code through functions and organizing code with modules.
Overview¶
Building on Week 1's foundation, you'll learn how to write more organized and reusable code. By the end of this week, you'll understand:
- Iterators and generators for efficient data processing
- Creating and using functions
- Variable scope and namespaces
- Organizing code into modules
- Code reusability and best practices
Lessons¶
Work through these lessons in order:
- Iterators & Generators - Efficient iteration patterns
- Functions - Creating reusable code blocks
- Scope of Variables - Understanding variable visibility
- Modules - Organizing and importing code
Homework Assignment¶
Rock, Paper, Scissors Game - Build an interactive game that demonstrates your understanding of functions, decision making, and user interaction.
Check the HW folder for detailed requirements and starter code.
Learning Objectives¶
By the end of Week 2, you should be able to:
- Create and use functions with parameters and return values
- Understand local vs global scope
- Use iterators and generators for memory-efficient code
- Organize code into reusable modules
- Import and use both built-in and custom modules
- Write cleaner, more maintainable code
Estimated Time¶
- Lessons: 1.5-2 hours
- Homework: 2-3 hours
- Total: 3.5-5 hours
Key Concepts¶
Functions¶
Functions are the building blocks of organized code. You'll learn:
- Defining functions with def
- Parameters and arguments
- Return values
- Default arguments
- args and *kwargs
Modules¶
Modules help you organize code into logical units: - Creating your own modules - Importing modules and specific functions - Understanding the Python standard library - Package organization
Tips for Success¶
- Practice function design - Think about what each function should do
- Use descriptive names - Function names should explain their purpose
- Keep functions focused - Each function should do one thing well
- Comment your code - Explain complex logic
- Test as you go - Try your functions with different inputs
Next Steps¶
After completing Week 2 and the Rock, Paper, Scissors game, move on to Week 3 to learn about file I/O and error handling!
