Class

데이터 분석과 AI의 첫걸음

I. 자료형 개요

  • 자료형은 데이터를 다루는 데 필요한 기본적인 틀을 제공

  • 주로 사용되는 자료형: 숫자, 문자열, 콜렉션 자료형(튜플, 리스트, 세트, 딕셔너리)

II. 숫자 자료형

  • 정수형 (Integer): 소수점 없는 숫자

    • 예: integer_1 = 3214, integer_2 = -128109

  • 실수형 (Float): 소수점이 있는 숫자

    • 예: float_1 = -1.986214, float_2 = 123.e2

  • 실행 예시:

  print(integer_1, type(integer_1))  # 3214 <class 'int'>
  print(float_2, type(float_2))  # 12300.0 <class 'float'>

III. 문자열

  • 문자열(String): 문자와 기호를 포함하는 자료형

  • 확장 문자 (Escape sequence): 특정 문자를 표현하기 위해 사용 (", \ 등)

    • 예: string_5 = "This is String! \"따옴표\" 기호:!@#$%^"

  • 실행 예시:

  print(string_5)  # This is String! "따옴표" 기호:!@#$%^  

IV. 콜렉션 자료형

  • 튜플 (Tuple): 수정 불가능한 순서가 있는 데이터 목록

    • 예: tuple_1 = (1, 2, 3, 4, 5)

    • 값 변경 시 오류 발생: tuple_1[2] = 100 - TypeError

  • 세트 (Set): 중복된 값을 허용하지 않으며, 순서가 중요하지 않음

    • 예: set_1 = {1, 2, 3, '가', '나', '다'}

  • 리스트 (List): 변경 가능하고, 매우 활용성이 좋은 컬렉션 자료형

    • 데이터 추가 및 삭제:

    • 예시:
      python list_1 = [1, 2, 3] list_1.append(4) # 리스트에 4 추가 list_1.remove(2) # 리스트에서 2 제거

V. 딕셔너리

  • 딕셔너리 (Dictionary): 키-값 쌍으로 구성된 자료형

    • 예시:
      python dict_1 = {'name': '홍길동', 'birth': 1990} dict_1['addr'] = 'KR' # 새로운 키-값 쌍 추가 print(dict_1['birth']) # 1990 반환

VI. CSV 파일

  • CSV (Comma-separated Values): 데이터를 쉼표로 구분하는 텍스트 형식.

  • 사용 예시:

    • 파일 기본 구조:
      text 이름,국어,영어,수학 철수,100,90,80 영수,90,95,75

  • 장점:

    • 단순한 구조와 높은 호환성

    • 쉽게 읽고 편집 가능

    • 가벼운 파일 크기

VII. 파이썬으로 CSV 파일 다루기

  • csv 라이브러리 사용

    • 파일 읽기:

   import csv
   with open('characters.csv', 'r', encoding='cp949') as f:
       rdr = csv.reader(f)
       for line in rdr:
           print(line)
  • 파일 작성하기:

   with open('output.csv', 'w', encoding='cp949', newline='') as f:
       writer = csv.writer(f)
       writer.writerow(['ID', '이름'])
       writer.writerow(['001', '홍길동'])

요약

  • 자료형의 이해는 데이터 분석의 기초

  • 다양한 자료형을 선택하여 데이터의 특성에 맞춰 처리할 수 있음

  • CSV 파일의 활용은 데이터 분석시 매우 중요함

Translation
I. Overview of Data Types
  • Data types provide the fundamental framework needed for handling data.

  • Commonly used data types: numbers, strings, collection types (tuples, lists, sets, dictionaries)

II. Numeric Data Types
  • Integer: Numbers without decimals

    • Example: integer_1 = 3214, integer_2 = -128109

  • Float: Numbers with decimals

    • Example: float_1 = -1.986214, float_2 = 123.e2

  • Execution examples:

print(integer_1, type(integer_1))  # 3214 <class 'int'>
print(float_2, type(float_2))  # 12300.0 <class 'float'>
III. Strings
  • String: A data type that includes characters and symbols.

  • Escape Sequence: Used to represent certain characters (e.g., ", \)

    • Example: string_5 = "This is String! \"Quote\" symbol:!@#$%^"

  • Execution example:

print(string_5)  # This is String! "Quote" symbol:!@#$%^ 
IV. Collection Data Types
  • Tuple: An ordered list of data that is immutable.

    • Example: tuple_1 = (1, 2, 3, 4, 5)

    • Changing a value raises an error: tuple_1[2] = 100 - TypeError

  • Set: Does not allow duplicate values, and order does not matter.

    • Example: set_1 = {1, 2, 3, 'A', 'B', 'C'}

  • List: A mutable and highly versatile collection type.

    • Adding and deleting data:

    • Example:

      list_1 = [1, 2, 3] list_1.append(4) # Add 4 to the list list_1.remove(2) # Remove 2 from the list

V. Dictionary
  • Dictionary: A data type consisting of key-value pairs.

    • Example:

      dict_1 = {'name': 'Hong Gil-dong', 'birth': 1990} dict_1['addr'] = 'KR' # Add new key-value pair print(dict_1['birth']) # Returns 1990

VI. CSV Files
  • CSV (Comma-separated Values): A text format that separates data with commas.

  • Usage Example:

    • Basic structure of a file:

      name, Korean, English, Math Cheolsu, 100, 90, 80 Yeongsu, 90, 95, 75

  • Advantages:

    • Simple structure and high compatibility

    • Easily readable and editable

    • Light file size

VII. Handling CSV Files with Python
  • Using the csv library:

    • Reading a file:

import csv
with open('characters.csv', 'r', encoding='cp949') as f:
    rdr = csv.reader(f)
    for line in rdr:
        print(line)
  • Writing a file:

with open('output.csv', 'w', encoding='cp949', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['ID', 'Name'])
    writer.writerow(['001', 'Hong Gil-dong'])
Summary
  • Understanding data types is the foundation of data analysis.

  • Various data types can be chosen to match the characteristics of the data.

  • The use of CSV files is very important in data analysis.