From 2afe8f41dafd5cb0f84c2e18ffc4ba0da3a2d4b3 Mon Sep 17 00:00:00 2001
From: lenndev <37726487+lenn-dev@users.noreply.github.com>
Date: Wed, 4 Jun 2025 23:38:12 +1000
Subject: [PATCH 1/2] =?UTF-8?q?Colab=EC=9D=84=20=ED=86=B5=ED=95=B4=20?=
=?UTF-8?q?=EC=83=9D=EC=84=B1=EB=90=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"python_basic_\355\230\201\355\216\234.ipynb" | 2338 +++++++++++++++++
1 file changed, 2338 insertions(+)
create mode 100644 "python_basic_\355\230\201\355\216\234.ipynb"
diff --git "a/python_basic_\355\230\201\355\216\234.ipynb" "b/python_basic_\355\230\201\355\216\234.ipynb"
new file mode 100644
index 00000000..08cc5672
--- /dev/null
+++ "b/python_basic_\355\230\201\355\216\234.ipynb"
@@ -0,0 +1,2338 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "authorship_tag": "ABX9TyMUbwcJWMo9i/zl3oIrsAqi",
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 47,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "8-VKAKAX6Iae",
+ "outputId": "f7430158-fe5e-4631-cda5-feafbb9d0549"
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Hello Wolrd\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(\"Hello Wolrd\")\n",
+ "# ctrl +enter 현재 셀 실행\n",
+ "# 주석처리\n",
+ "# ctrl + / 로 셀처리 토글\n",
+ "# shift + enter 실행하고 다음셀로 넘어감\n",
+ "# alt + enter 실행하고 밑에 셀을 새로 생성\n",
+ "# esc 하고 방향키 위아래로 셀 왔다갔다 엔터치면 다시 코드 작성\n",
+ "# esc + 이 모드에서 a하면 위에 셀 b는 아래셀 생성\n",
+ "# ctrl + m, d하면 셀 삭제 z 하면 셀 수준 실행 취소 ctrl+shift+y는 셀 수준 다시 실행\n",
+ "# ctrl + z, ctrl+shift+z, ctrl+c, ctrl+v"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [],
+ "metadata": {
+ "id": "tAmm4k44D8Ny"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "5%2 # 모듈러 연산"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "P4zQKCHLF74Y",
+ "outputId": "56976fdd-9a32-438d-aa42-5705b5e9edea"
+ },
+ "execution_count": 48,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 48
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "print(0b11) # 0b는 2진수\n",
+ "print(0xA) # 0x는 16진수\n",
+ "# print를 붙이지 않으면 마지막 열만 출력해서 다 print 해줌"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "SJ-0-dGZGNV1",
+ "outputId": "35d65a0b-e127-4369-c64a-6612e9398164"
+ },
+ "execution_count": 49,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "3\n",
+ "10\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# del a #변수 삭제"
+ ],
+ "metadata": {
+ "id": "920SK7AqG0sW"
+ },
+ "execution_count": 50,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 리스트"
+ ],
+ "metadata": {
+ "id": "RF7cIjWyrVBy"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 리스트\n",
+ "a=[]\n",
+ "a.append(3)\n",
+ "a += [1] #.append와 같은 역할\n",
+ "a += [\"ㅋㅎㅎ\"]\n",
+ "a += [2]\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "vmkugoOdRWPK",
+ "outputId": "9556cdad-04ef-4860-f1fa-d5120e4a37fd"
+ },
+ "execution_count": 51,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[3, 1, 'ㅋㅎㅎ', 2]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 실전 예: 딥러닝에서 현재 계산된 loss값을 update 해나갈때 추이를 보고 싶을 때\n",
+ "loss_history=[]\n",
+ "loss =1\n",
+ "loss_history +=[loss]#loss_history.append(loss)\n",
+ "loss =0.5\n",
+ "loss_history +=[loss]\n",
+ "loss =0.1\n",
+ "loss_history +=[loss]\n",
+ "print(loss_history)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "h2zN4XO8Rk5A",
+ "outputId": "958b2da8-56c1-41f0-f799-75ed73dd0111"
+ },
+ "execution_count": 52,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 0.5, 0.1]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Range"
+ ],
+ "metadata": {
+ "id": "h107z0U9rcs1"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [],
+ "metadata": {
+ "id": "9m18fBHUreEN"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# range 반복문 친구\n",
+ "range(10) # range(0, 10)\n",
+ "list(range(10)) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
+ "# 숫자 나열 보고 싶으면 range를 list로 바꿔줌"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "QX_IyXJ9TfCu",
+ "outputId": "499fb27c-74da-47f4-942c-6888136b9c90"
+ },
+ "execution_count": 53,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 53
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "b=list(range(1,11)) # 1부터 시작 11 미만\n",
+ "print(b)\n",
+ "c=list(range(1,11,2)) # 1부터 시작 11 미만 2간격\n",
+ "print(c)\n",
+ "print(list(range(10,0,-1)))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "lXxylxbqTWwa",
+ "outputId": "87b3daf3-c84e-4f7e-9d76-57b3a23f930b"
+ },
+ "execution_count": 54,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
+ "[1, 3, 5, 7, 9]\n",
+ "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 튜플"
+ ],
+ "metadata": {
+ "id": "ubaFbPWcrfM-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 튜플\n",
+ "a =(1,\"a\",3)\n",
+ "b=[1,2,3]\n",
+ "print(type(a))\n",
+ "print(type(b))\n",
+ "\n",
+ "b=tuple(range(10))\n",
+ "print(b)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "9_gHmocbUuKz",
+ "outputId": "8627d738-02f2-4521-85d1-47df00f24d1d"
+ },
+ "execution_count": 55,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "a= list(range(10))\n",
+ "b= tuple(range(10))\n",
+ "c= tuple(a)\n",
+ "d= list(b)\n",
+ "print(a)\n",
+ "print(b)\n",
+ "print(c)\n",
+ "print(d)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "T2XWpIx5XDVu",
+ "outputId": "2fd3e17c-4046-4a94-bfb1-bbfaeaf78095"
+ },
+ "execution_count": 56,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
+ "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n",
+ "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n",
+ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#in 있냐?\n",
+ "a=list(range(10))\n",
+ "b=tuple(range(10))\n",
+ "c=(range(10))\n",
+ "print(1 in a)\n",
+ "print(15 in a)\n",
+ "print(1 not in b)\n",
+ "print(15 in b)\n",
+ "print(5 in c)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "Cr8RLCu9a1yH",
+ "outputId": "2d0c0072-bb7b-41e9-9bca-de9ad0ddca46"
+ },
+ "execution_count": 57,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "True\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "True\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "s=\"Hello\"\n",
+ "print(\"h\" in s)\n",
+ "print(\"H\" in s)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "9YDNAx2pbVhl",
+ "outputId": "f046a2a1-731b-4f86-9c2f-319a910412b8"
+ },
+ "execution_count": 58,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "False\n",
+ "True\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 리스트 기본연산과 인덱싱"
+ ],
+ "metadata": {
+ "id": "ORf-NSfOrkkW"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 리스트 기본 연산과 인덱싱\n",
+ "a =[1,2,3]\n",
+ "b=a+a\n",
+ "c=a*3\n",
+ "d=a*(12//4) #d=a*(12/4) 는 3.0 이므로 에러\n",
+ "d=a*int(12/4)\n",
+ "print(a)\n",
+ "print(b)\n",
+ "print(c)\n",
+ "print(d)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "ZPzpV7YQbgvw",
+ "outputId": "e758159e-d2bb-450c-e295-39e5224f233a"
+ },
+ "execution_count": 59,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2, 3]\n",
+ "[1, 2, 3, 1, 2, 3]\n",
+ "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
+ "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# len 길이\n",
+ "a=[1,2,3]\n",
+ "print(len(a))\n",
+ "s=\"hello\" # 문자열이 들어오면 문자의 갯수\n",
+ "s2=[\"hello\"] # 리스트가 들어오면 리스트 항목의 갯수\n",
+ "print(len(s))\n",
+ "print(len(s2))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "yeiEOKDrcOkq",
+ "outputId": "2a07e732-24af-4040-fd21-f415ba1326c1"
+ },
+ "execution_count": 60,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "3\n",
+ "5\n",
+ "1\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 인덱싱"
+ ],
+ "metadata": {
+ "id": "KuIu6-o4rrTl"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 인덱싱\n",
+ "a=[1,2,3]\n",
+ "print(a[0])\n",
+ "print(a[-1]) # 마지막 원소\n",
+ "print(a[-1],a[-2],a[-3])\n",
+ "\n",
+ "r=range(1,10,2)\n",
+ "print(r[0],r[1],r[2])"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "qNTbvWnDdJkS",
+ "outputId": "780e4702-1ac9-4c3f-b6df-15f885511c89"
+ },
+ "execution_count": 61,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1\n",
+ "3\n",
+ "3 2 1\n",
+ "1 3 5\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 인덱싱으로 원소값 바꿔주기\n",
+ "# 리스트만 가능하고 튜플은 불가능\n",
+ "a=[1,2,3]\n",
+ "a[0] = 3\n",
+ "a[1] = 6\n",
+ "a[2] = 9\n",
+ "print(a)\n",
+ "\n",
+ "# a=(1,2,3)\n",
+ "# a[0]=1 #immutable"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "uSMqNl3EfA19",
+ "outputId": "32ba8088-9d91-4429-ff12-d3b943e81921"
+ },
+ "execution_count": 62,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[3, 6, 9]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "a=[1,2,3]\n",
+ "del a[1]\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "XfRAz1pChM3e",
+ "outputId": "dae4cb1e-d8d8-445d-a127-5a3ccd5e12f7"
+ },
+ "execution_count": 63,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 3]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 슬라이스"
+ ],
+ "metadata": {
+ "id": "BJs6ilb4rzKG"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 굉장히 많이 쓰는 슬라이스\n",
+ "a =[1,2,3,4,5,6,7,8,9,10]\n",
+ "b=a[0:3] #0부터 3미만번째\n",
+ "print(b)\n",
+ "\n",
+ "print(a[1:3]) #1부터 3미만\n",
+ "print(a[3:-1]) #마지막원소 -1 미만까지\n",
+ "print(a[1:7:2]) #1부터 7미만 2간격\n",
+ "print(a[:5]) # 0(디폴트)~ 5미만 첫번째 비우면 처음것부터\n",
+ "print(a[5:]) # 두번째 자리 비우면 마지막(포함)까지\n",
+ "print(a[:]) # 처음부터 끝까지\n",
+ "print(a[:7:2]) # 처음부터 7미만까지 2간격\n",
+ "print(a[7::2]) # 7부터 끝까지 2간격\n",
+ "print(a[::2])# 처음부터 끝까지 2간격\n",
+ "print(a[::]) # 처음부터 끝까지 1간격 a(:)과 동일\n",
+ "b=[[1,2],[3,4]] # 리스트 안의 리스트도 적용됨\n",
+ "print(b[::])\n",
+ "print(b[:])"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "Y4ojkhLEhaaf",
+ "outputId": "471b1a8b-2a5f-40af-b650-a443398c6193"
+ },
+ "execution_count": 64,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2, 3]\n",
+ "[2, 3]\n",
+ "[4, 5, 6, 7, 8, 9]\n",
+ "[2, 4, 6]\n",
+ "[1, 2, 3, 4, 5]\n",
+ "[6, 7, 8, 9, 10]\n",
+ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
+ "[1, 3, 5, 7]\n",
+ "[8, 10]\n",
+ "[1, 3, 5, 7, 9]\n",
+ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
+ "[[1, 2], [3, 4]]\n",
+ "[[1, 2], [3, 4]]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "a=[1,2,3,4,5]\n",
+ "del a[1:3]\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "nvFA36Krhqlg",
+ "outputId": "d2dd84fd-2fb2-4983-e8ed-104f0078aa62"
+ },
+ "execution_count": 65,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 4, 5]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 딕셔너리"
+ ],
+ "metadata": {
+ "id": "vIjvJ70ur2Ud"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 딕셔너리\n",
+ "dic1 ={\"a\":1,\"b\":2,\"c\":3} # 중괄호로 표현, 콜론: 이 들어감\n",
+ "# 키와 값의 쌍 (키:값)으로 표현, (숫자,문자 모두 가능)\n",
+ "# 키: 수정못함, 튜플 가능, 리스트 불가능\n",
+ "# 값: 튜플, 리스트 가능\n",
+ "print(dic1)\n",
+ "print(dic1[\"a\"]) # 키를 통한 인덱싱을 통해 값을 확인\n",
+ "print(dic1[\"b\"])\n",
+ "print(dic1[\"c\"])\n",
+ "\n",
+ "print(len(dic1)) # 딕셔너리 길이: 키를 기준으로 세거나, 쌍으로 세거나.\n",
+ "print(dic1.keys()) # 키만 확인\n",
+ "print(dic1.values()) # 값(valu)만 확인\n",
+ "print(dic1.items()) # 쌍을 확인"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "ndGVeCdfm_Gj",
+ "outputId": "5cc86ab0-ad83-4ed0-c8c9-6b638d8c9f89"
+ },
+ "execution_count": 66,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "{'a': 1, 'b': 2, 'c': 3}\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "3\n",
+ "dict_keys(['a', 'b', 'c'])\n",
+ "dict_values([1, 2, 3])\n",
+ "dict_items([('a', 1), ('b', 2), ('c', 3)])\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 지퍼를 이용해 딕셔너리로 바꾸기"
+ ],
+ "metadata": {
+ "id": "VTfpr_Q-r6nj"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#지퍼를 이용해 키와 값을 따로 리스트로 나열해서 딕셔너리로 바꿀 수 있음\n",
+ "print(list(zip([\"a\",\"b\",\"c\"],[1,2,3])))\n",
+ "# 잘 모르겠을 때 리스트로 바꿔보면 튜플로 묶여있음\n",
+ "a = zip([\"a\",\"b\",\"c\"],[1,2,3])\n",
+ "print(dict(a))\n",
+ "# 튜플로 묶여있는 a를 딕셔너리로 바꿈."
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "pePTLs4FoMHq",
+ "outputId": "7e0efdbd-6ea6-4797-d8f5-f4db8d270ec1"
+ },
+ "execution_count": 67,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[('a', 1), ('b', 2), ('c', 3)]\n",
+ "{'a': 1, 'b': 2, 'c': 3}\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "dic2 = {'a': 1, 'b': 2, 'c': 3, 'd':4}\n",
+ "dic2['a']=100; dic2['b']=200; dic2['c']=300; dic2['d']=400;\n",
+ "print(dic2)\n",
+ "dic2['e'] ='새로운 키와 밸류'\n",
+ "print(dic2)\n",
+ "print(\"a\" in dic2)\n",
+ "print(100 in dic2) # 키를 기준으로 찾으므로 False"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "TrMXa04Lp8Fq",
+ "outputId": "113a419e-e9a2-4d53-c3d4-ac8203a880bd"
+ },
+ "execution_count": 68,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "{'a': 100, 'b': 200, 'c': 300, 'd': 400}\n",
+ "{'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': '새로운 키와 밸류'}\n",
+ "True\n",
+ "False\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 조건문"
+ ],
+ "metadata": {
+ "id": "Q8DgglObr_rT"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#조건문\n",
+ "a=5\n",
+ "print(a>3)\n",
+ "print(a<3)\n",
+ "print(a==5)\n",
+ "\n",
+ "# 들여쓰기 tab\n",
+ "# 들여쓰기 되돌리기 shift+tab\n",
+ "\n",
+ "print(bool(0))\n",
+ "print(bool(\"hello\"))\n",
+ "print(bool(\"\"))\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "collapsed": true,
+ "id": "nQKf-ZX7tDR1",
+ "outputId": "f8a73d7e-733d-40b4-9300-68b36fa9b37a"
+ },
+ "execution_count": 69,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "True\n",
+ "False\n",
+ "True\n",
+ "False\n",
+ "True\n",
+ "False\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 반복문"
+ ],
+ "metadata": {
+ "id": "7ktI_PlJsBZ5"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 반복문\n",
+ "\n",
+ "for i in range(10,0,-1):\n",
+ " print(i)\n",
+ "\n",
+ "for i in reversed(range(10)):\n",
+ " print(i)\n",
+ "\n",
+ "# 모르겠을 때 리스트로 바꿔서 보기\n",
+ "list(reversed(range(10)))\n",
+ "\n",
+ "# 리스트에 대해서 반복문 실행도 가능\n",
+ "a=[10,20,30,40,50]\n",
+ "for i in a:\n",
+ " print(i)\n",
+ "\n",
+ "# 보통 이런식으로 해서 쓰는데 이건 파이썬스럽지 않음\n",
+ "for i in range(len(a)):\n",
+ " print(a[i])\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "collapsed": true,
+ "id": "P7SLTiVFVUZo",
+ "outputId": "bf90cde4-4b56-46e6-dd1c-352975f3c4f9"
+ },
+ "execution_count": 70,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "10\n",
+ "9\n",
+ "8\n",
+ "7\n",
+ "6\n",
+ "5\n",
+ "4\n",
+ "3\n",
+ "2\n",
+ "1\n",
+ "9\n",
+ "8\n",
+ "7\n",
+ "6\n",
+ "5\n",
+ "4\n",
+ "3\n",
+ "2\n",
+ "1\n",
+ "0\n",
+ "10\n",
+ "20\n",
+ "30\n",
+ "40\n",
+ "50\n",
+ "10\n",
+ "20\n",
+ "30\n",
+ "40\n",
+ "50\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 문자 리스트에 대해서도 실행 가능\n",
+ "a=[\"apple\",\"banana\",\"carrot\",\"berries\",\"melon\"]\n",
+ "for i in a:\n",
+ " print(i)\n",
+ "\n",
+ "b=\"hello\"\n",
+ "for i in b:\n",
+ " print(i)\n",
+ "\n",
+ "b=\"hello\"\n",
+ "for i in b:\n",
+ " print(i,end=\"\")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "xHtQnMjmXTlf",
+ "outputId": "7013e17a-853e-4cd7-97cc-5b5b2e181aaa"
+ },
+ "execution_count": 71,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "apple\n",
+ "banana\n",
+ "carrot\n",
+ "berries\n",
+ "melon\n",
+ "h\n",
+ "e\n",
+ "l\n",
+ "l\n",
+ "o\n",
+ "hello"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 이중for문"
+ ],
+ "metadata": {
+ "id": "vfFcY8gesFqB"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#이중 for문\n",
+ "for i in range(3):\n",
+ " for j in range(3):\n",
+ " print(i,j)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "Er5-AhaFXsLs",
+ "outputId": "4afdfd6e-bc6d-47f1-8af3-e6c566cf9167"
+ },
+ "execution_count": 72,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0 0\n",
+ "0 1\n",
+ "0 2\n",
+ "1 0\n",
+ "1 1\n",
+ "1 2\n",
+ "2 0\n",
+ "2 1\n",
+ "2 2\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## while 문"
+ ],
+ "metadata": {
+ "id": "67ELjEWvsIVJ"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "i=0\n",
+ "while i<5:\n",
+ " print(i)\n",
+ " i +=1\n",
+ "print(\"i=\",i)\n",
+ "\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "bz_7YowBa2l2",
+ "outputId": "85d9d079-ec9b-4eb5-abfe-6ce5ce9d7b96"
+ },
+ "execution_count": 73,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "i= 5\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "i=0\n",
+ "while True:\n",
+ " print(i)\n",
+ " i += 1\n",
+ " if i==5: # 무한루프 벗어나기 위한 조건\n",
+ " break"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "ph9XNNPncA6l",
+ "outputId": "b6fc74f0-eca4-4937-f09d-ec32403ee111"
+ },
+ "execution_count": 74,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "break 사용 (디버깅용)"
+ ],
+ "metadata": {
+ "id": "mabpaPmlsM1J"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "EPOCH= 10 # 딥러닝 용어\n",
+ "# epoch is one complete pass through the entire training dataset\n",
+ "# by the neural network\n",
+ "for i in range(EPOCH):\n",
+ " print(i)\n",
+ " break # 디버깅용: 한번만 실행\n",
+ "\n",
+ "# next(iter())"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "-bIijmpffw3p",
+ "outputId": "878d583e-d912-469f-f5a6-193e652cdeda"
+ },
+ "execution_count": 75,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "continue 로 건너뛰기"
+ ],
+ "metadata": {
+ "id": "4dlJn0EWsRqf"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "for i in range(10):\n",
+ " if i%2==0:\n",
+ " continue # 건너뛰기\n",
+ " # continue 만나면 그 밑으로는 실행하지 않고,하던 반복 마저 실행\n",
+ " # 따라서 여기서는 짝수일 때는 print(i)실행없이 반복문 돌아감\n",
+ " print(i)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "B_xgRs8HhYc9",
+ "outputId": "a22cb51a-f037-43b9-bd85-3dfff88d7dd4"
+ },
+ "execution_count": 77,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1\n",
+ "3\n",
+ "5\n",
+ "7\n",
+ "9\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "a=0\n",
+ "while a<10:\n",
+ " a +=1\n",
+ " if a%2==0:\n",
+ " continue\n",
+ " print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "KJ4LrNxtif41",
+ "outputId": "7189c9ff-332d-41eb-b91e-5e7dba1cc969"
+ },
+ "execution_count": 79,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1\n",
+ "3\n",
+ "5\n",
+ "7\n",
+ "9\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 인덱스가 0부터 시작해서 순서가 1부터 매칭이 안되서 i+1 해줌.\n",
+ "a=[1,2,3,4,5]\n",
+ "for i in range(len(a)):\n",
+ " print(i+1,\"번째에\",a[i],\"가 들어있어요\")\n",
+ "\n",
+ "\n",
+ "j=1\n",
+ "for i in a:\n",
+ " print(j,\"번째에\",i,\"가 들어있어요\")\n",
+ " j += 1\n",
+ "\n",
+ "# 1부터 인덱싱을 위한 j=1 설정하는 방법\n",
+ "\n",
+ "# 파이썬에서는 이를 보완하기 위한 enumerate 이 있음"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "4Zw_BApzkJfX",
+ "outputId": "94e27aab-3b39-45e7-afdc-778232759289"
+ },
+ "execution_count": 87,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1 번째에 1 가 들어있어요\n",
+ "2 번째에 2 가 들어있어요\n",
+ "3 번째에 3 가 들어있어요\n",
+ "4 번째에 4 가 들어있어요\n",
+ "5 번째에 5 가 들어있어요\n",
+ "1 번째에 1 가 들어있어요\n",
+ "2 번째에 2 가 들어있어요\n",
+ "3 번째에 3 가 들어있어요\n",
+ "4 번째에 4 가 들어있어요\n",
+ "5 번째에 5 가 들어있어요\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Enumerate 인덱스와 값을 쌍으로 받고, 인덱스 시작을 1로 지정가능"
+ ],
+ "metadata": {
+ "id": "xhaBK9npsYsO"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# enumerate 은 쌍으로 받음\n",
+ "a=[1,2,3,4,5]\n",
+ "for i,val in enumerate(a):\n",
+ " print(i,\"번째\",val,\"이 들어있어요\")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "uuV_jN92m3z3",
+ "outputId": "2f363453-28f4-476f-daab-76b956913361"
+ },
+ "execution_count": 89,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "0 번째 1 이 들어있어요\n",
+ "1 번째 2 이 들어있어요\n",
+ "2 번째 3 이 들어있어요\n",
+ "3 번째 4 이 들어있어요\n",
+ "4 번째 5 이 들어있어요\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# enumerate 은 인덱스와 값을 쌍으로 줌\n",
+ "# 인덱스 시작을 1로 지정해줄수도 있음\n",
+ "a=[1,2,3,4,5]\n",
+ "for i,val in enumerate(a,start=1):\n",
+ " print(i,\"번째\",val,\"이 들어있어요\")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "N5uB7nTjnX7Y",
+ "outputId": "2a0f6d19-9375-4136-beb0-23f3e2dd4280"
+ },
+ "execution_count": 90,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1 번째 1 이 들어있어요\n",
+ "2 번째 2 이 들어있어요\n",
+ "3 번째 3 이 들어있어요\n",
+ "4 번째 4 이 들어있어요\n",
+ "5 번째 5 이 들어있어요\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 별찍기 반복문 과제"
+ ],
+ "metadata": {
+ "id": "pyNu_pooshiX"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 별찍기\n",
+ "#1 내코드\n",
+ "for i in range(5):\n",
+ " print(\"*\"*i)\n",
+ "\n",
+ "#2\n",
+ "a =\"\"\n",
+ "for i in range(4):\n",
+ " a += \"*\"\n",
+ " print(a)\n",
+ "\n",
+ "#3\n",
+ "for i in range(5):\n",
+ " for j in range(i):\n",
+ " print(\"*\",end=\"\")\n",
+ " print()\n",
+ "#4\n",
+ "i=1\n",
+ "while i<5:\n",
+ " print(\"*\"*i)\n",
+ " i+=1\n",
+ "\n",
+ "#5\n",
+ "i=\"*\"\n",
+ "while True:\n",
+ " print(i)\n",
+ " i +=\"*\"\n",
+ " if i == '*****':\n",
+ " break;\n",
+ "\n",
+ "#6\n",
+ "i=\"*\"\n",
+ "while i !=\"*****\":\n",
+ " print(i)\n",
+ " i += '*'"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "collapsed": true,
+ "id": "moe3hBzJnzkz",
+ "outputId": "ed30283f-52f5-46af-c002-11dd09960fdd"
+ },
+ "execution_count": 101,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "\n",
+ "*\n",
+ "**\n",
+ "***\n",
+ "****\n",
+ "*\n",
+ "**\n",
+ "***\n",
+ "****\n",
+ "\n",
+ "*\n",
+ "**\n",
+ "***\n",
+ "****\n",
+ "*\n",
+ "**\n",
+ "***\n",
+ "****\n",
+ "*\n",
+ "**\n",
+ "***\n",
+ "****\n",
+ "*\n",
+ "**\n",
+ "***\n",
+ "****\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 리스트 심화"
+ ],
+ "metadata": {
+ "id": "iglELbX_spC5"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 리스트 심화\n",
+ "# 두개의 원소를 넣어주고 싶을 때 한개씩 밖에 안되서\n",
+ "# 리스트로 묶어서 넣으면 그야말로, 하나의 원소로 묶여서 들어감\n",
+ "a =[1,2,3]\n",
+ "a.append([4,5])\n",
+ "print(a,'len(a)',len(a))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "-J-46EifoMLP",
+ "outputId": "86fcd269-62f4-40b1-b968-77d928189c37"
+ },
+ "execution_count": 102,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2, 3, [4, 5]] len(a) 4\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# extend() 쓰면, 개별 원소로 넣어주기 가능\n",
+ "a =[1,2,3]\n",
+ "a.extend([4,5])\n",
+ "print(a,'len(a)',len(a))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "TPOotrv6tdER",
+ "outputId": "348be2e3-5783-4941-9a96-0926b70ee322"
+ },
+ "execution_count": 104,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2, 3, 4, 5] len(a) 5\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 더 간단하게 += 리스트로 넣어주면 됨\n",
+ "a =[1,2,3]\n",
+ "a += [4,5]\n",
+ "print(a,'len(a)',len(a))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "UWorD31lt8rE",
+ "outputId": "3d5f4ad7-03f9-4908-fd92-0bde1e98ffce"
+ },
+ "execution_count": 105,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2, 3, 4, 5] len(a) 5\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ " #insert 중간에 넣고 싶을 때\n",
+ "a =[1,2,3]\n",
+ "a.insert(1,100)\n",
+ "print(a,'len(a)',len(a))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "ewdWDx2LuQVi",
+ "outputId": "23a812cb-2223-47f8-b1f1-ae12eff48733"
+ },
+ "execution_count": 107,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 100, 2, 3] len(a) 4\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "a =[1,2,3]\n",
+ "a.insert(1,[100,200])\n",
+ "print(a,'len(a)',len(a))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "OH5K2bRguzZ5",
+ "outputId": "c160db96-1fbd-453f-bef9-428fbbc4a9d7"
+ },
+ "execution_count": 108,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, [100, 200], 2, 3] len(a) 4\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ " #insert() 사용하지 않고 insert하기\n",
+ " a=[1,2,3]\n",
+ " b=[100,200]\n",
+ "# a[1:2]=b # 1의 자리에 값을 무시하고 b가 삽입\n",
+ " a[1:1]=b # 이 성질 이용해 1의 자리에 값 살리고 삽입(꼼수)\n",
+ " print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "5frhQt6Wu6Pg",
+ "outputId": "baba33b6-534d-4704-d30c-e7a19ad33c26"
+ },
+ "execution_count": 114,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 100, 200, 2, 3]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# pop\n",
+ "# 스택처럼 맨 오른쪽 부터 하나씩 out\n",
+ "a=[1,2,3]\n",
+ "a.pop()\n",
+ "print(a)\n",
+ "a.pop()\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "SiYBbWW0yfYU",
+ "outputId": "46fd52fb-1a19-4b4c-facb-41e3518253ad"
+ },
+ "execution_count": 117,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2]\n",
+ "[1]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# pop\n",
+ "# 특정 인덱스 pop\n",
+ "a=[1,2,3]\n",
+ "a.pop(1)\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "EgjvMa1t0DpV",
+ "outputId": "0217e0bb-5e9e-4638-e1a4-702035d89b80"
+ },
+ "execution_count": 118,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 3]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#remove 는 인덱스가 아닌 인수값을 찾아서 제거.\n",
+ "# 중복인 경우 왼쪽부터 제거\n",
+ "a=[1,2,3,4,3,5,3]\n",
+ "a.remove(3)\n",
+ "print(a)\n",
+ "a.remove(3)\n",
+ "print(a)\n",
+ "a.remove(3)\n",
+ "print(a)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "8gJ_ctql0pOr",
+ "outputId": "d600c9ac-31b8-4551-fc56-7698023a82de"
+ },
+ "execution_count": 123,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2, 4, 3, 5, 3]\n",
+ "[1, 2, 4, 5, 3]\n",
+ "[1, 2, 4, 5]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 특정값이 위치한 처음 인덱스 찾기 index()\n",
+ "a=[1,2,3,4,3,5,3]\n",
+ "a.index(3)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "WPebCgwr1Td4",
+ "outputId": "31bd639f-8957-4a9d-bf55-9b90f4ab2835"
+ },
+ "execution_count": 125,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "2"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 125
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 특정 값의 갯수 세기 count()\n",
+ "a=[1,2,3,4,3,5,3]\n",
+ "a.count(3)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "1eiMSOTE1jZ5",
+ "outputId": "ff29551a-8710-4468-dbba-9e692d3bcdc1"
+ },
+ "execution_count": 127,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "3"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 127
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 원소 순서 뒤집기 reverse()\n",
+ "a=[1,2,3,4,5]\n",
+ "a.reverse()\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "AUS_9p1A11dm",
+ "outputId": "bfab39df-9c3f-4343-e445-08fbcb1d9167"
+ },
+ "execution_count": 128,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[5, 4, 3, 2, 1]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 정렬 sort\n",
+ "# 오름차순\n",
+ "a=[1,5,1,2,6,1,7]\n",
+ "a.sort()\n",
+ "print(a)\n",
+ "\n",
+ "# 내림차순\n",
+ "a.sort(reverse=True)\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "jwmX_BgA2P5n",
+ "outputId": "437082d3-1183-47c2-f592-422d179c831a"
+ },
+ "execution_count": 130,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 1, 1, 2, 5, 6, 7]\n",
+ "[7, 6, 5, 2, 1, 1, 1]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 리스트 내용 모두 제거하기\n",
+ "# del은 정의 자체를 지움\n",
+ "a=[1,2,3]\n",
+ "a.clear()\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "EhTXGP1l2hmD",
+ "outputId": "1b28d451-e952-461d-f8ce-a7bbc277cff0"
+ },
+ "execution_count": 131,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# b[1]값만 바꾸고 싶은데 b=a 로 인해 a[1]도 바뀜\n",
+ "a=[1,2,3]\n",
+ "b=a\n",
+ "b[1]=10\n",
+ "print(\"a=\",a,\"b=\",b)\n",
+ "# b가 a 주소값 같이 씀 동일함\n",
+ "a is b"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "zrVyH4xD25pQ",
+ "outputId": "60f7ead3-9d4d-4db9-961d-d77ab78ca254"
+ },
+ "execution_count": 138,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "a= [1, 10, 3] b= [1, 10, 3]\n"
+ ]
+ },
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 138
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# copy()를 이용해 정의해야 b만 바뀜\n",
+ "a=[1,2,3]\n",
+ "b=a.copy()\n",
+ "b[1]=10\n",
+ "print(\"a=\",a,\"b=\",b)\n",
+ "# b가 a의 값만 복사하고 주소 다름\n",
+ "a is b"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "q2CfepCk3jhg",
+ "outputId": "3bc9d942-8dbf-44cf-c3b0-140d39a0f7b2"
+ },
+ "execution_count": 139,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "a= [1, 2, 3] b= [1, 10, 3]\n"
+ ]
+ },
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 139
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "a=[1,2,3]\n",
+ "print(min(a))\n",
+ "print(max(a))\n",
+ "print(sum(a))\n",
+ "print(round(1.53423423,3))# 소수점 셋째자리까지 반올림"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "59kA7pcJ406Y",
+ "outputId": "60b12dad-418a-459c-c8b3-0b150b9fabcf"
+ },
+ "execution_count": 141,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1\n",
+ "3\n",
+ "6\n",
+ "1.534\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 리스트 표현식: 코드 깔끔해짐"
+ ],
+ "metadata": {
+ "id": "EWlsT9R15Q82"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#range(5) 안의 값들을 i라는 이름으로 불러오고\n",
+ "# i 값으로 리스트를 만들어줌\n",
+ "a=[i for i in range(5)]\n",
+ "print(a)\n",
+ "\n",
+ "# 아래 코드를 보면 계산된 i+5 값들이 쌓이는 걸 알 수 있음\n",
+ "a=[i+5 for i in range(5)]\n",
+ "print(a)\n",
+ "\n",
+ "# 리스트는 쌓는것이므로 각각의 원소에 연산을 원하면 리스트 표현식 사용하면됨\n",
+ "[1,2,3,4,5]+[5]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "c-no9t-P5Lq8",
+ "outputId": "3757844d-ba52-44c1-94ad-0089f250f0a1"
+ },
+ "execution_count": 144,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[0, 1, 2, 3, 4]\n",
+ "[5, 6, 7, 8, 9]\n"
+ ]
+ },
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "[1, 2, 3, 4, 5, 5]"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 144
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 리스트 표현식 이중for문도 가능함\n",
+ "a = [i-j for i in range(1,5)\n",
+ " for j in range(1,5)]\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "NFw14pzi5D-W",
+ "outputId": "3668844f-e928-4e9f-97fa-bdb44ef7cf34"
+ },
+ "execution_count": 148,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[0, -1, -2, -3, 1, 0, -1, -2, 2, 1, 0, -1, 3, 2, 1, 0]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 위의 이중반복문 표현식이 기본 for문으로 표현하면 아래와 같음\n",
+ "a=[]\n",
+ "for i in range(1,5):\n",
+ " for j in range(1,5):\n",
+ " a += [i-j]\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "mZRhb-Fe7Re9",
+ "outputId": "c27b9caf-c16a-432d-c349-f140311c7738"
+ },
+ "execution_count": 149,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[0, -1, -2, -3, 1, 0, -1, -2, 2, 1, 0, -1, 3, 2, 1, 0]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 표현식에 for, if문 같이 쓰기\n",
+ "# 짝수일 경우만 출력\n",
+ "a=[i for i in range(10) if i%2==0]\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "xHdrW0O18W_t",
+ "outputId": "9da36511-b57b-472f-ac9a-2ac89c7a6f27"
+ },
+ "execution_count": 151,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[0, 2, 4, 6, 8]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 아래 4줄의 코드를 위의 표현식 한줄로 간단히 표현함\n",
+ "a=[i]\n",
+ "for i in range(10):\n",
+ " if i%2 ==0\n",
+ " a += [i]\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "id": "aXSr794e9-T5"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "##리스트 속에 리스트"
+ ],
+ "metadata": {
+ "id": "an_2pqyB-va0"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 리스트 안의 리스트로 하나의 요소로 묶어서 생각\n",
+ "a=[[1,2,3],[4,5,6]]\n",
+ "print(a,\"len(a)=\", len(a))\n",
+ "\n",
+ "# 따라서 인덱싱도 마찬가지\n",
+ "print(a[0])\n",
+ "print(a[1])\n",
+ "print(a[0][0])\n",
+ "print(a[0][1])\n",
+ "print(a[0][2])\n",
+ "print(a[1][0])\n",
+ "\n",
+ "a[1][1] =500\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "1xKUv2Yq-y3E",
+ "outputId": "0ceb6404-a04a-4bc7-ab0b-87537af4eedb"
+ },
+ "execution_count": 155,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[[1, 2, 3], [4, 5, 6]] len(a)= 2\n",
+ "[1, 2, 3]\n",
+ "[4, 5, 6]\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "[[1, 2, 3], [4, 500, 6]]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 이중 리스트에 for문 돌릴때 출력\n",
+ "a=[[1,2,3],[4,5,6]]\n",
+ "for i in a:\n",
+ " print(i)\n",
+ "print()\n",
+ "\n",
+ "# 리스트를 분해해서 나열하고 싶을 때\n",
+ "# 각각의 리스트 갯수가 다를 때\n",
+ "#1\n",
+ "for i in a:\n",
+ " for j in i:\n",
+ " print(j,end=\" \")\n",
+ " print()\n",
+ "print()\n",
+ "\n",
+ "#2 복수개를 받아서 출력\n",
+ "# 각각의 리스트 개수가 같을때\n",
+ "for x,y,z in a:\n",
+ " print(x,y,z)\n",
+ "print()\n",
+ "\n",
+ "#3 제일 파이썬스럽지 않은 코드\n",
+ "# 각각의 리스트 갯수가 다를 때\n",
+ "for i in range(len(a)):\n",
+ " for j in range(len(a[i])):\n",
+ " print(a[i][j],end=\" \")\n",
+ " print()"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "eT8vE6G-_Cr8",
+ "outputId": "c2396a3b-3386-4bdc-aff2-64f1d94d33ad"
+ },
+ "execution_count": 162,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2, 3]\n",
+ "[4, 5, 6]\n",
+ "1 2 3 \n",
+ "4 5 6 \n",
+ "1 2 3\n",
+ "4 5 6\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 빈 리스트 0으로 초기화하기\n",
+ "a=[]\n",
+ "for i in range(5):\n",
+ " a.append(0)\n",
+ "print(a)\n",
+ "\n",
+ "# 빈리스트에 2개원소로 하는 3개의 리스트 0으로 초기화\n",
+ "a=[]\n",
+ "for i in range(3):\n",
+ " line=[]\n",
+ " for j in range(2):\n",
+ " line += [0]\n",
+ " a += [line]\n",
+ "print(a)\n",
+ "\n",
+ "# 표현식으로 나타내기\n",
+ "# 이중 리스트를 만들어서 직관적으로\n",
+ "a =[[0 for i in range(2)]for j in range(3)]\n",
+ "print(a)\n",
+ "\n",
+ "# 인덱싱으로 추출이 필요없을 때 _로 반복만 해도 됨\n",
+ "a =[[0 for _ in range(2)]for _ in range(3)]\n",
+ "print(a)\n",
+ "\n",
+ "#\n",
+ "a= [[0]*2 for _ in range(3)]\n",
+ "print(a)\n",
+ "\n",
+ "#\n",
+ "a =[[0]*2]*3\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "JcdyuM_h_53g",
+ "outputId": "d076a81a-85cd-46f2-aa7f-2d0189e57726"
+ },
+ "execution_count": 183,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[0, 0, 0, 0, 0]\n",
+ "[[0, 0], [0, 0], [0, 0]]\n",
+ "[[0, 0], [0, 0], [0, 0]]\n",
+ "[[0, 0], [0, 0], [0, 0]]\n",
+ "[[0, 0], [0, 0], [0, 0]]\n",
+ "[[0, 0], [0, 0], [0, 0]]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 일률적이지 않은 리스트 갯수로 이루어진 리스트 만들기\n",
+ "a=[3,1,3,2]\n",
+ "b=[]\n",
+ "for i in a:\n",
+ " line=[]\n",
+ " for j in range(i):\n",
+ " line+=[0]\n",
+ " b+=[line]\n",
+ "print(b)\n",
+ "\n",
+ "# 표현식 : 나중에 이런 표현식이 훨씬 간단하고 파워풀해짐\n",
+ "a=[3,1,3,2]\n",
+ "a=[[0]*i for i in a]\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "eWuwZ2a0DxDq",
+ "outputId": "eb4c7a4c-6697-449a-820e-f5ee24ebdde3"
+ },
+ "execution_count": 187,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[[0, 0, 0], [0], [0, 0, 0], [0, 0]]\n",
+ "[[0, 0, 0], [0], [0, 0, 0], [0, 0]]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# 이중리스트일 때 카피는?\n",
+ "a=[[1,2],[3,4]]\n",
+ "b=a.copy()\n",
+ "b[0][0]=100\n",
+ "print(b)\n",
+ "print(a)\n",
+ "# copy를 사용해도 a,b 둘다 바뀌어 있음\n",
+ "# deepcopy를 사용해야함"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "aeB6eBF-Izo8",
+ "outputId": "947019b4-c69a-49f7-bed0-4455227efddc"
+ },
+ "execution_count": 190,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[[100, 2], [3, 4]]\n",
+ "[[100, 2], [3, 4]]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# deepcopy()\n",
+ "# 리스트 안의 리스트까지 깊이 복사해줌\n",
+ "import copy # deepcopy()를 사용하기 위한 library import\n",
+ "a=[[1,2],[3,4]]\n",
+ "b=copy.deepcopy(a)\n",
+ "b[0][0]=100\n",
+ "print(b)\n",
+ "print(a)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "xSYn0EAQJUnr",
+ "outputId": "9cbd4a68-a6bf-4d23-aa0d-8b8a60f898f5"
+ },
+ "execution_count": 192,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[[100, 2], [3, 4]]\n",
+ "[[1, 2], [3, 4]]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## 헷갈리는 리스트 표현식 ->앞에서부터 써내려가면서 for문 만들기\n"
+ ],
+ "metadata": {
+ "id": "4gp04sCUJ09g"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "-kwu1JXCJu7o"
+ },
+ "execution_count": null,
+ "outputs": []
+ }
+ ]
+}
\ No newline at end of file
From 023e55bea7015e97a5ad1c97fe4530f3c041f33e Mon Sep 17 00:00:00 2001
From: lenndev <37726487+lenn-dev@users.noreply.github.com>
Date: Wed, 4 Jun 2025 23:38:55 +1000
Subject: [PATCH 2/2] =?UTF-8?q?Delete=20python=5Fbasic=5F=ED=98=81?=
=?UTF-8?q?=ED=8E=9C.ipynb?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: lenndev <37726487+lenn-dev@users.noreply.github.com>
---
"python_basic_\355\230\201\355\216\234.ipynb" | 2338 -----------------
1 file changed, 2338 deletions(-)
delete mode 100644 "python_basic_\355\230\201\355\216\234.ipynb"
diff --git "a/python_basic_\355\230\201\355\216\234.ipynb" "b/python_basic_\355\230\201\355\216\234.ipynb"
deleted file mode 100644
index 08cc5672..00000000
--- "a/python_basic_\355\230\201\355\216\234.ipynb"
+++ /dev/null
@@ -1,2338 +0,0 @@
-{
- "nbformat": 4,
- "nbformat_minor": 0,
- "metadata": {
- "colab": {
- "provenance": [],
- "authorship_tag": "ABX9TyMUbwcJWMo9i/zl3oIrsAqi",
- "include_colab_link": true
- },
- "kernelspec": {
- "name": "python3",
- "display_name": "Python 3"
- },
- "language_info": {
- "name": "python"
- }
- },
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "view-in-github",
- "colab_type": "text"
- },
- "source": [
- "
"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 47,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "8-VKAKAX6Iae",
- "outputId": "f7430158-fe5e-4631-cda5-feafbb9d0549"
- },
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "Hello Wolrd\n"
- ]
- }
- ],
- "source": [
- "print(\"Hello Wolrd\")\n",
- "# ctrl +enter 현재 셀 실행\n",
- "# 주석처리\n",
- "# ctrl + / 로 셀처리 토글\n",
- "# shift + enter 실행하고 다음셀로 넘어감\n",
- "# alt + enter 실행하고 밑에 셀을 새로 생성\n",
- "# esc 하고 방향키 위아래로 셀 왔다갔다 엔터치면 다시 코드 작성\n",
- "# esc + 이 모드에서 a하면 위에 셀 b는 아래셀 생성\n",
- "# ctrl + m, d하면 셀 삭제 z 하면 셀 수준 실행 취소 ctrl+shift+y는 셀 수준 다시 실행\n",
- "# ctrl + z, ctrl+shift+z, ctrl+c, ctrl+v"
- ]
- },
- {
- "cell_type": "markdown",
- "source": [],
- "metadata": {
- "id": "tAmm4k44D8Ny"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "5%2 # 모듈러 연산"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "P4zQKCHLF74Y",
- "outputId": "56976fdd-9a32-438d-aa42-5705b5e9edea"
- },
- "execution_count": 48,
- "outputs": [
- {
- "output_type": "execute_result",
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "metadata": {},
- "execution_count": 48
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "print(0b11) # 0b는 2진수\n",
- "print(0xA) # 0x는 16진수\n",
- "# print를 붙이지 않으면 마지막 열만 출력해서 다 print 해줌"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "SJ-0-dGZGNV1",
- "outputId": "35d65a0b-e127-4369-c64a-6612e9398164"
- },
- "execution_count": 49,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "3\n",
- "10\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# del a #변수 삭제"
- ],
- "metadata": {
- "id": "920SK7AqG0sW"
- },
- "execution_count": 50,
- "outputs": []
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 리스트"
- ],
- "metadata": {
- "id": "RF7cIjWyrVBy"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 리스트\n",
- "a=[]\n",
- "a.append(3)\n",
- "a += [1] #.append와 같은 역할\n",
- "a += [\"ㅋㅎㅎ\"]\n",
- "a += [2]\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "vmkugoOdRWPK",
- "outputId": "9556cdad-04ef-4860-f1fa-d5120e4a37fd"
- },
- "execution_count": 51,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[3, 1, 'ㅋㅎㅎ', 2]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 실전 예: 딥러닝에서 현재 계산된 loss값을 update 해나갈때 추이를 보고 싶을 때\n",
- "loss_history=[]\n",
- "loss =1\n",
- "loss_history +=[loss]#loss_history.append(loss)\n",
- "loss =0.5\n",
- "loss_history +=[loss]\n",
- "loss =0.1\n",
- "loss_history +=[loss]\n",
- "print(loss_history)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "h2zN4XO8Rk5A",
- "outputId": "958b2da8-56c1-41f0-f799-75ed73dd0111"
- },
- "execution_count": 52,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 0.5, 0.1]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## Range"
- ],
- "metadata": {
- "id": "h107z0U9rcs1"
- }
- },
- {
- "cell_type": "markdown",
- "source": [],
- "metadata": {
- "id": "9m18fBHUreEN"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# range 반복문 친구\n",
- "range(10) # range(0, 10)\n",
- "list(range(10)) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
- "# 숫자 나열 보고 싶으면 range를 list로 바꿔줌"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "QX_IyXJ9TfCu",
- "outputId": "499fb27c-74da-47f4-942c-6888136b9c90"
- },
- "execution_count": 53,
- "outputs": [
- {
- "output_type": "execute_result",
- "data": {
- "text/plain": [
- "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
- ]
- },
- "metadata": {},
- "execution_count": 53
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "b=list(range(1,11)) # 1부터 시작 11 미만\n",
- "print(b)\n",
- "c=list(range(1,11,2)) # 1부터 시작 11 미만 2간격\n",
- "print(c)\n",
- "print(list(range(10,0,-1)))"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "lXxylxbqTWwa",
- "outputId": "87b3daf3-c84e-4f7e-9d76-57b3a23f930b"
- },
- "execution_count": 54,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
- "[1, 3, 5, 7, 9]\n",
- "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 튜플"
- ],
- "metadata": {
- "id": "ubaFbPWcrfM-"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 튜플\n",
- "a =(1,\"a\",3)\n",
- "b=[1,2,3]\n",
- "print(type(a))\n",
- "print(type(b))\n",
- "\n",
- "b=tuple(range(10))\n",
- "print(b)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "9_gHmocbUuKz",
- "outputId": "8627d738-02f2-4521-85d1-47df00f24d1d"
- },
- "execution_count": 55,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "\n",
- "\n",
- "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "a= list(range(10))\n",
- "b= tuple(range(10))\n",
- "c= tuple(a)\n",
- "d= list(b)\n",
- "print(a)\n",
- "print(b)\n",
- "print(c)\n",
- "print(d)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "T2XWpIx5XDVu",
- "outputId": "2fd3e17c-4046-4a94-bfb1-bbfaeaf78095"
- },
- "execution_count": 56,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
- "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n",
- "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n",
- "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "#in 있냐?\n",
- "a=list(range(10))\n",
- "b=tuple(range(10))\n",
- "c=(range(10))\n",
- "print(1 in a)\n",
- "print(15 in a)\n",
- "print(1 not in b)\n",
- "print(15 in b)\n",
- "print(5 in c)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "Cr8RLCu9a1yH",
- "outputId": "2d0c0072-bb7b-41e9-9bca-de9ad0ddca46"
- },
- "execution_count": 57,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "True\n",
- "False\n",
- "False\n",
- "False\n",
- "True\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "s=\"Hello\"\n",
- "print(\"h\" in s)\n",
- "print(\"H\" in s)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "9YDNAx2pbVhl",
- "outputId": "f046a2a1-731b-4f86-9c2f-319a910412b8"
- },
- "execution_count": 58,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "False\n",
- "True\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 리스트 기본연산과 인덱싱"
- ],
- "metadata": {
- "id": "ORf-NSfOrkkW"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 리스트 기본 연산과 인덱싱\n",
- "a =[1,2,3]\n",
- "b=a+a\n",
- "c=a*3\n",
- "d=a*(12//4) #d=a*(12/4) 는 3.0 이므로 에러\n",
- "d=a*int(12/4)\n",
- "print(a)\n",
- "print(b)\n",
- "print(c)\n",
- "print(d)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "ZPzpV7YQbgvw",
- "outputId": "e758159e-d2bb-450c-e295-39e5224f233a"
- },
- "execution_count": 59,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2, 3]\n",
- "[1, 2, 3, 1, 2, 3]\n",
- "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
- "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# len 길이\n",
- "a=[1,2,3]\n",
- "print(len(a))\n",
- "s=\"hello\" # 문자열이 들어오면 문자의 갯수\n",
- "s2=[\"hello\"] # 리스트가 들어오면 리스트 항목의 갯수\n",
- "print(len(s))\n",
- "print(len(s2))"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "yeiEOKDrcOkq",
- "outputId": "2a07e732-24af-4040-fd21-f415ba1326c1"
- },
- "execution_count": 60,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "3\n",
- "5\n",
- "1\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 인덱싱"
- ],
- "metadata": {
- "id": "KuIu6-o4rrTl"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 인덱싱\n",
- "a=[1,2,3]\n",
- "print(a[0])\n",
- "print(a[-1]) # 마지막 원소\n",
- "print(a[-1],a[-2],a[-3])\n",
- "\n",
- "r=range(1,10,2)\n",
- "print(r[0],r[1],r[2])"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "qNTbvWnDdJkS",
- "outputId": "780e4702-1ac9-4c3f-b6df-15f885511c89"
- },
- "execution_count": 61,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "1\n",
- "3\n",
- "3 2 1\n",
- "1 3 5\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 인덱싱으로 원소값 바꿔주기\n",
- "# 리스트만 가능하고 튜플은 불가능\n",
- "a=[1,2,3]\n",
- "a[0] = 3\n",
- "a[1] = 6\n",
- "a[2] = 9\n",
- "print(a)\n",
- "\n",
- "# a=(1,2,3)\n",
- "# a[0]=1 #immutable"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "uSMqNl3EfA19",
- "outputId": "32ba8088-9d91-4429-ff12-d3b943e81921"
- },
- "execution_count": 62,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[3, 6, 9]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "a=[1,2,3]\n",
- "del a[1]\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "XfRAz1pChM3e",
- "outputId": "dae4cb1e-d8d8-445d-a127-5a3ccd5e12f7"
- },
- "execution_count": 63,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 3]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 슬라이스"
- ],
- "metadata": {
- "id": "BJs6ilb4rzKG"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 굉장히 많이 쓰는 슬라이스\n",
- "a =[1,2,3,4,5,6,7,8,9,10]\n",
- "b=a[0:3] #0부터 3미만번째\n",
- "print(b)\n",
- "\n",
- "print(a[1:3]) #1부터 3미만\n",
- "print(a[3:-1]) #마지막원소 -1 미만까지\n",
- "print(a[1:7:2]) #1부터 7미만 2간격\n",
- "print(a[:5]) # 0(디폴트)~ 5미만 첫번째 비우면 처음것부터\n",
- "print(a[5:]) # 두번째 자리 비우면 마지막(포함)까지\n",
- "print(a[:]) # 처음부터 끝까지\n",
- "print(a[:7:2]) # 처음부터 7미만까지 2간격\n",
- "print(a[7::2]) # 7부터 끝까지 2간격\n",
- "print(a[::2])# 처음부터 끝까지 2간격\n",
- "print(a[::]) # 처음부터 끝까지 1간격 a(:)과 동일\n",
- "b=[[1,2],[3,4]] # 리스트 안의 리스트도 적용됨\n",
- "print(b[::])\n",
- "print(b[:])"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "Y4ojkhLEhaaf",
- "outputId": "471b1a8b-2a5f-40af-b650-a443398c6193"
- },
- "execution_count": 64,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2, 3]\n",
- "[2, 3]\n",
- "[4, 5, 6, 7, 8, 9]\n",
- "[2, 4, 6]\n",
- "[1, 2, 3, 4, 5]\n",
- "[6, 7, 8, 9, 10]\n",
- "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
- "[1, 3, 5, 7]\n",
- "[8, 10]\n",
- "[1, 3, 5, 7, 9]\n",
- "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
- "[[1, 2], [3, 4]]\n",
- "[[1, 2], [3, 4]]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "a=[1,2,3,4,5]\n",
- "del a[1:3]\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "nvFA36Krhqlg",
- "outputId": "d2dd84fd-2fb2-4983-e8ed-104f0078aa62"
- },
- "execution_count": 65,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 4, 5]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 딕셔너리"
- ],
- "metadata": {
- "id": "vIjvJ70ur2Ud"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 딕셔너리\n",
- "dic1 ={\"a\":1,\"b\":2,\"c\":3} # 중괄호로 표현, 콜론: 이 들어감\n",
- "# 키와 값의 쌍 (키:값)으로 표현, (숫자,문자 모두 가능)\n",
- "# 키: 수정못함, 튜플 가능, 리스트 불가능\n",
- "# 값: 튜플, 리스트 가능\n",
- "print(dic1)\n",
- "print(dic1[\"a\"]) # 키를 통한 인덱싱을 통해 값을 확인\n",
- "print(dic1[\"b\"])\n",
- "print(dic1[\"c\"])\n",
- "\n",
- "print(len(dic1)) # 딕셔너리 길이: 키를 기준으로 세거나, 쌍으로 세거나.\n",
- "print(dic1.keys()) # 키만 확인\n",
- "print(dic1.values()) # 값(valu)만 확인\n",
- "print(dic1.items()) # 쌍을 확인"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "ndGVeCdfm_Gj",
- "outputId": "5cc86ab0-ad83-4ed0-c8c9-6b638d8c9f89"
- },
- "execution_count": 66,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "{'a': 1, 'b': 2, 'c': 3}\n",
- "1\n",
- "2\n",
- "3\n",
- "3\n",
- "dict_keys(['a', 'b', 'c'])\n",
- "dict_values([1, 2, 3])\n",
- "dict_items([('a', 1), ('b', 2), ('c', 3)])\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 지퍼를 이용해 딕셔너리로 바꾸기"
- ],
- "metadata": {
- "id": "VTfpr_Q-r6nj"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "#지퍼를 이용해 키와 값을 따로 리스트로 나열해서 딕셔너리로 바꿀 수 있음\n",
- "print(list(zip([\"a\",\"b\",\"c\"],[1,2,3])))\n",
- "# 잘 모르겠을 때 리스트로 바꿔보면 튜플로 묶여있음\n",
- "a = zip([\"a\",\"b\",\"c\"],[1,2,3])\n",
- "print(dict(a))\n",
- "# 튜플로 묶여있는 a를 딕셔너리로 바꿈."
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "pePTLs4FoMHq",
- "outputId": "7e0efdbd-6ea6-4797-d8f5-f4db8d270ec1"
- },
- "execution_count": 67,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[('a', 1), ('b', 2), ('c', 3)]\n",
- "{'a': 1, 'b': 2, 'c': 3}\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "dic2 = {'a': 1, 'b': 2, 'c': 3, 'd':4}\n",
- "dic2['a']=100; dic2['b']=200; dic2['c']=300; dic2['d']=400;\n",
- "print(dic2)\n",
- "dic2['e'] ='새로운 키와 밸류'\n",
- "print(dic2)\n",
- "print(\"a\" in dic2)\n",
- "print(100 in dic2) # 키를 기준으로 찾으므로 False"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "TrMXa04Lp8Fq",
- "outputId": "113a419e-e9a2-4d53-c3d4-ac8203a880bd"
- },
- "execution_count": 68,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "{'a': 100, 'b': 200, 'c': 300, 'd': 400}\n",
- "{'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': '새로운 키와 밸류'}\n",
- "True\n",
- "False\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 조건문"
- ],
- "metadata": {
- "id": "Q8DgglObr_rT"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "#조건문\n",
- "a=5\n",
- "print(a>3)\n",
- "print(a<3)\n",
- "print(a==5)\n",
- "\n",
- "# 들여쓰기 tab\n",
- "# 들여쓰기 되돌리기 shift+tab\n",
- "\n",
- "print(bool(0))\n",
- "print(bool(\"hello\"))\n",
- "print(bool(\"\"))\n"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "collapsed": true,
- "id": "nQKf-ZX7tDR1",
- "outputId": "f8a73d7e-733d-40b4-9300-68b36fa9b37a"
- },
- "execution_count": 69,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "True\n",
- "False\n",
- "True\n",
- "False\n",
- "True\n",
- "False\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 반복문"
- ],
- "metadata": {
- "id": "7ktI_PlJsBZ5"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 반복문\n",
- "\n",
- "for i in range(10,0,-1):\n",
- " print(i)\n",
- "\n",
- "for i in reversed(range(10)):\n",
- " print(i)\n",
- "\n",
- "# 모르겠을 때 리스트로 바꿔서 보기\n",
- "list(reversed(range(10)))\n",
- "\n",
- "# 리스트에 대해서 반복문 실행도 가능\n",
- "a=[10,20,30,40,50]\n",
- "for i in a:\n",
- " print(i)\n",
- "\n",
- "# 보통 이런식으로 해서 쓰는데 이건 파이썬스럽지 않음\n",
- "for i in range(len(a)):\n",
- " print(a[i])\n"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "collapsed": true,
- "id": "P7SLTiVFVUZo",
- "outputId": "bf90cde4-4b56-46e6-dd1c-352975f3c4f9"
- },
- "execution_count": 70,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "10\n",
- "9\n",
- "8\n",
- "7\n",
- "6\n",
- "5\n",
- "4\n",
- "3\n",
- "2\n",
- "1\n",
- "9\n",
- "8\n",
- "7\n",
- "6\n",
- "5\n",
- "4\n",
- "3\n",
- "2\n",
- "1\n",
- "0\n",
- "10\n",
- "20\n",
- "30\n",
- "40\n",
- "50\n",
- "10\n",
- "20\n",
- "30\n",
- "40\n",
- "50\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 문자 리스트에 대해서도 실행 가능\n",
- "a=[\"apple\",\"banana\",\"carrot\",\"berries\",\"melon\"]\n",
- "for i in a:\n",
- " print(i)\n",
- "\n",
- "b=\"hello\"\n",
- "for i in b:\n",
- " print(i)\n",
- "\n",
- "b=\"hello\"\n",
- "for i in b:\n",
- " print(i,end=\"\")"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "xHtQnMjmXTlf",
- "outputId": "7013e17a-853e-4cd7-97cc-5b5b2e181aaa"
- },
- "execution_count": 71,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "apple\n",
- "banana\n",
- "carrot\n",
- "berries\n",
- "melon\n",
- "h\n",
- "e\n",
- "l\n",
- "l\n",
- "o\n",
- "hello"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 이중for문"
- ],
- "metadata": {
- "id": "vfFcY8gesFqB"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "#이중 for문\n",
- "for i in range(3):\n",
- " for j in range(3):\n",
- " print(i,j)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "Er5-AhaFXsLs",
- "outputId": "4afdfd6e-bc6d-47f1-8af3-e6c566cf9167"
- },
- "execution_count": 72,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "0 0\n",
- "0 1\n",
- "0 2\n",
- "1 0\n",
- "1 1\n",
- "1 2\n",
- "2 0\n",
- "2 1\n",
- "2 2\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## while 문"
- ],
- "metadata": {
- "id": "67ELjEWvsIVJ"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "i=0\n",
- "while i<5:\n",
- " print(i)\n",
- " i +=1\n",
- "print(\"i=\",i)\n",
- "\n"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "bz_7YowBa2l2",
- "outputId": "85d9d079-ec9b-4eb5-abfe-6ce5ce9d7b96"
- },
- "execution_count": 73,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "0\n",
- "1\n",
- "2\n",
- "3\n",
- "4\n",
- "i= 5\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "i=0\n",
- "while True:\n",
- " print(i)\n",
- " i += 1\n",
- " if i==5: # 무한루프 벗어나기 위한 조건\n",
- " break"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "ph9XNNPncA6l",
- "outputId": "b6fc74f0-eca4-4937-f09d-ec32403ee111"
- },
- "execution_count": 74,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "0\n",
- "1\n",
- "2\n",
- "3\n",
- "4\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "break 사용 (디버깅용)"
- ],
- "metadata": {
- "id": "mabpaPmlsM1J"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "EPOCH= 10 # 딥러닝 용어\n",
- "# epoch is one complete pass through the entire training dataset\n",
- "# by the neural network\n",
- "for i in range(EPOCH):\n",
- " print(i)\n",
- " break # 디버깅용: 한번만 실행\n",
- "\n",
- "# next(iter())"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "-bIijmpffw3p",
- "outputId": "878d583e-d912-469f-f5a6-193e652cdeda"
- },
- "execution_count": 75,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "0\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "continue 로 건너뛰기"
- ],
- "metadata": {
- "id": "4dlJn0EWsRqf"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "for i in range(10):\n",
- " if i%2==0:\n",
- " continue # 건너뛰기\n",
- " # continue 만나면 그 밑으로는 실행하지 않고,하던 반복 마저 실행\n",
- " # 따라서 여기서는 짝수일 때는 print(i)실행없이 반복문 돌아감\n",
- " print(i)\n"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "B_xgRs8HhYc9",
- "outputId": "a22cb51a-f037-43b9-bd85-3dfff88d7dd4"
- },
- "execution_count": 77,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "1\n",
- "3\n",
- "5\n",
- "7\n",
- "9\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "a=0\n",
- "while a<10:\n",
- " a +=1\n",
- " if a%2==0:\n",
- " continue\n",
- " print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "KJ4LrNxtif41",
- "outputId": "7189c9ff-332d-41eb-b91e-5e7dba1cc969"
- },
- "execution_count": 79,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "1\n",
- "3\n",
- "5\n",
- "7\n",
- "9\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 인덱스가 0부터 시작해서 순서가 1부터 매칭이 안되서 i+1 해줌.\n",
- "a=[1,2,3,4,5]\n",
- "for i in range(len(a)):\n",
- " print(i+1,\"번째에\",a[i],\"가 들어있어요\")\n",
- "\n",
- "\n",
- "j=1\n",
- "for i in a:\n",
- " print(j,\"번째에\",i,\"가 들어있어요\")\n",
- " j += 1\n",
- "\n",
- "# 1부터 인덱싱을 위한 j=1 설정하는 방법\n",
- "\n",
- "# 파이썬에서는 이를 보완하기 위한 enumerate 이 있음"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "4Zw_BApzkJfX",
- "outputId": "94e27aab-3b39-45e7-afdc-778232759289"
- },
- "execution_count": 87,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "1 번째에 1 가 들어있어요\n",
- "2 번째에 2 가 들어있어요\n",
- "3 번째에 3 가 들어있어요\n",
- "4 번째에 4 가 들어있어요\n",
- "5 번째에 5 가 들어있어요\n",
- "1 번째에 1 가 들어있어요\n",
- "2 번째에 2 가 들어있어요\n",
- "3 번째에 3 가 들어있어요\n",
- "4 번째에 4 가 들어있어요\n",
- "5 번째에 5 가 들어있어요\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## Enumerate 인덱스와 값을 쌍으로 받고, 인덱스 시작을 1로 지정가능"
- ],
- "metadata": {
- "id": "xhaBK9npsYsO"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# enumerate 은 쌍으로 받음\n",
- "a=[1,2,3,4,5]\n",
- "for i,val in enumerate(a):\n",
- " print(i,\"번째\",val,\"이 들어있어요\")"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "uuV_jN92m3z3",
- "outputId": "2f363453-28f4-476f-daab-76b956913361"
- },
- "execution_count": 89,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "0 번째 1 이 들어있어요\n",
- "1 번째 2 이 들어있어요\n",
- "2 번째 3 이 들어있어요\n",
- "3 번째 4 이 들어있어요\n",
- "4 번째 5 이 들어있어요\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# enumerate 은 인덱스와 값을 쌍으로 줌\n",
- "# 인덱스 시작을 1로 지정해줄수도 있음\n",
- "a=[1,2,3,4,5]\n",
- "for i,val in enumerate(a,start=1):\n",
- " print(i,\"번째\",val,\"이 들어있어요\")"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "N5uB7nTjnX7Y",
- "outputId": "2a0f6d19-9375-4136-beb0-23f3e2dd4280"
- },
- "execution_count": 90,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "1 번째 1 이 들어있어요\n",
- "2 번째 2 이 들어있어요\n",
- "3 번째 3 이 들어있어요\n",
- "4 번째 4 이 들어있어요\n",
- "5 번째 5 이 들어있어요\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 별찍기 반복문 과제"
- ],
- "metadata": {
- "id": "pyNu_pooshiX"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 별찍기\n",
- "#1 내코드\n",
- "for i in range(5):\n",
- " print(\"*\"*i)\n",
- "\n",
- "#2\n",
- "a =\"\"\n",
- "for i in range(4):\n",
- " a += \"*\"\n",
- " print(a)\n",
- "\n",
- "#3\n",
- "for i in range(5):\n",
- " for j in range(i):\n",
- " print(\"*\",end=\"\")\n",
- " print()\n",
- "#4\n",
- "i=1\n",
- "while i<5:\n",
- " print(\"*\"*i)\n",
- " i+=1\n",
- "\n",
- "#5\n",
- "i=\"*\"\n",
- "while True:\n",
- " print(i)\n",
- " i +=\"*\"\n",
- " if i == '*****':\n",
- " break;\n",
- "\n",
- "#6\n",
- "i=\"*\"\n",
- "while i !=\"*****\":\n",
- " print(i)\n",
- " i += '*'"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "collapsed": true,
- "id": "moe3hBzJnzkz",
- "outputId": "ed30283f-52f5-46af-c002-11dd09960fdd"
- },
- "execution_count": 101,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "\n",
- "*\n",
- "**\n",
- "***\n",
- "****\n",
- "*\n",
- "**\n",
- "***\n",
- "****\n",
- "\n",
- "*\n",
- "**\n",
- "***\n",
- "****\n",
- "*\n",
- "**\n",
- "***\n",
- "****\n",
- "*\n",
- "**\n",
- "***\n",
- "****\n",
- "*\n",
- "**\n",
- "***\n",
- "****\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 리스트 심화"
- ],
- "metadata": {
- "id": "iglELbX_spC5"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 리스트 심화\n",
- "# 두개의 원소를 넣어주고 싶을 때 한개씩 밖에 안되서\n",
- "# 리스트로 묶어서 넣으면 그야말로, 하나의 원소로 묶여서 들어감\n",
- "a =[1,2,3]\n",
- "a.append([4,5])\n",
- "print(a,'len(a)',len(a))"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "-J-46EifoMLP",
- "outputId": "86fcd269-62f4-40b1-b968-77d928189c37"
- },
- "execution_count": 102,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2, 3, [4, 5]] len(a) 4\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# extend() 쓰면, 개별 원소로 넣어주기 가능\n",
- "a =[1,2,3]\n",
- "a.extend([4,5])\n",
- "print(a,'len(a)',len(a))"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "TPOotrv6tdER",
- "outputId": "348be2e3-5783-4941-9a96-0926b70ee322"
- },
- "execution_count": 104,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2, 3, 4, 5] len(a) 5\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 더 간단하게 += 리스트로 넣어주면 됨\n",
- "a =[1,2,3]\n",
- "a += [4,5]\n",
- "print(a,'len(a)',len(a))"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "UWorD31lt8rE",
- "outputId": "3d5f4ad7-03f9-4908-fd92-0bde1e98ffce"
- },
- "execution_count": 105,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2, 3, 4, 5] len(a) 5\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- " #insert 중간에 넣고 싶을 때\n",
- "a =[1,2,3]\n",
- "a.insert(1,100)\n",
- "print(a,'len(a)',len(a))"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "ewdWDx2LuQVi",
- "outputId": "23a812cb-2223-47f8-b1f1-ae12eff48733"
- },
- "execution_count": 107,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 100, 2, 3] len(a) 4\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "a =[1,2,3]\n",
- "a.insert(1,[100,200])\n",
- "print(a,'len(a)',len(a))"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "OH5K2bRguzZ5",
- "outputId": "c160db96-1fbd-453f-bef9-428fbbc4a9d7"
- },
- "execution_count": 108,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, [100, 200], 2, 3] len(a) 4\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- " #insert() 사용하지 않고 insert하기\n",
- " a=[1,2,3]\n",
- " b=[100,200]\n",
- "# a[1:2]=b # 1의 자리에 값을 무시하고 b가 삽입\n",
- " a[1:1]=b # 이 성질 이용해 1의 자리에 값 살리고 삽입(꼼수)\n",
- " print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "5frhQt6Wu6Pg",
- "outputId": "baba33b6-534d-4704-d30c-e7a19ad33c26"
- },
- "execution_count": 114,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 100, 200, 2, 3]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# pop\n",
- "# 스택처럼 맨 오른쪽 부터 하나씩 out\n",
- "a=[1,2,3]\n",
- "a.pop()\n",
- "print(a)\n",
- "a.pop()\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "SiYBbWW0yfYU",
- "outputId": "46fd52fb-1a19-4b4c-facb-41e3518253ad"
- },
- "execution_count": 117,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2]\n",
- "[1]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# pop\n",
- "# 특정 인덱스 pop\n",
- "a=[1,2,3]\n",
- "a.pop(1)\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "EgjvMa1t0DpV",
- "outputId": "0217e0bb-5e9e-4638-e1a4-702035d89b80"
- },
- "execution_count": 118,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 3]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "#remove 는 인덱스가 아닌 인수값을 찾아서 제거.\n",
- "# 중복인 경우 왼쪽부터 제거\n",
- "a=[1,2,3,4,3,5,3]\n",
- "a.remove(3)\n",
- "print(a)\n",
- "a.remove(3)\n",
- "print(a)\n",
- "a.remove(3)\n",
- "print(a)\n"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "8gJ_ctql0pOr",
- "outputId": "d600c9ac-31b8-4551-fc56-7698023a82de"
- },
- "execution_count": 123,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2, 4, 3, 5, 3]\n",
- "[1, 2, 4, 5, 3]\n",
- "[1, 2, 4, 5]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 특정값이 위치한 처음 인덱스 찾기 index()\n",
- "a=[1,2,3,4,3,5,3]\n",
- "a.index(3)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "WPebCgwr1Td4",
- "outputId": "31bd639f-8957-4a9d-bf55-9b90f4ab2835"
- },
- "execution_count": 125,
- "outputs": [
- {
- "output_type": "execute_result",
- "data": {
- "text/plain": [
- "2"
- ]
- },
- "metadata": {},
- "execution_count": 125
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 특정 값의 갯수 세기 count()\n",
- "a=[1,2,3,4,3,5,3]\n",
- "a.count(3)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "1eiMSOTE1jZ5",
- "outputId": "ff29551a-8710-4468-dbba-9e692d3bcdc1"
- },
- "execution_count": 127,
- "outputs": [
- {
- "output_type": "execute_result",
- "data": {
- "text/plain": [
- "3"
- ]
- },
- "metadata": {},
- "execution_count": 127
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 원소 순서 뒤집기 reverse()\n",
- "a=[1,2,3,4,5]\n",
- "a.reverse()\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "AUS_9p1A11dm",
- "outputId": "bfab39df-9c3f-4343-e445-08fbcb1d9167"
- },
- "execution_count": 128,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[5, 4, 3, 2, 1]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 정렬 sort\n",
- "# 오름차순\n",
- "a=[1,5,1,2,6,1,7]\n",
- "a.sort()\n",
- "print(a)\n",
- "\n",
- "# 내림차순\n",
- "a.sort(reverse=True)\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "jwmX_BgA2P5n",
- "outputId": "437082d3-1183-47c2-f592-422d179c831a"
- },
- "execution_count": 130,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 1, 1, 2, 5, 6, 7]\n",
- "[7, 6, 5, 2, 1, 1, 1]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 리스트 내용 모두 제거하기\n",
- "# del은 정의 자체를 지움\n",
- "a=[1,2,3]\n",
- "a.clear()\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "EhTXGP1l2hmD",
- "outputId": "1b28d451-e952-461d-f8ce-a7bbc277cff0"
- },
- "execution_count": 131,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# b[1]값만 바꾸고 싶은데 b=a 로 인해 a[1]도 바뀜\n",
- "a=[1,2,3]\n",
- "b=a\n",
- "b[1]=10\n",
- "print(\"a=\",a,\"b=\",b)\n",
- "# b가 a 주소값 같이 씀 동일함\n",
- "a is b"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "zrVyH4xD25pQ",
- "outputId": "60f7ead3-9d4d-4db9-961d-d77ab78ca254"
- },
- "execution_count": 138,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "a= [1, 10, 3] b= [1, 10, 3]\n"
- ]
- },
- {
- "output_type": "execute_result",
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "metadata": {},
- "execution_count": 138
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# copy()를 이용해 정의해야 b만 바뀜\n",
- "a=[1,2,3]\n",
- "b=a.copy()\n",
- "b[1]=10\n",
- "print(\"a=\",a,\"b=\",b)\n",
- "# b가 a의 값만 복사하고 주소 다름\n",
- "a is b"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "q2CfepCk3jhg",
- "outputId": "3bc9d942-8dbf-44cf-c3b0-140d39a0f7b2"
- },
- "execution_count": 139,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "a= [1, 2, 3] b= [1, 10, 3]\n"
- ]
- },
- {
- "output_type": "execute_result",
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "metadata": {},
- "execution_count": 139
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "a=[1,2,3]\n",
- "print(min(a))\n",
- "print(max(a))\n",
- "print(sum(a))\n",
- "print(round(1.53423423,3))# 소수점 셋째자리까지 반올림"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "59kA7pcJ406Y",
- "outputId": "60b12dad-418a-459c-c8b3-0b150b9fabcf"
- },
- "execution_count": 141,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "1\n",
- "3\n",
- "6\n",
- "1.534\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 리스트 표현식: 코드 깔끔해짐"
- ],
- "metadata": {
- "id": "EWlsT9R15Q82"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "#range(5) 안의 값들을 i라는 이름으로 불러오고\n",
- "# i 값으로 리스트를 만들어줌\n",
- "a=[i for i in range(5)]\n",
- "print(a)\n",
- "\n",
- "# 아래 코드를 보면 계산된 i+5 값들이 쌓이는 걸 알 수 있음\n",
- "a=[i+5 for i in range(5)]\n",
- "print(a)\n",
- "\n",
- "# 리스트는 쌓는것이므로 각각의 원소에 연산을 원하면 리스트 표현식 사용하면됨\n",
- "[1,2,3,4,5]+[5]"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "c-no9t-P5Lq8",
- "outputId": "3757844d-ba52-44c1-94ad-0089f250f0a1"
- },
- "execution_count": 144,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[0, 1, 2, 3, 4]\n",
- "[5, 6, 7, 8, 9]\n"
- ]
- },
- {
- "output_type": "execute_result",
- "data": {
- "text/plain": [
- "[1, 2, 3, 4, 5, 5]"
- ]
- },
- "metadata": {},
- "execution_count": 144
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 리스트 표현식 이중for문도 가능함\n",
- "a = [i-j for i in range(1,5)\n",
- " for j in range(1,5)]\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "NFw14pzi5D-W",
- "outputId": "3668844f-e928-4e9f-97fa-bdb44ef7cf34"
- },
- "execution_count": 148,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[0, -1, -2, -3, 1, 0, -1, -2, 2, 1, 0, -1, 3, 2, 1, 0]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 위의 이중반복문 표현식이 기본 for문으로 표현하면 아래와 같음\n",
- "a=[]\n",
- "for i in range(1,5):\n",
- " for j in range(1,5):\n",
- " a += [i-j]\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "mZRhb-Fe7Re9",
- "outputId": "c27b9caf-c16a-432d-c349-f140311c7738"
- },
- "execution_count": 149,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[0, -1, -2, -3, 1, 0, -1, -2, 2, 1, 0, -1, 3, 2, 1, 0]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 표현식에 for, if문 같이 쓰기\n",
- "# 짝수일 경우만 출력\n",
- "a=[i for i in range(10) if i%2==0]\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "xHdrW0O18W_t",
- "outputId": "9da36511-b57b-472f-ac9a-2ac89c7a6f27"
- },
- "execution_count": 151,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[0, 2, 4, 6, 8]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 아래 4줄의 코드를 위의 표현식 한줄로 간단히 표현함\n",
- "a=[i]\n",
- "for i in range(10):\n",
- " if i%2 ==0\n",
- " a += [i]\n",
- "print(a)"
- ],
- "metadata": {
- "id": "aXSr794e9-T5"
- },
- "execution_count": null,
- "outputs": []
- },
- {
- "cell_type": "markdown",
- "source": [
- "##리스트 속에 리스트"
- ],
- "metadata": {
- "id": "an_2pqyB-va0"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# 리스트 안의 리스트로 하나의 요소로 묶어서 생각\n",
- "a=[[1,2,3],[4,5,6]]\n",
- "print(a,\"len(a)=\", len(a))\n",
- "\n",
- "# 따라서 인덱싱도 마찬가지\n",
- "print(a[0])\n",
- "print(a[1])\n",
- "print(a[0][0])\n",
- "print(a[0][1])\n",
- "print(a[0][2])\n",
- "print(a[1][0])\n",
- "\n",
- "a[1][1] =500\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "1xKUv2Yq-y3E",
- "outputId": "0ceb6404-a04a-4bc7-ab0b-87537af4eedb"
- },
- "execution_count": 155,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[[1, 2, 3], [4, 5, 6]] len(a)= 2\n",
- "[1, 2, 3]\n",
- "[4, 5, 6]\n",
- "1\n",
- "2\n",
- "3\n",
- "4\n",
- "[[1, 2, 3], [4, 500, 6]]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 이중 리스트에 for문 돌릴때 출력\n",
- "a=[[1,2,3],[4,5,6]]\n",
- "for i in a:\n",
- " print(i)\n",
- "print()\n",
- "\n",
- "# 리스트를 분해해서 나열하고 싶을 때\n",
- "# 각각의 리스트 갯수가 다를 때\n",
- "#1\n",
- "for i in a:\n",
- " for j in i:\n",
- " print(j,end=\" \")\n",
- " print()\n",
- "print()\n",
- "\n",
- "#2 복수개를 받아서 출력\n",
- "# 각각의 리스트 개수가 같을때\n",
- "for x,y,z in a:\n",
- " print(x,y,z)\n",
- "print()\n",
- "\n",
- "#3 제일 파이썬스럽지 않은 코드\n",
- "# 각각의 리스트 갯수가 다를 때\n",
- "for i in range(len(a)):\n",
- " for j in range(len(a[i])):\n",
- " print(a[i][j],end=\" \")\n",
- " print()"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "eT8vE6G-_Cr8",
- "outputId": "c2396a3b-3386-4bdc-aff2-64f1d94d33ad"
- },
- "execution_count": 162,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[1, 2, 3]\n",
- "[4, 5, 6]\n",
- "1 2 3 \n",
- "4 5 6 \n",
- "1 2 3\n",
- "4 5 6\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 빈 리스트 0으로 초기화하기\n",
- "a=[]\n",
- "for i in range(5):\n",
- " a.append(0)\n",
- "print(a)\n",
- "\n",
- "# 빈리스트에 2개원소로 하는 3개의 리스트 0으로 초기화\n",
- "a=[]\n",
- "for i in range(3):\n",
- " line=[]\n",
- " for j in range(2):\n",
- " line += [0]\n",
- " a += [line]\n",
- "print(a)\n",
- "\n",
- "# 표현식으로 나타내기\n",
- "# 이중 리스트를 만들어서 직관적으로\n",
- "a =[[0 for i in range(2)]for j in range(3)]\n",
- "print(a)\n",
- "\n",
- "# 인덱싱으로 추출이 필요없을 때 _로 반복만 해도 됨\n",
- "a =[[0 for _ in range(2)]for _ in range(3)]\n",
- "print(a)\n",
- "\n",
- "#\n",
- "a= [[0]*2 for _ in range(3)]\n",
- "print(a)\n",
- "\n",
- "#\n",
- "a =[[0]*2]*3\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "JcdyuM_h_53g",
- "outputId": "d076a81a-85cd-46f2-aa7f-2d0189e57726"
- },
- "execution_count": 183,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[0, 0, 0, 0, 0]\n",
- "[[0, 0], [0, 0], [0, 0]]\n",
- "[[0, 0], [0, 0], [0, 0]]\n",
- "[[0, 0], [0, 0], [0, 0]]\n",
- "[[0, 0], [0, 0], [0, 0]]\n",
- "[[0, 0], [0, 0], [0, 0]]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 일률적이지 않은 리스트 갯수로 이루어진 리스트 만들기\n",
- "a=[3,1,3,2]\n",
- "b=[]\n",
- "for i in a:\n",
- " line=[]\n",
- " for j in range(i):\n",
- " line+=[0]\n",
- " b+=[line]\n",
- "print(b)\n",
- "\n",
- "# 표현식 : 나중에 이런 표현식이 훨씬 간단하고 파워풀해짐\n",
- "a=[3,1,3,2]\n",
- "a=[[0]*i for i in a]\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "eWuwZ2a0DxDq",
- "outputId": "eb4c7a4c-6697-449a-820e-f5ee24ebdde3"
- },
- "execution_count": 187,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[[0, 0, 0], [0], [0, 0, 0], [0, 0]]\n",
- "[[0, 0, 0], [0], [0, 0, 0], [0, 0]]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# 이중리스트일 때 카피는?\n",
- "a=[[1,2],[3,4]]\n",
- "b=a.copy()\n",
- "b[0][0]=100\n",
- "print(b)\n",
- "print(a)\n",
- "# copy를 사용해도 a,b 둘다 바뀌어 있음\n",
- "# deepcopy를 사용해야함"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "aeB6eBF-Izo8",
- "outputId": "947019b4-c69a-49f7-bed0-4455227efddc"
- },
- "execution_count": 190,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[[100, 2], [3, 4]]\n",
- "[[100, 2], [3, 4]]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "code",
- "source": [
- "# deepcopy()\n",
- "# 리스트 안의 리스트까지 깊이 복사해줌\n",
- "import copy # deepcopy()를 사용하기 위한 library import\n",
- "a=[[1,2],[3,4]]\n",
- "b=copy.deepcopy(a)\n",
- "b[0][0]=100\n",
- "print(b)\n",
- "print(a)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "xSYn0EAQJUnr",
- "outputId": "9cbd4a68-a6bf-4d23-aa0d-8b8a60f898f5"
- },
- "execution_count": 192,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "[[100, 2], [3, 4]]\n",
- "[[1, 2], [3, 4]]\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "## 헷갈리는 리스트 표현식 ->앞에서부터 써내려가면서 for문 만들기\n"
- ],
- "metadata": {
- "id": "4gp04sCUJ09g"
- }
- },
- {
- "cell_type": "code",
- "source": [],
- "metadata": {
- "id": "-kwu1JXCJu7o"
- },
- "execution_count": null,
- "outputs": []
- }
- ]
-}
\ No newline at end of file