[Python 기초 정리] 기초 자료형

서해빈·2021년 4월 18일
0

python

목록 보기
3/5

강의 바로가기

Data Type

1. 파이썬 지원 자료형

  • int : 정수
  • float : 실수
  • complex : 복소수
  • bool : 불린
  • str : 문자열(시퀀스)
  • list : 리스트(시퀀스)
  • tuple : 튜플(시퀀스)
  • set : 집합
  • dict : 사전

1.1 사용 예시

str_example1 = "Python"
str_example2 = "Anaconda"
bool_example = True
float_example = 10.0
int_example = 7
list_example = [str1, str2]
dict_example = {
    "name": "Machine Learning",
    "version": 2.0
}
tuple_example = 3, 5, 7 # or (3, 5, 7)
set_example = {7, 8, 9}

2. 숫자형

2.1 연산자 및 내장 함수

  • +, -, *, / : 사칙연산
  • // : 몫
  • % : 나머지
  • abs(x) : 절대값
  • int(x), float(x), complex(x): 형변환
  • pow(x, y), x ** y : 제곱(xyx^y)
  • x, y = divmod(100, 8) : 몫, 나머지 반환

2.1.1 Implicit type casting

이항연산자에 정수와 실수를 입력값으로 주면, 정수는 실수로 묵시적 형변환되어 계산된다. 이 후 결과를 실수로 반환된다.

>>> i1 = 39
>>> i2 = 939
>>> f1 = 1.234
>>> f2 = 3.939
>>> print(f1 + i2)
940.234
>>> print(f1 - i2)
-937.766
>>> print(f1 * i2)
1158.7259999999999
>>> print(f2 / i1)
0.101
>>> print(f2 // i1)
0.0
>>> print(f1 % i2)
1.234
>>> print(f1 ** i2)
5.5582100569458844e+85

2.1.2 외부 모듈 함수

  • math.ceil(5.1): 올림
  • math.ceil(5.1): 내림

2.2 선언

2.2.1 정수

int, long 같은 크기에 따른 다른 선언을 생각할 필요없이 그냥 선언하면 된다.

i1 = 77
i2 = -14
big_int = 999999999999999999999999999999999999999

2.2.2 실수

f1 = 0.9999
f2 = -3.9
f3 = 3 / 9

3. 문자형

3.1 생성

PEP8에서는 큰따옴표와 작은따옴표는 동일하며, 자체 규칙을 세울것을 권장한다.
단 세 개의 따옴표를 사용할 때에는 반드시 쌍따옴표를 사용해 표기한다.

str1 = "I am Python."
str2 = 'Python'
str3 = """How are you?"""
str4 = '''Thank you!'''

3.2 활용

3.2.1 길이

len(str1)

3.2.2 빈 문자열

str_t1 = ''
str_t2 = str()

3.2.3 escape

\n : 개행
\t : 탭
\\ : 문자
\' : 문자
\" : 문자
\000 : 널 문자

3.2.4 raw string

escape 처리 x

>>> raw_s1 = r'D:\Python\python3'
>>> raw_s2 = r"\\x\y\z\q"
>>> print(raw_s1)
D:\Python\python3
>>> print(raw_s2)
\\x\y\z\q

3.3 연산 및 함수

3.3.1 연산

sequence 형(list, string, tuple)은 in 연산자 사용 가능

>>> str_o1 = "Python"
>>> str_o2 = "Apple"
>>> str_o3 = "How are you doing?"
>>> str_o4 = "Seoul Deajeon Busan Jinju"
>>>
>>> print(3 * str_o1)
PythonPythonPython
>>> print(str_o1 + str_o2)
PythonApple
>>> print('y' in str_o1)
True
>>> print('P' not in str_o2)
True
>>> print(dir(str_o1))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize',
'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',
'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal',
'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans',
'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit',
'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill']

내장함수 dir([object])
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

3.3.2 함수 및 문자열 매서드

>>> str_o1 = "Python"
>>> str_o2 = "Apple"
>>> str_o3 = "How are you doing?"
>>> str_o4 = "Seoul Deajeon Busan Jinju"
>>>
>>> print(str_o1.capitalize())
Python
>>> print(str_o2.endswith("s"))
False
>>> print(str_o1.join(["I'm ", "!"]))
I'm Python!
>>> print(str_o1.replace("thon", " Good"))
Py Good
>>> print(str_o3.replace("are", "was"))
How was you doing?
>>> print(str_o4.split(" ")) # list 반환
['Seoul', 'Deajeon', 'Busan', 'Jinju']
>>> print(sorted(str_o1))  # default: reverse=False
['P', 'h', 'n', 'o', 't', 'y']
>>> print(reversed(str_o2)) # reverse iterator 반환(sequence protocol 지원)
<reversed object at 0x7f9707c29c10>
>>> print(list(reversed(str_o2)))
['e', 'l', 'p', 'p', 'A']

cf) 반복문에서 사용

dir()에서 확인했을 때 __iter__가 존재한다면 반복문에서 사용할 수 있다.

for s in str_example:
    print(s)

3.4 슬라이싱

시퀀스 자료형에서 일부를 가져올 때 많이 사용한다. [start, stop)의 범위로 가져오며, 새로 생성한 객체를 반환한다. (기존의 데이터 변화 x)

[] (slicing)

[start:stop[:step]]

  • start: Optional. Starting index of the slice. Defaults to 0.
  • stop: Optional. The last index of the slice or the number of items to get. Defaults to len(sequence).
  • step: Optional. Extended slice syntax. Step value of the slice. Defaults to 1.

“Degenerate slice indices are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string.” — Official Python 3 Documentation

str_sl = 'Nice Python'

print(str_sl[1:4:2]) # [1, 4), 매번 2칸씩 이동(하나 건너 이동)
print(str_sl[-4:-2]) # 음수는 뒤에서의 상대 위치를 의미. ex) -1은 마지막 elem의 index.

3.5 ASCII 코드

>>> a = 'z'
>>> print(ord(a)) # 해당 문자의 아스키 코드 값 반환
122
>>> print(chr(122)) # 해당 아스키 코드 값의 문자 반환
z

4. 리스트 (List)

  • 순서 O
  • 중복 O
  • 수정 O
  • 삭제 O

4.1 선언

a = []
b = list()

4.2 인덱싱 및 슬라이싱

>>> e = [1000, 10000, ['Ace', 'Base', 'Captine']]
>>> print(e[-1][1])
Base
>>> print(list(e[-1][1]))
['B', 'a', 's', 'e']
>>> print(e[2][1:3])
['Base', 'Captine']

4.3 수정 및 삭제

>>> l = [4, 75, 80, 85]
>>> l[1:2] = ["a", "b", "c"]  # [['a', 'b', 'c']]을 넣으면 l[1] = ["a", "b", "c"]와 같다.
# l: [4, 'a', 'b', 'c', 80, 85]
>>> l[1] = ["a", "b", "c"]
# l: [4, ['a', 'b', 'c'], 'b', 'c', 80, 85]
>>> l[1:3] = [] # del l[1:3]과 같은 결과. del을 사용하자.
# l: [4, 'c', 80, 85]
>>> del l[3]
# l: [4, 'c', 80]

4.4 연산 및 메서드

4.4.1 연산

결과를 새로운 리스트로 생성해 반환한다.

>>> a = [70, 75, 80, 85]
>>> b = [1000, 10000, "Ace", "Base", "Captine"]
>>> e = a + b # concat
>>> f = a * 3 # repeat
>>> 
>>> print(id(a) != id(b) != id(e))
True
>>> print(id(a) != id(f))
True

4.4.2 메서드

>>> l = [5, 2, 3, 1, 4]
>>> l.append(6)     # [5, 2, 3, 1, 4, 6]
>>> l.sort()        # [1, 2, 3, 4, 5, 6]
>>> l.reverse()     # [6, 5, 4, 3, 2, 1]
>>> l.insert(2, 7)  # [6, 5, 7, 4, 3, 2, 1] # idx = 2에 7 삽입
>>> l.remove(1)     # [6, 5, 7, 4, 3, 2]    # value 1을 찾아서 삭제
>>> l.pop()         # [6, 5, 7, 4, 3]
>>> l.pop()         # [6, 5, 7, 4]
>>> l.count(4)      # 1                     # 4의 개수
>>> ex = [8, 9]
>>> l.extend(ex)    # [6, 5, 7, 4, 8, 9]
>>> l.clear()       # []                    # 모두 제거

5. 튜플 (Tuple)

list와 마찬가지로 다양한 타입이 함께 포함될 수 있다.

  • 순서 O
  • 중복 O
  • 수정 X
  • 삭제 X, immutable

5.1 선언

a = ()
b = (1,)

5.2 연산 및 함수

5.2.1 연산

>>> a = (11, 12,13, 14)
>>> b = (100, 1000,'Ace', 'Base', 'Captine')
>>> print(a + b)
(11, 12, 13, 14, 100, 1000, 'Ace', 'Base', 'Captine')
>>> print(a * 3)
(11, 12, 13, 14, 11, 12, 13, 14, 11, 12, 13, 14)

5.2.2 함수

>>> a = (5, 2, 3, 1, 4)
>>> print(a.index(5)) # 5의 index 반환
2
>>> print(a.count(4)) # 4의 개수 반환
1

5.3 팩킹 & 언팩킹 (Packing, and Unpacking)

5.3.1 팩킹

t = ('foo', 'bar', 'baz', 'qux') # 괄호 생략 가능. 하지만 가독성을 위해 작성하자.

5.3.2 언팩킹

(x1, x2, x3, x4) = t # 괄호 생략 가능. 하지만 가독성을 위해 작성하자.
(x1, x2, x3, x4) = ('foo', 'bar', 'baz', 'qux')

6. 딕셔너리 (Dictionary)

  • (일단은) 순서 X
  • 키 중복 X
  • 수정 O
  • 삭제 O

파이썬 3.6 부터는 기본 사전(dict)도 OrderedDict 클래스와 동일하게 동작하기 때문에 입력 순서가 보장된다. 그래도 하위 호환성 보장 측면에서 가급적 데이터의 순서가 중요한 경우에는 사전 보다는 OrderedDict 클래스를 사용하는 것이 권장된다.

6.1 선언

Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.

a = {'name': 'Kim', 'phone': '01012345678', 'birth': '870124'}
b = {0: 'Hello python!'}
c = {'arr': [1, 2, 3, 4]}
d = {
    'Name' : 'Niceman',
    'City'   : 'Seoul',
    'Age': '33',
    'Grade': 'A',
    'Status'  : True
}
e =  dict([
    ( 'Name', 'Niceman'),
    ('City', 'Seoul'),
    ('Age', '33'),
    ('Grade', 'A'),
    ('Status', True)
])

f =  dict(
    Name='Niceman',
    City='Seoul',
    Age='33',
    Grade='A',
    Status=True
)

6.2 접근, 추가, 길이

# 접근
a['name'] # 존재X -> 에러 발생
a.get('name') # 존재X -> None 처리

# 추가
a['address'] = 'seoul'

# 길이 (키-값 쌍의 개수)
len(a)

6.3 메서드 및 함수 (연산자)

dict_keys, dict_values, dict_items : 반복문(iterate) 사용 가능

# dict_keys
>>> print(a.keys())
dict_keys(['name', 'phone', 'birth'])
>>> print(list(a.keys()))
['name', 'phone', 'birth']

# dict_values
>>> print(a.values())
dict_values(['Kim', '01012345678', '870124'])
>>> print(list(a.values()))
['Kim', '01012345678', '870124']

# dict_items
>>> print(a.items())
dict_items([('name', 'Kim'), ('phone', '01012345678'), ('birth', '870124')])
>>> print(list(a.items()))
[('name', 'Kim'), ('phone', '01012345678'), ('birth', '870124')]

# 삭제
>>> print(a.pop("name"))
Kim
>>> print(f.popitem())
('Status', True)

# 포함
>>> print("phone" in a)
True
>>> print("addr" in a)
False

# 수정
>>> f.update(Age=36) # Age: 33 -> 36
# f: {'Name': 'Niceman', 'City': 'Seoul', 'Age': '36', 'Grade': 'A' }
>>> temp = {"Age": 27}
>>> f.update(temp) # Age: 36 -> 27
# f: {'Name': 'Niceman', 'City': 'Seoul', 'Age': '27', 'Grade': 'A' }

pop(key[, default])

If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.

popitem()

Remove and return a (key, value) pair from the dictionary. (since version 3.7) Pairs are returned in LIFO order. (In prior versions, popitem() would return an arbitrary key/value pair)

popitem() is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling popitem() raises a KeyError.

파이썬 3.0 이후 버전의 keys 함수, 어떻게 달라졌나?

파이썬 2.7 버전까지는 a.keys() 함수를 호출할 때 반환 값으로 dict_keys가 아닌 리스트를 돌려준다. 리스트를 돌려주기 위해서는 사전의 정보를 복사하느라 메모리 낭비가 발생하는데, 파이썬 3.0 이후 버전에서는 이러한 메모리 낭비를 줄이기 위해 dict_keys 객체를 돌려준다. 다음에 소개할 dict_values, dict_items 역시 파이썬 3.0 이후 버전에서 추가된 것들이다. 만약 3.0 이후 버전에서 반환 값으로 리스트가 필요한 경우에는 list(a.keys())를 사용하면 된다. dict_keys, dict_values, dict_items 등은 리스트로 변환하지 않더라도 기본적인 반복(iterate) 구문(예: for문)을 실행할 수 있다.

Dictionary view objects

The objects returned by dict.keys(), dict.values() and dict.items() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.

Dictionary views can be iterated over to yield their respective data, and support membership tests:

  • len(dictview)
  • iter(dictview)
  • x in dictview
  • reversed(dictview)

7. 집합 (Set)

  • 순서 X
  • 중복 X

A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed).

7.1 선언

a = set()
b = set([1, 2, 3, 4])
c = {'foo', 'bar', 'baz', 'foo', 'qux'}

7.2 변환, 길이

# 튜플 변환
t = tuple(b)

# 리스트 변환
l = list(c)

# 길이
print(len(a))

7.3 연산 및 메서드

>>> s1 = set([1, 2, 3, 4, 5, 6])
>>> s2 = set([4, 5, 6, 7, 8, 9])

>>> print(s1 & s2)
{4, 5, 6}
>>> print(s1.intersection(s2))
{4, 5, 6}

>>> print(s1 | s2)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> print(s1.union(s2))
{1, 2, 3, 4, 5, 6, 7, 8, 9}

>>> print(s1 - s2)
{1, 2, 3}
>>> print(s1.difference(s2))
{1, 2, 3}

>>> print(s1.isdisjoint(s2)) # 서로소 집합 확인
False
>>> print(s1.issubset(s2)) # 부분 집합 확인
False
>>> print(s1.issuperset(s2)) # 상위 집합 확인
False

# 추가 & 제거
>>> s1.add(7)
# s1: {1, 2, 3, 4, 5, 6, 7}
>>> s1.remove(2)
# s1: {1, 3, 4, 5, 6, 7}
>>> s1.discard(3)
# s1: {1, 4, 5, 6, 7}
>>> s1.discard(9) # discard는 set에 없는 값을 입력으로 주어도 error 발생 X. remove는 KeyError.
# s1: {1, 4, 5, 6, 7}

# 모두 제거
s1.clear()

0개의 댓글