Lesson guide
What this Python exercise practices
Square Each Number is a beginner practice lesson that focuses on loops, iteration, counters. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Lists or strings
- Basic for loop syntax
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Summary
Return a list where each number from the input has been squared.
Problem statement
Write a function square_each(nums) that takes a list of integers nums and returns a new list where each integer is replaced by its square (n * n). Preserve the order. For an empty input list, return an empty list.
Task
Practice transforming list elements using loops and returning a new list.
Examples
Simple squaring
Input
square_each([1, 2, 3])
Output
[1, 4, 9]
Explanation
Each element is squared: 1->1, 2->4, 3->9.
Input format
A single list of integers, e.g. [1, -2, 3].
Output format
A list of integers representing squares of the inputs, e.g. [1, 4, 9].
Constraints
Do not use list comprehensions for beginners who have not learned them yet; use an explicit loop. Aim for O(n) time.
Samples
Sample input 0
square_each([-1, 0, 5])
Sample output 0
[1, 0, 25]
Explanation 0
Squares: (-1)^2=1, 0^2=0, 5^2=25.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.