{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 3 일차\n", "# For Loops의 이해\n", "5명의 팀을 구성하여 의논하여 다음 코드의 실행 결과를 예상해보자.\n", "\n", "아래 명령은 미리 정의되어 있는 명령어들의 집합을 쓸 수 있도록 한다. 거북이를 사용하여 그림을 그릴 수 있는 명령어 집합을 제공한다." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import turtle as t # 거북이 라이브러리를 t라는 이름으로 불러옴" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```for```에서 중요한 것은 **들여쓰기**와 반복 횟수이다. 용법에 익숙해지자." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 1\n", "다음의 명령들 중에서 ```range```라는 명령은 반복을 하도록 하는 명령(함수)이다. ```range()```는 인자의 수가 가변적이다. 1개 부터 3개까지 입력할 수 있다. 인자가 하나 일 때는 반복의 횟수를 지정한다. 인자가 두 개인 경우 시작값과 반복 종료값를 지정한다. 인자가 셋인 경우 시작값, 반복 종료 값, 건너뜀 값을 지정할 수 있다. 아래 코드의 실행 결과를 예상해보자." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import turtle as t\n", "\n", "for x in range(3):\n", " t.forward(100)\n", " t.left(120)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import turtle as t\n", "\n", "for x in range(5):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import turtle as t\n", "\n", "for x in range(1, 4):\n", " t.forward(100)\n", " t.left(90)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import turtle as t\n", "\n", "for x in range(3, 6):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import turtle as t\n", "\n", "for x in range(3, 8, 2):\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제 2\n", "하나의 이름에 하나의 값만 저장하는 것을 보고 변수라고 부른다. 하나의 이름을 갖고 있지만 여러 개의 다른 값을 저장하는 것도 변수이기는 한데, 특별히 배열이라는 부른다. 대괄호를 쓰고 그 안에 값들을 쉼표로 구분하면 된다. 배열은 상당히 사용빈도가 높기 때문에 익숙해지는 것이 좋다. 아래의 예를 실행해서 어떻게 동작하는지 확인해보자. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 감나무 402\n", "400\n" ] } ], "source": [ "# 배열의 사용 예 1\n", "Numbers = [100, \"감나무\", 300, 400, 401, 402] # 배열의 선언, 문자는 쌍따옴표로 묶음\n", "print(Numbers[0], Numbers[1], Numbers[5]) # 배열의 값의 호출, 주의 할 것은 시작위치값이 0부터 시작\n", "print(Numbers[0]+Numbers[2]) # 연산도 가능함" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 배열의 사용 예 2\n", "primes = [2, 3, 5, 7] # primes라는 변수에 값이 4개가 저장되어 있음\n", "for prime in primes: # primes는 배열이라는 타입을 갖고 있음\n", " print(prime)\n", " print(prime[3], prime[2], prime[1], prime[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. ```for``` 문을 사용하여 다음과 같은 결과를 출력하는 프로그램을 작성해보자. 배열 또는 변수를 사용하여도 됨\n", "```\n", "Great, delicious ham \n", "Great, delicious eggs \n", "Great, delicious nuts \n", "```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. 지난 시간에 작성한 좋아하는 연예인 10명의 이름과 나이 그리고 좋아하는 이유를 한 문장으로 해서 3개의 서로 다른 문장을 출력하는 프로그램을 수정하여 배열을 활용하도록 작성해보자. 배열의 이름으로 NAME (좋아하는 연예인 이름)과 AGE (나이)를 사용하자. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 지난 시간의 코드 참고" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제 3\n", "다음 코드의 예상 결과를 작성해보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 100 \n", "sum = 0 \n", "for counter in range(1,n+1): \n", " sum = sum + counter \n", " print(\"sum of 1 until \", n, \": \", sum)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(0, 5):\n", " for j in range(0, i+1):\n", " print(\"* \", end=\"\") # end=\"\" 는 줄바꿈을 하지 않는다는 뜻\n", " \n", " print(\"\\r\") # “\\r”는 줄바꿈을 한다는 뜻\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제 4\n", "아래와 같이 출력하는 프로그램을 작성해보자\n", "```\n", " *\n", " * *\n", " * * * \n", " * * * *\n", " * * * * * \n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 숙제\n", "\n", "1. ```for``` 문을 사용하여 아래와 같이 출력하는 프로그램을 작성해보자.\n", "```\n", " *\n", " * *\n", " * * * \n", " * * * *\n", " * * * * * \n", "```\n", "2. ```for``` 문을 사용하여 다음과 같이 출력하는 프로그램을 작성해보자.\n", "```\n", " *\n", " * *\n", " * * * \n", " * * * *\n", " * * * * * \n", " * * * *\n", " * * * \n", " * *\n", " *\n", "```\n", "3. ```for``` 문을 사용하여 배열을 활용하여 다음과 같이 출력하는 프로그램을 작성해보자.\n", "```\n", " H\n", " He\n", " Hel\n", " Hell\n", " Hello\n", " Hello \n", " Hello W\n", " Hello Wo\n", " Hello Wor\n", " Hello Worl\n", " Hello World\n", "```" ] } ], "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }