> For the complete documentation index, see [llms.txt](https://www.pythonclassroom.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pythonclassroom.com/loops/for-loop-range-two-arguments.md).

# for loop - range (two arguments)

## Objective

After working through this lesson, you’ll be able to

* Write Python code using the for loop using the range function with two arguments.

## Introductory Problem

Using a for loop, print numbers 0 - 6

{% code title="intro.py" %}

```python
done = False
while not done:
    print("Menu")
    print("I - Introductory Problem")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "I":
        print("Introductory Problem")
        for x in range(7):
            print(x)
    elif choice == "Q":
        print("Exiting Game!")
        done = True
```

{% endcode %}

## for loop

With two arguments in the range function, the sequence starts at the first value and ends one before the second argument.

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Ld1TCC5sk4sA_mxW6_f%2F-Ld1TFkOIEArjlGIZDcn%2FScreen%20Shot%202019-04-21%20at%208.36.13%20PM.png?alt=media\&token=78acce95-4cc4-4231-bb60-b3f6a86dc164)

{% hint style="info" %}
Programmers us&#x65;**`x`**&#x6F;&#x72;**`i`**&#x61;s a stepper variable.
{% endhint %}

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Ld1TXPMY1ann_Uzjaca%2F-Ld1TjgCxjqb3jS25Y-Y%2FScreen%20Shot%202019-04-21%20at%208.38.26%20PM.png?alt=media\&token=d40044fa-16f0-4b62-b9fd-fea91bf10513)

## Examples

### Example 1

Use a for loop to print numbers from 1 to 6.

{% code title="example1.py" %}

```python
done = False
while not done:
    print("Menu")
    print("E1 - Example 1")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E1":
        print("Example 1")
        for x in range(1,7):
            print(x)
    elif choice == "Q":
        print("Exiting Game!")
        done = True
```

{% endcode %}

#### Example 1 Output

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-M2eWmjHl7FGcSGmlzW-%2F-M2eXC5sJrvB9nLrRcIL%2FScreen%20Shot%202020-03-17%20at%206.06.28%20PM.png?alt=media\&token=4608dd73-5182-4fcd-915b-3086cb241e76)

### Example 2

Using a for loop, create a table to show the numbers -5 to 5 and their squares.  Create a header:

print("Number\tSquared")

{% code title="example2.py" %}

```python
done = False
while not done:
    print("Menu")
    print("E2 - Example 2")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E2":
        print("Example 2")
        print("Number\tSquared")
        for x in range(-5,6):
            print(x,"\t",x**2)
    elif choice == "Q":
        print("Exiting Game!")
        done = True
```

{% endcode %}

#### Example 2 Output

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-M2eWGR1S6lDg9f1emyH%2F-M2eWhMlRe1v-MYTPCBS%2Fimage.png?alt=media\&token=65e6666d-d166-4c5d-95dd-161d81b270ac)

### Example 3

Ask the user for a range, meaning a minimum and maximum, of numbers .  Determine how many numbers are divisible by a number within that range.  Divisible means capable of being divided by another number without a remainder.  How many questions do you need to ask? Hint: use the modulus operator %.

{% code title="example3.py" %}

```python
done = False
while not done:
    print("Menu")
    print("E3 - Example 3")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E3":
        print("Example 3")
        count = 0
        min = int(input("Min: "))
        max = int(input("Max: "))
        divisible = int(input("Divisible: "))
        for x in range(min,max+1):
            if x % divisible == 0:
                count = count + 1
        print("Count:",count)
    elif choice == "Q":
        print("Exiting Game!")
        done = True
```

{% endcode %}

#### Example 3 Output

Example output if you choose 5 for the minimum, 10 for the maximum and divisible of 2.

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-M2eXEskvjysNF2IVddz%2F-M2eXcEIYornYxKlN1ac%2FScreen%20Shot%202020-03-17%20at%206.08.22%20PM.png?alt=media\&token=44dd8dd1-b242-4c6d-9ea9-5084050fcd2a)

### Example 4

From yesterday’s lesson, using a for loop, ask the user for 5 integers.  Print the third input.

{% code title="example4.py" %}

```python
done = False
while not done:
    print("Menu")
    print("E4 - Example 4")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E4":
        for x in range(5):
            num = int(input("Number: "))
            if x == 2:
                third = num
        print("Third Number:",third)
    elif choice == "Q":
        print("Exiting Game!")
        done = True
```

{% endcode %}

#### Example 4 Output

Example output if you choose the numbers 10, 3, 4, 11 and 3.

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-M2eXx8lvumODcSlTD4o%2F-M2eYWLQ080EECcVP4zi%2FScreen%20Shot%202020-03-17%20at%206.12.01%20PM.png?alt=media\&token=ebc08ff7-c47b-4f01-8387-0b239efeead3)

### Combined Examples

```python
done = False
while not done:
    print("Menu")
    print("E1 - Example 1")
    print("E2 - Example 2")
    print("E3 - Example 3")
    print("E4 - Example 4")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E1":
        print("Example 1")
        for x in range(1,7):
            print(x)
    elif choice == "E2":
        print("Example 2")
        print("Number\tSquared")
        for x in range(-5,6):
            print(x,"\t",x**2)
    elif choice == "E3":
        print("Example 3")
        count = 0
        min = int(input("Min: "))
        max = int(input("Max: "))
        divisible = int(input("Divisible: "))
        for x in range(min,max+1):
            if x % divisible == 0:
                count = count + 1
        print("Count:",count)
    elif choice == "E4":
        for x in range(5):
            num = int(input("Number: "))
            if x == 2:
                third = num
        print("Third Number:",third)
    elif choice == "Q":
        print("Exiting Game!")
        done = True
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pythonclassroom.com/loops/for-loop-range-two-arguments.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
