2.2. Numerical Lists

It is common to store a set of numbers in a list. Python provides many tools to help making lists of numbers easily.

range() function

Using range(), we can easily generate a sequence of numbers. Here's an example of range() function:

The value inside `range()` denotes the beginning of the list and the last-minus-one of the list. To write the number 2 - 6, we need to use `range(2, 7)`

range() to Make a List

To convert the results of range() into a list, use the list() function as follows:

The range() function can also be used to skip numbers in a given range. This uses a third argument for range(). For example, here's two list: one is even-numbers, one only multiple of 5.

We can extend the use of range() to make various list. Here, we try to make a list of cubes (that is, numbers to the power 3). At the beginning, we assign an empty list to cubic_num. This is a common practice when we want to make a new list.

Inside the for loop, we raised the num to the power 3, saved it in a variable called cubic_num, then finally add it using append() to our empty list.

To write this code more compactly, you can omit the temporary variable cubic_num and append each new value to the list:

List Comprehensions

A list comprehensions allows you to generate the same list in just one line of code. This combines the for loop and the making of new items into one line without using append().

The following example builds the same list as the previous two examples:

> To use this syntax,
  1. Begin with a name for a list (here we use cubes).
  2. Open with [
  3. Write the expression for the values you want to store (here num ** 3)
  4. Write a for loop to generate the number into the expression (here for num in range(1,11))
  5. Close with ]

It takes practice to write your own list comprehension, but you'll find it useful once you are used to it. When you're writing 3 or 4 lines to generate a list, consider using list comprehension.


Exercise 2.2

  1. Counting to Fifteen
    Print the following
    Here are some numbers
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
    Hint: Use end=", "

  2. One Hundred
    Using the given a and b, print the following
    We are counting from 6 to 9
    6, 7, 8, 9,
    (Change a,b =6,9)

  3. Summing with Loop
    Using for loop, add numbers from 6 to 9 then save it in the variable sum_a_b. Then, print:
    If we add the numbers from 6 to 9 we get 30.
    Hint: Use sum_a_b += 1

  4. Even Numbers
    Use the third argument of the range() function to make a list of the even numbers from 0 to 30. Then, use a for loop to print the following:
    We are now counting down from 30 by 2 steps:
    30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0,

  5. Multiples of 6
    Make a list of the multiples of 6 from 0 to 30. Use a for loop to print the following:
    The multiples of 6 from 0 to 30 are:
    0, 6, 12, 18, 24, 30,

  6. Squares
    A number raised to the second power is called a square (the square of 3 is written as 3**2 in Python). Make a list of squares from the list in no.5. Then, use a for loop to print out the following:
    The squares of those numbers are:
    0, 36, 144, 196, 576, 900,

  7. Squares Comprehension
    Use list comprehension to make a list of squares of numbers you got in no. 6, then print the following:
    Again, the squares of those numbers are:
    0, 1296, 20736, 38416, 331776, 81000,