1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

# No.046【Python】range関数について

### ■ Python3における range()関数

range function on Python3

`````` ここでは、Python3におけるrange関数の使い方を紹介する
``````
``````>>> # Python3には x range()はない
>>> # range()のみ
>>>
>>> print(xrange(3))
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
print(xrange(3))
NameError: name 'xrange' is not defined
``````
`````` range():range型のオブジェクトを返す
``````
``````>>> print(range(5))
range(0, 5)
>>>
>>> print(type(range(5)))
<class 'range'>
>>>
>>> for i in range(5):
print(i)

0
1
2
3
4
``````
`````` リストを生成したい場合はlist()を使う。
``````
``````>>> print(list(range(5)))
[0, 1, 2, 3, 4]
``````

### ■ 連番(0から任意の値までの): range(stop)

Serial numbers(from 0 to the optional value)

``````例：range(stop)の引数に整数を1つ指定
0 ≦ i < stopの連番を生成
``````
``````>>> print(list(range(5)))
[0, 1, 2, 3, 4]
>>>
>>> print(list(range(15)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
``````
`````` 負の値指定の場合、空となる
``````
``````>>> print(list(range(-5)))
[]
``````

### ■ 任意の範囲内の連番: range(start / stop)

Serial numbers within the optional range

``````range(start, stop)：引数に整数を2つ指定
start ≦ i < stopの連番を生成
stop ≦ startの場合は、空となる
``````
``````>>> print(list(range(5, 10)))
[5, 6, 7, 8, 9]
>>>
>>> print(list(range(10, 5)))
[]
>>>
>>> print(list(range(-5, 5)))
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
>>>
>>> print(list(range(5, -5)))
[]
``````
``````Startを0としたrange(0, stop)はrange(stop)と等価
``````
``````>>> print(range(0, 5) == range(5))
True
``````

### ■ 任意範囲または増分の等差数列: range(start, stop, step)

Arithmetic progression for the optional range or incremental

``````range(startm stop, step)；引数に整数を3つ指定
start ≦ i < stop
stepずつ増加する等差数列を生成
stepに負の値を指定：減少
start ≦ stopでない場合、空となる
``````
``````>>> print(list(range(1, 10, 4)))
[1, 5, 9]
>>>
>>> print(list(range(10, 4, 2)))
[]
>>>
>>> print(list(range(10, 4, -2)))
[10, 8, 6]
>>>
>>> print(list(range(1, 10, -4)))
[]
``````
``````stepを1としたrange(start, stop, 1)
range(start, stop)と等価。
``````
``````>>> print(range(4, 10, 1) == range(4, 10))
True
``````
``````startを0としたrange(0, stop, 1)
range(0, stop)およびrange(stop)と等価
``````
``````>>> print(range(0, 10, 1) == range(0, 10) == range(10))
True
``````

### ■ 逆順range関数

Reverse ordered range function

``````第三引数stepに負の値を指定:減少する連番および等差数列を生成

しかし、start・stopと合わせて適切な値を指定が必要
``````
``````>>> print(list(range(4, 10, 3)))
[4, 7]
>>>
>>> print(list(range(7, 1, -2)))
[7, 5, 3]
``````
``````reversed()を使うと結果は、逆順となる
``````
``````>>> print(list(reversed(range(4, 10, 3))))
[7, 4]
``````
``````reversed()の場合:for文で使う場合、list()は不要。
``````
``````>>> for i in reversed(range(3, 12, 2)):
print(i)

11
9
7
5
3
``````

### ■ 浮動小数点数(float)の場合

In case of float

``````整数(integer)のみ、range()の引数に指定可能

``````
``````>>> print(list(range(0.1, 1.0, 0.4)))
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
print(list(range(0.1, 1.0, 0.4)))
TypeError: 'float' object cannot be interpreted as an integer
``````
``````浮動小数点数の等差数列生成の場合：リスト内包表記
``````
``````>>> print([i / 10 for i in range(4, 12, 2)])
[0.4, 0.6, 0.8, 1.0]
``````
``````浮動小数点数との積を使用時、誤差が生じる場合がある
``````
``````>>> print([i * 0.1 for i in range(4, 10, 2)])
[0.4, 0.6000000000000001, 0.8]
>>>
>>> print([round(i * 0.1, 1) for i in range(4, 10, 2)])
[0.4, 0.6, 0.8]
``````
``````NumPyの場合：np.arange()
np.arange()の引数の指定は、range()と同様

``````
``````>>> import numpy as np

>>> print(np.arange(5))
[0 1 2 3 4]
>>>
>>> print(np.arange(34, 10))
SyntaxError: invalid syntax
>>>
>>> print(np.arange(2, 10))
[2 3 4 5 6 7 8 9]
>>>
>>> print(np.arange(2, 10, 1))
[2 3 4 5 6 7 8 9]
>>>
>>> print(np.arange(0.4, 1.0, 0.1))
[0.4 0.5 0.6 0.7 0.8 0.9]

``````

I'll update my article at all times.
So, please subscribe my articles from now on.

If you have some requests, please leave some messages! by You-Tarin

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
What you can do with signing up
1
Help us understand the problem. What are the problem?