{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7. range보다는 enumerate를 사용하라"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "range 내장 함수는 어떤 정수 집합을 이터레이션하는 루프가 필요할 떄 유용하다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_bits = 0\n",
    "for i in range(32):\n",
    "    if randint(0, 1):\n",
    "        random_bits |= 1 << i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b10000100000011001111110100101010\n"
     ]
    }
   ],
   "source": [
    "print(bin(random_bits))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "바닐라 맛있어요.\n",
      "초콜릿 맛있어요.\n",
      "피칸 맛있어요.\n",
      "딸기 맛있어요.\n"
     ]
    }
   ],
   "source": [
    "flavor_list = ['바닐라', '초콜릿', '피칸', '딸기']\n",
    "for flavor in flavor_list:\n",
    "    print(f'{flavor} 맛있어요.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: 바닐라\n",
      "2: 초콜릿\n",
      "3: 피칸\n",
      "4: 딸기\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(flavor_list)):\n",
    "    flavor = flavor_list[i]\n",
    "    print(f'{i + 1}: {flavor}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "it = enumerate(flavor_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, '바닐라')\n"
     ]
    }
   ],
   "source": [
    "print(next(it))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, '초콜릿')\n"
     ]
    }
   ],
   "source": [
    "print(next(it))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: 바닐라\n",
      "2: 초콜릿\n",
      "3: 피칸\n",
      "4: 딸기\n"
     ]
    }
   ],
   "source": [
    "for i, flavor in enumerate(flavor_list):\n",
    "    print(f'{i + 1}: {flavor}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "enumerate의 두 번째 파라미터로 어디부터 수를 세기 시작할지 지정할 수 있다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: 바닐라\n",
      "2: 초콜릿\n",
      "3: 피칸\n",
      "4: 딸기\n"
     ]
    }
   ],
   "source": [
    "for i, flavor in enumerate(flavor_list, 1):\n",
    "    print(f'{i}: {flavor}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 기억해야 할 내용\n",
    "- enumerate를 사용하면 이터레이터에 대해 루프를 돌면서 이터레이터에서 가져오는 원소의 인덱스까지 얻는 코드를 간결하게 작성할 수 있다.\n",
    "- range에 대해 루프를 돌면서 시퀀스의 원소를 인덱스로 가져오기보다는 enumerate를 사용하라.\n",
    "- enumerate의 두 번째 파라미터로 어디부터 원소를 가져오기 시작할지 지정할 수 있다. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}