4.3. range( ) 문법

The range()Function


If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions:
만약 숫자로 이루어진 내용을 반복해서 작업해야 한다면, 내장함수(built-in function)인 range()를 사용하면 편리하다. range()는 주어진 범위(range) 안에서 등차 수열을 만든다.
-arithmatic progressions : 등차수열 *-등차수열 : 서로 이웃하는 두 항 사이의 간격이 일정한 수열

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

The given end point is never part of the generated sequence; range(10) generates 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the ‘step’):
주의할 점: range()함수에 주어진 end point, 즉 위 예시에서 괄호 안에 주어진 '5'는 만들어지는 시퀀스에 포함되지 않는다. 마찬가지로 range(10)은 10개의 값을 가지는 시퀀스를 만들어내지만, 10은 포함하지 않는다. range(10)은 0부터 시작하여, 0~9까지 10개의 값을 가진다. range()가 0이 아닌 다른 수로 범위를 시작하게 할 수도 있다. 또는 1씩 증가하지 않고 특정한 간격을 가지도록 할 수도 있다. 마이너스의 간격을 가지는 것도 가능하다. 때로 이러한 간격은 'step'이라고 불린다.

참고
sequence 라는 단어가 계속해서 등장한다. 영화 등에서 하나의 사건 덩어리(하나로 묶을 수 있는 연속된 사건이나 행동 들)를 묶어서 시퀀스라고 부른다. 하나의 '에피소드'와 비슷하다. 파이썬에서 말하는 시퀀스는 주어진 명령으로 인해 생기는 결과 덩어리인 듯 하다. 앞으로 그냥 계속 시퀀스로 표기한다. 적절한 단어가 떠오르면 교체할 예정.

range(5, 10)
   5 through 9  

range(0, 10, 3)  # 0부터 10이전까지, 3씩 증가한다.
   0, 3, 6, 9

range(-10, -100, -30)  # -10부터 -100이전까지, -30씩 증가한다.
  -10, -40, -70

To iterate over the indices of a sequence, you can combine range() and len() as follows:
시퀀스 안의 인덱스들에 반복작업을 하기 위해서, range()len()을 조합할 수 있다.

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
...     print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb

In most such cases, however, it is convenient to use the enumerate() function, see Looping Techniques.
사실, 이런 경우에는 enumerate()함수를 쓰는 쪽이 더 편리하다. 반복 문법을 사용하는 기술에 대해 알아보자.
-여기서 원본 글의 '반복문법을 사용하는 기술' (Looping Techniques)는 다른 글로 연결된다. 하지만 이 글에서는 아직 작성하지 못하였다.

A strange thing happens if you just print a range:
range()print()안에 사용하면 이상한 일이 벌어진다.

>>> print(range(10))
range(0, 10)

In many ways the object returned by range() behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.
많은 경우 range()의 결과는 리스트처럼 보이지만, 사실은 리스트가 아니다. range()는 우리가 어떤 동작을 반복시켰을 때 얻어진 값을 보여줄 뿐이다. range()는 리스트를 만들지 않고 공간을 절약한다.

We say such an object is iterable, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. We have seen that the for statement is such an iterator. The function list() is another; it creates lists from iterables:
이처럼 주어진 범위 안에서 연속된 값들을 얻을 수 있는 것들을 iterable이라고 부른다. iterable은 함수과 구조의 대상으로 적절하다. for문법 역시 이와 비슷한 iterator이다. list()함수는 iterable들을 가지고 리스트를 만든다.

>>> list(range(5))
[0, 1, 2, 3, 4]

Later we will see more functions that return iterables and take iterables as argument.
나중에 우리는 iterable들을 결과값으로 돌려주거나(return) 인수로 가지는 함수들을 더 많이 알게 될 것이다.

참고
iterableiterator에 대해 정리된 글이 있어 링크를 첨부한다. ▶ [물과같이]님의 글 : "Python iterable과 iterator의 의미
Python docs에서 간단하게 정의만 옮겨오자면

  • Iterable : An object capable of returning its members one at a time.
  • iterator : An object representing stream of data
    *차후 내용을 추가할 필요가 있음

return
보통 '반환하다'라고 번역된다. 함수에 어떤 재료를 집어넣어서 나오는 값을 우리에게 보여주는 것을 말하는데, '반환하다' 외에 다른 표현이 있었으면 싶지만 아직 적절한 것이 떠오르지 않는다. argument
인수 : 초기에 주어지는 값. 동작을 수행하기 위해 주어지는 변수
예를 들어, 변수 = [a,b,c]에서 a,b,c 각각은 '인수'이다.
-위 내용은 아직 불안정한 풀이라고 생각됨. 점차 수정하겠음

results matching ""

    No results matching ""