Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: List Range and Concatenation

check

Finding list ranges and combining lists.


Python allows you to create lists using a built-in function called range(), and combine or concatenate two lists using the + operator. Both of these operations are crucial in data processing and manipulation tasks in Python.

List Range

The range() function generates a sequence of numbers within a given range. You can convert this range into a list using the list() function.

numbers = list(range(1, 11))
print(numbers)

> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List Concatenation

The + operator can be used to combine or concatenate two lists into one.

numbers_2 = list(range(11, 21))
all_numbers = numbers + numbers_2
print(all_numbers)

> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Practice: Generate List of Even Numbers

  1. Create a list named even_numbers and initialize it with even numbers from 2 to 20 using range().

  2. Print the even_numbers list.

Run your code and confirm that the output is a list of even numbers from 2 to 20.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
even_numbers = list(range(2, 21, 2))
print(even_numbers)

Practice: Combine Two Lists

  1. Create a list odd_numbers with odd numbers from 1 to 20.

  2. Concatenate even_numbers and odd_numbers into a list all_numbers.

  3. Print the all_numbers list.

Run your code and confirm that the output is a combined list of even and odd numbers from 1 to 20.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
odd_numbers = list(range(1, 20, 2))
all_numbers = even_numbers + odd_numbers
print(all_numbers)

Nice work! These operations allow for efficient manipulation of lists in Python, which is particularly useful when working with large data sets.