Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Create a list of integers from 1 to nE02. Access elements by positive and negative indexE03. Slice a list to get first and last three itemsE

Problem No 1

Create a list of integers from 1 to n

Easy

6 minute session

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.

Code editor
Loading editor…

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.

12:45 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.