{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 01wk-2: 파이썬의 자료형 (1)\n", "\n", "최규빈 \n", "2023-03-07\n", "\n", "\n", "\n", "# 강의영상\n", "\n", "> youtube:\n", "> \n", "\n", "# Intro\n", "\n", "`-` 파이썬의 기본자료형은 int, float, bool, str, list, tuple, dict, set\n", "등이 있다.\n", "\n", "- 0차원 자료형: int, float, bool\n", "- 1차원 자료형: str, list, tuple, dict, set\n", "\n", "# int, float, bool\n", "\n", "`-` int형" ], "id": "d67ca734-eeab-413a-b1a5-28ff31df1f07" }, { "cell_type": "code", "execution_count": 14, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=100" ], "id": "fdb3309a-e078-4e2b-93e2-b6def0ef8971" }, { "cell_type": "code", "execution_count": 15, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "type(a)" ], "id": "11202d76-a8da-4e56-9125-96faaaf9102f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` float형" ], "id": "2302ea45-56e0-457b-9cb7-568aeee8e73e" }, { "cell_type": "code", "execution_count": 7, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=1.2*3\n", "a" ], "id": "b7ce3930-402d-48a9-956d-7689692d2b71" }, { "cell_type": "code", "execution_count": 19, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "type(a)" ], "id": "7da95b54-e04b-4952-b9e4-8e5a6757d525" }, { "cell_type": "code", "execution_count": 20, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a?" ], "id": "e77f2b88-f329-402b-b02b-54bd8279acf6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` bool형" ], "id": "e382fe96-c3ed-498b-a5f4-2d9f1ff7832c" }, { "cell_type": "code", "execution_count": 52, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=True ## 숫자1으로 생각할 수 있음 \n", "b=False ## 숫자0으로 생각할 수 있음" ], "id": "e4cf89f7-55ea-4291-84ab-247db0ee4890" }, { "cell_type": "code", "execution_count": 24, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "type(a)" ], "id": "10e9faf2-466c-4a02-bbda-e9b3d5f25f36" }, { "cell_type": "code", "execution_count": 25, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "type(b)" ], "id": "03c105a3-2672-4dd6-85aa-e3695eda8ddb" }, { "cell_type": "code", "execution_count": 27, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a?" ], "id": "e7c178cc-8d06-4bd1-b5e2-fdadb64e99ff" }, { "cell_type": "code", "execution_count": 28, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "b?" ], "id": "54b6caa8-4102-476c-aaf2-39b7900222ad" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` bool형의 연산" ], "id": "f92807b5-8be8-4095-beba-ac45319297da" }, { "cell_type": "code", "execution_count": 15, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=True ## 1\n", "b=False ## 0 " ], "id": "e68b2f1f-81ab-4c59-9191-faa0fc326617" }, { "cell_type": "code", "execution_count": 66, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a+b" ], "id": "11fb5fc9-4f20-4554-ae96-ae3cd86ccd46" }, { "cell_type": "code", "execution_count": 67, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a*b " ], "id": "1ed0252c-915d-4514-8a86-7c856666ec6a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` complex형" ], "id": "29aeefe7-63cd-45fa-8b26-afa95767e405" }, { "cell_type": "code", "execution_count": 20, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=1+2j\n", "b=2-2j" ], "id": "f3f89287-ca1a-440f-a5c4-3c97977e772a" }, { "cell_type": "code", "execution_count": 21, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "type(a)" ], "id": "09fad72b-86a9-452d-976d-cac70bdaa0fb" }, { "cell_type": "code", "execution_count": 22, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "type(b)" ], "id": "b734208a-0410-441d-8e7f-ea7eada9b75c" }, { "cell_type": "code", "execution_count": 23, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a?" ], "id": "feb1dd1b-a4d8-4ae2-8212-b33995f1b060" }, { "cell_type": "code", "execution_count": 24, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "b?" ], "id": "f2ae33ba-9d6d-4784-8b3f-24ed04d375ee" }, { "cell_type": "code", "execution_count": 25, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "c=a+b" ], "id": "7bd1a508-8a35-495e-88e7-85d8cab5c8ab" }, { "cell_type": "code", "execution_count": 26, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "c" ], "id": "fcddae31-b57c-4b0c-98e1-2a08ebeb7686" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 형태변환: float $\\to$ int\n", "\n", "(예시1)" ], "id": "698e78b1-1f69-461a-838d-cebf25182fee" }, { "cell_type": "code", "execution_count": 27, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=3.0\n", "type(a)" ], "id": "6f964452-1762-43f6-ba1d-74596e10f49f" }, { "cell_type": "code", "execution_count": 28, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=int(a)" ], "id": "c80b0380-031c-4c21-8606-3afffc6c1bca" }, { "cell_type": "code", "execution_count": 29, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "type(a)" ], "id": "dd9cf497-7fbf-4e39-8f44-e6a343cf0298" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시2) 이경우는 정보의 손실이 발생" ], "id": "39299891-2689-4b06-b316-ed4517c229d7" }, { "cell_type": "code", "execution_count": 30, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=3.14 \n", "int(a)" ], "id": "0f945465-ab18-4c52-92cd-6126e5b0efa2" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 형태변환: int $\\to$ float" ], "id": "c4a138ce-2c0c-4dc3-bb71-18dab315eec7" }, { "cell_type": "code", "execution_count": 39, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=3\n", "type(a)" ], "id": "3ed49aa6-8de6-45bc-9aef-f6b5523c2b72" }, { "cell_type": "code", "execution_count": 40, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=float(a)\n", "type(a)" ], "id": "ea3a05bd-567b-489d-a887-154261ce784c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 형태변환: bool $\\to$ int/float, int/float $\\to$ bool\n", "\n", "(예시1)" ], "id": "d7ddfc4c-1db3-4be9-96d7-be1ea192fc03" }, { "cell_type": "code", "execution_count": 41, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=True\n", "type(a)" ], "id": "4171066b-db9c-422f-abc8-e58545708528" }, { "cell_type": "code", "execution_count": 42, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "int(a)" ], "id": "5653b7c6-9610-4f4a-b528-ffb57b1cfb0e" }, { "cell_type": "code", "execution_count": 43, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "float(a)" ], "id": "1301ba7b-4945-4e65-9b7f-e3d1de995f41" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시2)" ], "id": "66b3be6f-9cc1-45ae-ab11-e06aee06c739" }, { "cell_type": "code", "execution_count": 59, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=1 \n", "bool(a)" ], "id": "85b738c9-3875-48bf-9aae-c49688ac0db2" }, { "cell_type": "code", "execution_count": 60, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=0\n", "bool(a)" ], "id": "13e395de-b15d-49e5-9d6b-3fc3f198f4da" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(예시3)" ], "id": "2c9054e3-6161-42fe-a71c-7eac98ab9fc4" }, { "cell_type": "code", "execution_count": 61, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=1.0\n", "bool(a)" ], "id": "508ff1d1-e626-4e76-ad52-dbb79377228b" }, { "cell_type": "code", "execution_count": 62, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "a=0.0\n", "bool(a)" ], "id": "50047512-a291-42b7-aa85-6031fb602437" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 이상한 형태변환도 가능하다. (이런것도 바꿔주나 싶은것도 바꿔줌)" ], "id": "0586695f-8fe2-496c-9e72-38065040751d" }, { "cell_type": "code", "execution_count": 75, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "bool(-3.14)" ], "id": "90d1d403-4f75-40f6-bc61-4e05f69b49e0" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 저는 이런 코드를 의도적으로 사용하지 않아요.." ], "id": "4590bc84-99a1-4914-8b41-b32f90ee72eb" }, { "cell_type": "code", "execution_count": 74, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "int(3.14)" ], "id": "3304a522-95be-4981-814f-313acd50bbf9" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 형태변환이 항상가능한것도 아님" ], "id": "807a59ad-e919-4199-b77b-03a0adc7b120" }, { "cell_type": "code", "execution_count": 78, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "float(3+0j) # 사실상 3+0j=3 이므로 float으로 형변환하면 3.0이 되어야 할 것 같은데 변환불가능하다. " ], "id": "601b94de-583e-4e6d-8953-3782f834cf03" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 암묵적형변환 (implicit)\n", "\n", "(예비학습) implicit의 의미\n", "\n", "- 추운날씨 -\\> 보일러좀 틀자! (explicit) / 오늘 날씨 좀 추운 것 같지\n", " 않아? (implicit)\n", "- 짜장면 먹을래? -\\> 싫어! (explicit) / 난 어제 짜장면 먹었는데..\n", " (implicit)\n", "\n", "(예제)" ], "id": "1885aef2-590e-48de-96e4-c451da10a146" }, { "cell_type": "code", "execution_count": 50, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "True * 1 # 1을 곱할건데 너 계속 True로 있을꺼야? " ], "id": "b8eb78ad-969a-4b23-acb6-ec2192d137ad" }, { "cell_type": "code", "execution_count": 82, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "1 * 1.0 # 1.0을 곱할건데 너 계속 int로 있을꺼야? " ], "id": "f9ec11b4-a75e-4c88-a4ad-de504ea3a556" }, { "cell_type": "code", "execution_count": 84, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "True+True # +연산을 할건데 계속 True로 있을꺼야? " ], "id": "5b72ccde-ba9e-4741-98ed-851225e16d28" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 숙제\n", "\n", "아래 강의노트의 영상 1-3을 참고하여 주피터랩을 설치하고 설치성공한\n", "화면을 스크린샷으로 LMS에 제출\n", "\n", "" ], "id": "c62bd8dd-a8f7-4c5d-94dc-593dea96ec7a" } ], "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" } } }