Range

Python’s range function allows us to get integer numbers between two numbers:

>>> for n in range(0, 5):
...     print(n)
...
0
1
2
3
4

Note that the numbers go up to but not including the final value.

We’re going to start with 0, we can leave it off:

>>> for n in range(5):
...     print(n)
...
0
1
2
3
4

We can specify a third argument as a step. Here, we get integers from 0 to 100 by 10s.

>>> for n in range(0, 100, 10):
...     print(n)
...
0
10
20
30
40
50
60
70
80
90

I’ve contradicted myself a little here, because we started at 0, but I kept it in. Why? Let’s see what happens if we leave it off.

>>> for n in range(0, 100, 10):
...     print(n)
...
10
11
12
13
14
15
16
# ...
99

It gets integers to 99 starting at 10. If we want it to get numbers by steps, there have to be three arguments.

Your Turn: Range 🏁

Range Exercises

Present Moment Journal

A common meditative practice is to stop periodically and take note of sounds, smells, and physical sensations. Using time.sleep() (refer to the documentation or look in pause.py for more info) to create a program that:

  1. Prompts the user for five sounds, five smells, and five physical sensations and adds the inputs to lists.
  2. Wait a certain amount of time and repeat the process, appending the responses to the existing lists.

Custom Meditative Breathing Guide

Improve upon your program breathe.py by allowing the user to input an amount of time they’d like to practice breathing, and have your program determine the number of breaths needed during that time.

Number Guesser

Refactor your number guesser program to use range.

Factors

Make a program factors.py that takes a number and prints out all of its factors. If the number has no factors it should tell you it’s prime.

Example:

$ python3.6 factors.py 4
1
2
$ python3.6 factors.py 30
1
2
3
5
6
10
15
$ python3.6 factors.py 9406
1
97
$ python3.6 factors.py 2
2 is prime!
$ python3.6 factors.py 8647
8647 is prime!

Visual Meditative Breathing Guide

Improve upon your program breathe.py by swapping out the printed word cues with a visual one! You can copy/paste from the provided file oscillating_line.py to try to make this work.