# 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.

![](/files/-Ld1TFkOIEArjlGIZDcn)

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

![](/files/-Ld1TjgCxjqb3jS25Y-Y)

## 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

![](/files/-M2eXC5sJrvB9nLrRcIL)

### 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

![](/files/-M2eWhMlRe1v-MYTPCBS)

### 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.

![](/files/-M2eXcEIYornYxKlN1ac)

### 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.

![](/files/-M2eYWLQ080EECcVP4zi)

### 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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
