Problem No 1
Create a list of integers from 1 to n
Easy≈ 6 minute session
Lesson guide
What this Python exercise practices
Create a list of integers from 1 to n is a beginner practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Summary
Generate a list containing integers from 1 up to n (inclusive).
Problem statement
Given an integer n, create and return a list of integers starting at 1 and ending at n (inclusive). If n is less than 1, return an empty list. This helps you practice list creation and basic control flow.
Task
Write a function that returns a list of integers from 1 to n. Handle non-positive n by returning an empty list.
Examples
Basic example
Input
create_list(5)
Output
[1, 2, 3, 4, 5]
Explanation
Returns a list with integers from 1 to 5.
Input format
A single integer n passed to the function create_list(n).
Output format
A list of integers from 1 to n (inclusive); [] if n < 1.
Constraints
n is an integer. If n < 1, return an empty list.
Samples
Sample input 0
create_list(3)
Sample output 0
[1, 2, 3]
Explanation 0
List from 1 to 3.
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.