{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02wk-2: 파이썬의 자료형 (3)\n",
"\n",
"최규빈 \n",
"2023-03-15\n",
"\n",
"
\n",
"\n",
"# 강의영상\n",
"\n",
"> \n",
"\n",
"# list 기본내용\n",
"\n",
"## 선언\n",
"\n",
"`-` 리스트의 선언"
],
"id": "1c4f5c96-2e26-48b7-8ca1-f48a6961367b"
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"a= [1,2,3,22] "
],
"id": "c68e3bd1-77ce-4aa1-a066-6f5cf661f871"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 비어있는 리스트의 선언"
],
"id": "ab8af47c-c138-4173-a26e-32d6ab328649"
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"a= []\n",
"a"
],
"id": "c7eff869-ed60-4922-97f9-6d69b8d123de"
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"a= list()\n",
"a"
],
"id": "8142f0ed-9dc6-4342-b679-b0147005d266"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 연산\n",
"\n",
"`-` 더하기연산"
],
"id": "b8b8c649-1aaf-470d-9920-3ca2e35d82e1"
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"[1,2]+[-3,4,5]"
],
"id": "85dff1ed-accb-4674-a770-fdf10d385059"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 우리의 예상과 다른 결과가 나옴 $\\to$ 파이썬은 R처럼 자체적으로 좋은\n",
" 계산기능을 내장하고 있지 않음.\n",
"\n",
"`-` 브로드캐스팅과 같이 R에서는 당연히 가능했던 기능을 사용할 수 없음."
],
"id": "5ec1750f-a457-4f8f-ae9a-a1556297c824"
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"[1,2,3,4,5] + 1"
],
"id": "e41a662c-eec0-4838-92d4-96ff272702f8"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 뺄셈은 정의되지 않음"
],
"id": "05e95f28-a0b0-4ca5-b48a-03d975c944fb"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"a= [1,2,1,2]\n",
"a-[1,2]"
],
"id": "4eedf8a0-f46f-4414-95cd-0f660efc85d5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 곱하기는 정의가능"
],
"id": "8ed094a0-8278-4ceb-83e2-f59f459fecaa"
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"[1,2]*3"
],
"id": "d96b70b4-4cda-41aa-9ac1-acf488d823af"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 나눗셈은 정의되지 않음"
],
"id": "825e5136-fe88-4477-b4bc-847fde35c376"
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"[1,2,1,2,1,2] /3"
],
"id": "08b974ff-b341-4783-95b2-7cae20b02d0f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 더하기와 곱하기는 원소의 추가와 반복추가를 의미하지만 그렇다고 해서\n",
"뺄셈과 나눗셈이 원소의 삭제를 의미하는것은 아님\n",
"\n",
"`-` 더하기와 곱하기가 원소의 추가와 반복추가를 의미하여 편리할때도\n",
"있긴하지만, 우리는 산술적인 `+`, `*` 를 원하는 경우도 있다. 이럴 경우는\n",
"어떻게 할 수 있을까?\n",
"\n",
"(예제)"
],
"id": "73107078-170c-463c-b618-bf27fa1eeb7a"
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2]\n",
"b=[3,4]"
],
"id": "643db365-7958-4ebf-87e1-8d086fde4623"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"a+b = \\[4,6\\] 이 되도록 하려면?\n",
"\n",
"(풀이1)"
],
"id": "0ef56a5a-bdc0-495d-bf0e-8c2b9727d6a9"
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"[a[0]+b[0],a[1]+b[1]]"
],
"id": "55f4bdc2-d08d-44f5-851f-a779500678e5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"풀이가 가능한 이유? a,b는 리스트이지만 a\\[0\\], a\\[1\\], b\\[0\\], b\\[1\\] 은\n",
"각각 인트형임. 인트형은 + 연산이 가능했음.\n",
"\n",
"(풀이2)\n",
"\n",
"numpy 패키지 (파이썬의 여러 수치연산들을 담당하는 라이브러리)\n",
"\n",
"- 이러한 벡터연산은 누구나 필요로 하는 연산임.\n",
"- 내가 아니더라도 누군가가 프로그램화 해놓았을 것임.\n",
"- 그 누군가가 자신이 만든 코드를 잘 정리하여 무료로 배포했을 수도\n",
" 있음. (패키지를 배포한다고 표현)\n",
"- 그 패키지를 우리는 가져와서 설치한뒤 사용하기만 하면된다.\n",
"\n",
"패키지를 설치하는 방법\n",
"\n",
"- `!pip install numpy` \\# 최신버전을 설치함\n",
"- `!conda install -c conda-forge numpy -y` \\# 안전한 버전을 설치함\n",
"\n",
"설치된 패키지를 사용하는 방법\n",
"\n",
"- `import numpy` 한뒤에 `numpy.??`로 기능을 사용\n",
"- `import numpy as np` 한뒤에 `np.??`로 기능을 사용"
],
"id": "15713b62-66d8-4eb2-8825-19b312526f93"
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"import numpy ## 설치한패키지를 쓰겠다고 선언함 "
],
"id": "8ef820c4-1e9d-49a1-8238-2705a8850a50"
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"a=[1,2]\n",
"b=[3,4]"
],
"id": "86978180-0621-40b0-b6c8-2cdb0fd60853"
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"aa = numpy.array(a)\n",
"bb = numpy.array(b)"
],
"id": "0e6322b6-ca8a-47f2-8b4e-ec7fa6372b6a"
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"aa+bb"
],
"id": "f4bfe720-241c-4d08-b47b-3210775f6bde"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"여러가지 연산 가능 (마치 R처럼 쓸 수 있음)"
],
"id": "e3cd21d3-e17d-43d5-8289-fb7d62f66be9"
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"2*aa"
],
"id": "8f5ce4c0-1a8c-4ccd-8ad2-450c0e97bdf1"
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"2*aa+1"
],
"id": "83773bda-e522-487f-a413-33184903be6b"
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"2*aa+1+bb"
],
"id": "a6aaf1c2-ac33-40b6-bbb2-028c0864bd89"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이3)"
],
"id": "e034b10c-4bb8-4793-8714-c4f0c7a9fec0"
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np ## 설치한 numpy라는 패키지를 쓰겠음. 그런데 numpy말고 np라는 이름으로 쓰겠음"
],
"id": "64c7d2ab-50a7-4d08-b204-27b4f3b00e4e"
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"np.array(a)+np.array(b)"
],
"id": "05f9e4a4-6912-4fb2-9725-f11cacadd665"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 인덱싱\n",
"\n",
"`-` str형과 동일한 방식"
],
"id": "088f60ed-c492-476c-b9bd-ef8a78fb099f"
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [],
"source": [
"a=[11,22,33,44,55] # 0 -4 -3 -2 -1"
],
"id": "1fab0cf8-b693-45f0-9ca2-be5923508d65"
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [],
"source": [
"a[-2:] # 끝의 2개의 원소를 뽑음 "
],
"id": "a5ed42cc-2ec2-4b4d-b390-8f22ad95f8f9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# list 고급내용 1\n",
"\n",
"## 콘테이너형 객체\n",
"\n",
"`-` 리스트의 원소는 int, float 따위만 가능한 것이 아니다. (리스트는\n",
"컨테이너형 객체이므로)"
],
"id": "46e974ea-156a-406e-9b18-2a54ba2a51a1"
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [],
"source": [
"lst = [1,3.14,True,'a',[1,2], \n",
" (1,2),{'name':'iu','age':27},{1,2,3}]"
],
"id": "6bca2ab6-762d-4476-9c3c-204778d824c0"
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [],
"source": [
"lst"
],
"id": "c202a191-a3e4-4a40-8353-03df6fa1eece"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"각 원소의 타입을 알아보자."
],
"id": "e216f36d-d913-404d-ba5e-5e80e36419b7"
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"type(lst[0])"
],
"id": "58b9d37c-017b-4965-9ced-8181f4b63772"
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"type(lst[1])"
],
"id": "8ad7e578-f4f4-409c-bc7a-ce188efb8293"
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
"type(lst[2])"
],
"id": "fef0fd0f-e394-4fcd-a2fd-cd986354640d"
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
"type(lst[3])"
],
"id": "97a7896b-bb24-4ef6-80f1-6772facb7345"
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"type(lst[4])"
],
"id": "2cdfa3a1-ddd7-4aa8-a48c-b76116358527"
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
"type(lst[5])"
],
"id": "990b57c8-03c9-4eea-8ee2-28e615fb410e"
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"type(lst[6])"
],
"id": "6a03c65f-054d-457a-a484-d081bae70e79"
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"type(lst[7])"
],
"id": "d251284d-2c55-471d-a498-5361b9e9f55a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` str은 컨테이너형이 아니다."
],
"id": "2d8323d8-5559-48dd-a2fa-582dd1380115"
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [],
"source": [
"'abcd'[2]"
],
"id": "7bac689f-25d5-424d-8092-02bd93edc2b8"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- str의 모든 원소는 문자임\n",
"\n",
"## 가변객체\n",
"\n",
"`-` 리스트는 원소를 수정할 수 있다. (리스트는 가변객체이므로)"
],
"id": "9149f154-c5a2-45d4-930c-778b27734f9d"
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [],
"source": [
"a=[11,22,33]\n",
"a"
],
"id": "03b12f77-4f18-4251-8ce4-51f7be28e631"
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
"a[0]"
],
"id": "3878c6ef-50b7-4b61-aef2-8d22a8bd3f17"
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [],
"source": [
"a[0]=111"
],
"id": "86bb058a-bf39-4a6b-8183-cc8ce2c7b62d"
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "0fd901e0-ef1d-4db9-b57e-1fffe10b28bb"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 원소수정은 당연한 기능같은데 이것이 불가능한 경우도 있다.\n",
"\n",
"(가능한경우)"
],
"id": "9691f769-6c04-46d1-a4ea-d4f29b8e451d"
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
"a=['g','u','e','b','i','n']\n",
"a"
],
"id": "147f5a58-b94f-45ea-9ab2-bb190913f6e9"
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
"a[0]"
],
"id": "e6c8c0b1-d95b-427a-92ca-ab09d7827974"
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [],
"source": [
"a[0]='G'"
],
"id": "53577bb1-80e2-4e9e-b17d-e7af1b1555f1"
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "ddf501eb-f3d9-4acd-8f77-f07424c9ab41"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(불가능한경우)"
],
"id": "7df74bbb-3d79-49c8-a246-fd3bc00509bc"
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [],
"source": [
"a='guebin'\n",
"a"
],
"id": "35db9d10-7e56-4c7b-9813-d302f453fdf8"
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [],
"source": [
"a[0]"
],
"id": "2bbde9f0-2a30-43cd-b76c-a578f181317a"
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [],
"source": [
"a[0]='G'"
],
"id": "d3712c4c-f962-4e7e-bd61-b8561f0afedd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 리스트의 원소 삭제\n",
"\n",
"(예제1) del을 이용한 원소삭제\n",
"\n",
"아래와 같이 문자로 된 리스트를 선언하자."
],
"id": "7832f641-8360-4f05-85ca-21cb8709b2bd"
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [],
"source": [
"a=['g','u','e','b','i','n']\n",
"a"
],
"id": "baab4373-6a21-4c64-b279-3595ead70469"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***사실 더 쉽게 선언할 수 있음***"
],
"id": "fb286a61-a590-4a22-a55a-2fe00662d702"
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [],
"source": [
"list('guebin')"
],
"id": "bbea1098-1882-4931-bbd5-97c843a1a464"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"첫번째 원소를 삭제하고 싶다면?"
],
"id": "343649e3-31b0-4c55-ae84-0564b4fa90bd"
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [],
"source": [
"del a[0]\n",
"a"
],
"id": "642812f1-f109-4d7f-b561-76a1afdeeda0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이 상태에서 다시 첫번째 원소를 삭제한다면?"
],
"id": "a973dabf-aa0a-4165-9cc2-86cadeb37538"
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [],
"source": [
"del a[0]\n",
"a"
],
"id": "6c0c6f66-ff71-4ac0-a16e-77ed9ca9b4c5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예제2) pop을 이용한 원소삭제"
],
"id": "e6654cc4-5b6b-48ab-9e56-bdf248941b7f"
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [],
"source": [
"a=list('guebin')\n",
"a"
],
"id": "47556a57-357a-48a2-b50c-900b3ff19f0f"
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"a.pop(0)"
],
"id": "b9825e53-c507-4caa-9e57-23d003111e49"
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "6ffc048f-6750-453b-a8bb-4451bb6d7a38"
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [],
"source": [
"a.pop(0)"
],
"id": "c2c4d187-e6e2-436f-8db2-ca3bcd18a707"
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "1e00e75d-2c1f-463d-bb52-aee86c5224d3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HW 0315\n",
"\n",
"`1`. 길이가 0인 리스트를 만들어라. (비어있는 리스트를 만들어라)\n",
"\n",
"(풀이1)"
],
"id": "a6588e7e-1799-479f-bfae-141b9ae347bf"
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"[]"
],
"id": "22ddba19-9269-4edb-97e2-77fb74ea96ee"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이2)"
],
"id": "6ea98cb0-6bc7-41ef-b285-0a4080e299ba"
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"list()"
],
"id": "36cf5ac9-70d4-46de-af2d-3e9a3affe355"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`2`. 다음의 실행값 중 가장 큰 숫자가 나오는 보기를 골라라. (에러가 나는\n",
"코드는 정답에서 제외함)\n",
"\n",
"``` python\n",
"(a) len(3.14) \n",
"(b) len([3.14]) \n",
"(c) len('3.14') \n",
"```\n",
"\n",
"(풀이)"
],
"id": "e33d258a-5834-492c-950e-9cdaf041d305"
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"len(3.14)"
],
"id": "1632a818-1ae1-4ab5-a4f1-f5933eb3a3f5"
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"len([3.14])"
],
"id": "543a1e27-2058-4395-9e80-339dd3d224e7"
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"len('3.14')"
],
"id": "651e4d30-f408-43e9-961f-a873de48dd5d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"따라서 답은 (c)\n",
"\n",
"`3`. 아래의 예제를 관찰하라."
],
"id": "9a481cfa-6877-4bc9-b204-db32c74c9d68"
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"'제 이름은 {}입니다'.format('최규빈')"
],
"id": "d93527ad-4af4-476d-bb47-bf939c1cf882"
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"'제 이름은 {}이고 사는곳은 {}입니다.'.format('최규빈','전주')"
],
"id": "b848a434-48b8-4ee4-82f3-2ec91ee2a124"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이 예제를 바탕으로 문자열에 포함된 `.format()`함수의 기능을 유추하라.\n",
"그리고 아래의 결과를 확인하라.\n",
"\n",
"``` python\n",
"string = '전북대학교 학생엠티가 {}월{}일-{}월{}일 완주 곶감펜션에서 있다고 합니다. 자세한 일정 등은 다시 공지하겠습니다.' \n",
"string.format(3,24,3,25)\n",
"```\n",
"\n",
"ChatGPT를 활용하여 `.format()`함수의 기능을 학습하라.\n",
"\n",
"(풀이)\n",
"\n",
"생략\n",
"\n",
"`4`. 아래의 예제를 관찰하라."
],
"id": "c0ff988f-ded0-467d-a122-4b085fe8a6ef"
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"'abcdefg'.replace('g','u')"
],
"id": "77f99a1c-fb7d-4268-b90b-0c48ed7ae650"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이 예제를 바탕으로 문자열의 `.replace()` 기능을 유추하라. 유추한 기능을\n",
"chatGPT로 확인하라.\n",
"\n",
"(풀이)\n",
"\n",
"생략\n",
"\n",
"`5`. 리스트자료형의 `+`와 `*` 연산을 이용하여 아래와 같은 list를\n",
"생성하라."
],
"id": "7e2f7dbb-4d3b-4a20-aaa1-cf9abdec68fa"
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]"
],
"id": "1b12f310-68f3-4802-b1cc-0d8eeb054b9c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**hint:** 아래의 코드를 관찰해보세요"
],
"id": "23130d87-63d3-4f64-bd88-5a92b3c83cb4"
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"[1]*1+[2]*2 "
],
"id": "913145d4-8ad0-4931-a05e-40ab6e1dfdbb"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "2d6879c2-3ece-47dd-a94a-53e24f7dab46"
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"[1]*1+[2]*2+[3]*3+[4]*4+[5]*5"
],
"id": "ef82a2b1-4735-4b83-b698-15c2ece51ddf"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`6`. 넘파이를 이용하여 아래와 같은 결과가 나오도록 코드가 작성하라.\n",
"\n",
"$$\\begin{bmatrix}\n",
"1 \\\\\n",
"2 \n",
"\\end{bmatrix} + \\begin{bmatrix}\n",
"11 \\\\\n",
"22 \n",
"\\end{bmatrix} = \n",
"\\begin{bmatrix}\n",
"12 \\\\\n",
"24\n",
"\\end{bmatrix}$$\n",
"\n",
"(풀이)"
],
"id": "d17183f4-db69-4281-be18-c21ec43746b1"
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
],
"id": "1c0cd7ae-57b8-4886-9bfb-1223d0072668"
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"np.array([1,2]) + np.array([11,22]) # 풀이1"
],
"id": "ff469bf6-c6c2-4ed7-a5bf-5d55c716223c"
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"np.array([[1],[2]]) + np.array([[11],[22]]) # 풀이2"
],
"id": "0a10a948-8dd7-4467-a527-196d2fb64705"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> 풀이1,2 모두 좋음\n",
"\n",
"`7`. 아래와 같은 벡터 ${\\boldsymbol a}$가 있다고 하자.\n",
"\n",
"$$\\boldsymbol{a}=\n",
"\\begin{bmatrix}\n",
"12 \\\\\n",
"24\n",
"\\end{bmatrix}$$\n",
"\n",
"넘파이를 이용하여 아래의 결과가 나오게 하는 코드를 작성하라.\n",
"\n",
"$$2\\boldsymbol{a}=\n",
"\\begin{bmatrix}\n",
"24 \\\\\n",
"48\n",
"\\end{bmatrix}$$\n",
"\n",
"(풀이)"
],
"id": "b4ef0a57-eb49-4948-89d5-56eb06a596b8"
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"2*np.array([12,24]) # 풀이1"
],
"id": "5427cb4d-7174-45e1-951e-7f46b561c52c"
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"2*np.array([[12],[24]]) # 풀이2"
],
"id": "5c40032f-c00d-4e2c-996f-e03798b4b30c"
}
],
"nbformat": 4,
"nbformat_minor": 5,
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"codemirror_mode": {
"name": "ipython",
"version": "3"
},
"file_extension": ".py",
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
}
}