How to use the range function, which is often used sensuously.

If you know it, the width will expand and it will be convenient.

**table of contents**

- [What you can do with the range function](# 1-What you can do with the range function)
- [Basic syntax of range function](# 2-Basic syntax of range function)
- [What is range type? ](What is # 3-range type)
- [How to check the contents of the execution result of the range function](# 4-Example of how to check the contents of the execution result of the range function)
- ① [Make it a list](Make it a #list)
- ② [Specify the sequence number](#Specify the sequence number)
- ③ [Retrieve with for statement](Extract with #for statement)
- [Use Minus](# 5-Use Minus)
- [If there is no element in the range](# 6-If there is no element in the range)
- [Error Occurrence Case](# 7-Error Occurrence Case)

## 1. What you can do with the range function

■ Numerical values in the specified range can be calculated as one set.

Example: Returns 9 elements "0,1,2,3,4,5,6,7,8" from 3 information "initial value: 0, end of range: 9, change amount: 1" ..

- The value at the end of the range is not included (less than). It means that it ends when it reaches that value.

■ Initial value and amount of change can be omitted. Example: Returns five elements "0,1,2,3,4" from one piece of information "end of range: 5".

## 2. Basic syntax of range function

`range(x, y, z)`

--Only ** 3 ** elements are used
--"X": Initial value. Optional. Default 0
--"Y": End of range. ** Cannot be omitted **.
-"Z": Amount of change. Optional. Default 1
--The end of the range is less than ** **.
--The specified number is not included.
――It means to end when the value is reached.
--Elements are ** integers **
--You can also use minus
--Float cannot be used
--Elements can be ** "integers" and "variables" **
--At least one element **
--Specify only the end of the range. (In the above, the variable "z")
-** Output is range type **
--You can't see the contents just by executing the range function **
--If there is no data in the specified range, the output will be empty (no error will occur).

### Classify by number of elements The following three descriptions can be used. ① `range (y)`: 1 element ② `range (x, y)`: 2 elements ③ `range (x, y, z)`: 3 elements

> ** ■ python official description ** [(URL)](https://docs.python.org/ja/3/library/functions.html#func-range) range(stop) range(start, stop[, step])

-Stop: An integer (or variable) that specifies the end of the range. -Start: An integer (or variable) that specifies the initial value. -Step: An integer (or variable) that specifies the amount of change.

## 3. What is range type? The output result of the range function. You can check the instructions.

If the amount of change is not specified, two numerical values, "initial value" and "end of range", are returned.

- 0 is displayed even if the "initial value" is omitted.

If the amount of change is also specified, three numerical values of "initial value", "end of range", and "amount of change" are returned.

** ▼ range type output result when only the end value is specified **

`Specify only the end value (int)`

```
range(10)
#Output result
# range(0, 10)
```

`Specify only the end value (variable)`

```
a = 10
range(a)
#Output result
# range(0, 10)
```

** ▼ Range type output result when initial value and change amount are also specified **

`Also specify the initial value (int)`

```
range(5,99)
#Output result
# range(5, 99)
```

`Also specify the initial value and the amount of change (int)`

```
range(5,99,11)
#Output result
# range(5,99,11)
```

`Specify the initial value and the amount of change (variable)`

```
a = 5
b = 99
c = 11
range(a,b,c)
#Output result
# range(5,99,11)
```

## 4. How to check the contents of the execution result of the range function (example) I don't know what kind of numerical value is stored in range type. How to check the contents quickly.

① Make a list ② Specify the sequence number ③ Take out with a for statement

`list type (Example 1)`

```
list(range(10))
#output
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```

`list type (Example 2)`

```
a = range(10)
list(a)
#output
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```

`list type (Example 3)`

```
a = 5
b = 99-10
c = 3*4
list(range(a,b,c))
#output
# [5, 17, 29, 41, 53, 65, 77]
```

### ② Specify the sequence number

`Specify the sequence number`

```
range(3,9)[4]
#output
# 7
```

The output of `range (3,9)`

is [3, 4, 5, 6, 7, 8]
The fourth (counting from 0) is 7.

### ③ Take out with a for statement You can print the contents one by one by using the for statement and the print method.

`Extract with a for statement (Example 1: One element)`

```
for a in range(5):
print(a)
#output
0
1
2
3
4
```

`Extract with a for statement (Example 2: Two elements)`

```
for b in range(4,8):
print(b)
#output
4
5
6
7
```

`Extract with a for statement (Example 3: 3 elements)`

```
for c in range(9,30,7):
print(c)
#output
9
16
23
```

`Extract with a for statement (Example 4: Variable)`

```
A =range(9,30,7)
for a in A:
print(a)
#output
9
16
23
```

You can also use minus for the initial value, range, and amount of change. (Decrease by the specified number)

`Initial value minus (Example 1)`

```
list(range(-3, 2))
#output
# [-3, -2, -1, 0, 1]
```

`Initial value / closing price minus (Example 2)`

```
list(range(-12, -6))
#output
# [-12, -11, -10, -9, -8, -7]
```

"Initial value <End value" (when the amount of change is positive. Default "+1")

If there is no value in the range, the contents will be empty (** not an error **)

`Change amount minus (Example 3)`

```
a =range(5,1,-1)
list(a)
#output
# [5, 4, 3, 2]
```

`Change amount minus-Specified by variable (Example 4)`

```
a = 10
b = -4 * 4
c = -6
A = range(a,b,c)
list(A)
#output
# [10, 4, -2, -8, -14]
```

### 6. If there are no elements in the range The data is just empty. No error occurs.

`Positive range (Example 1)`

```
list(range(10,5))
#output
# []
```

`Negative range (Example 2)`

```
list(range(-5, -10))
#output
# []
```

`Change amount minus (Example 2)`

```
list(range(-5, 10, -2))
#output
# []
```

`Error: Decimal point (float)`

```
range(1.25)
#output
# TypeError: 'float' object cannot be interpreted as an integer
```

`Error: string (str)`

```
range("AAA")
#output
# TypeError: 'str' object cannot be interpreted as an integer
```

[Return to top](Explains how to use the #pythonrange function with a concrete example)

Recommended Posts