"Python 자료형"의 두 판 사이의 차이

39번째 줄: 39번째 줄:
*list는 [] 를 사용하지만 Dictionaries는 {}를 사용함
*list는 [] 를 사용하지만 Dictionaries는 {}를 사용함


;학생들의 이름과 나이를 저장하는 Dictionary
학생들의 이름과 나이를 저장하는 Dictionary
<source lang="python">
<source lang="python">
students = {"SeongMin":10, "DongHyun":11, "JiWon":12}
students = {"SeongMin":10, "DongHyun":11, "JiWon":12}
45번째 줄: 45번째 줄:
</source>
</source>


;학생들의 나이, 과목, 음식의 정보를 저장하는 Dictionary
학생들의 나이, 과목, 음식의 정보를 저장하는 Dictionary
<source lang="python">
<source lang="python">
students = {
students = {
54번째 줄: 54번째 줄:
print(students["DongHyun"])
print(students["DongHyun"])
</source>
</source>
;월요일 시간표를 출력 한 후 History 과목을 추가 후 다시 출력
월요일 시간표를 출력 한 후 History 과목을 추가 후 다시 출력
<source lang="python">
<source lang="python">
monday = {
monday = {
67번째 줄: 67번째 줄:
print(monday)
print(monday)
</source>
</source>
;get 메쏘드를 통해 지정한 키의 값을 가져옴
get 메쏘드를 통해 지정한 키의 값을 가져옴
<source lang="python">
<source lang="python">
students = {"SeongMin":10, "DongHyun":11, "JiWon":12}
students = {"SeongMin":10, "DongHyun":11, "JiWon":12}

2018년 9월 1일 (토) 10:36 판

1 개념

Python Data Types
파이썬 데이터타입, 파이썬 자료형
  • 파이썬의 다양한 데이터 타입에 대한 정의
한국어명 영어명 형식 설명
튜플 tuple (1,2,3)
집합 set [1,2,3]
딕셔너리 dictionary {'a':1, 'b':2}
널 객체 null object None null과 유사한 개념

2 None

  • None 객체는 값이 존재 하지 않음을 표현
  • 다른 언어의 null과 유사한 개념
def returnNone():
    print("returnNone function doesn't return anything.")

def returnSomething():
    print("returnSomething function returns number 1.")
    return 1

ret = returnNone();
ret2 = returnSomething();
print(ret)
print(ret2)

3 딕셔너리 (Dictionaries)

  • 키와 값으로 이뤄진 데이터 타입임
  • list와 비슷하지만 list는 키가 숫자로 이뤄져 있다고 생각하면 됨
  • list는 [] 를 사용하지만 Dictionaries는 {}를 사용함

학생들의 이름과 나이를 저장하는 Dictionary

students = {"SeongMin":10, "DongHyun":11, "JiWon":12}
print(students["DongHyun"])

학생들의 나이, 과목, 음식의 정보를 저장하는 Dictionary

students = {
    "SeongMin":[10, "Math", "Noodle"],
    "DongHyun":[11, "Programming", "Rice"],
    "JiWon":[2, "English", "spaghetti"]
}
print(students["DongHyun"])

월요일 시간표를 출력 한 후 History 과목을 추가 후 다시 출력

monday = {
    1:"Math",
    2:"Science",
    3:"Music",
    4:"English"
}
print(monday)

monday[5] = "History"
print(monday)

get 메쏘드를 통해 지정한 키의 값을 가져옴

students = {"SeongMin":10, "DongHyun":11, "JiWon":12}
print(students.get("DongHyun"))

4 튜플 (Tuples)

  • list와 유사하지만 수정 될 수 없음
  • () 를 사용하여 정의
week = ("Mon", "Tue",  "Wed", "Thu", "Fri", "Sat")
print(week[1])
# 키 1번의 값인 "Tue"를 "John"으로 변경하려 시도하면 에러가 발생
# week[1] = "John"

5 집합 (set)

  • 순서가 없음
  • 중복 데이터가 포함 될 수 없음 (멤버쉽과 같이 중복이 없는 데이터 설계시 사용)
  • 중복삭제 용도로 사용 가능
a = [1, 2, 3, 4, 5, 5, 5]
print(type(a))
print(a)
b = set(a); # 중복 삭제
print(type(b))
print(b)
john@zetawiki:~$ python test.py
<type 'list'>
[1, 2, 3, 4, 5, 5, 5]
<type 'set'>
set([1, 2, 3, 4, 5])

6 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}