본문 바로가기

python

python 공부 3일차 - 3

728x90

파이썬 튜플
리스트와 비교 중요
튜플 자료형(순서o, 중복o, 수정x, 삭제x) 불변

 

선언

a = ()
b = (1,) # (수정x, 삭제x)
c = (11,12,13,14)
d = (100,200,'Ace','Base','Captine')
e = (100,200,('Ace','Base','Captine'))



# 인덱싱
print('c - ', c)
print('d - ', d[1])
print('d - ', d[0] + d[1] + d[1])
print('d - ', d[-1])
print('e - ', e[-1])
print('e - ', e[-1][1])
print('e - ', list(e[-1][1]))

c -  (11, 12, 13, 14)
d -  200
d -  500
d -  Captine
e -  ('Ace', 'Base', 'Captine')
e -  Base
e -  ['B', 'a', 's', 'e']

 

 

수정x
d[0] = 1500


슬라이싱

print('d - ',d[0:3])
print('d - ',d[2:])
print('d - ',d[2][1:3])


d -  (100, 200, 'Ace')
d -  ('Ace', 'Base', 'Captine')
d -  ce

 

튜플 연산

print('c + d', c + d)
print('c * 3', c * 3)

c + d (11, 12, 13, 14, 100, 200, 'Ace', 'Base', 'Captine')
c * 3 (11, 12, 13, 14, 11, 12, 13, 14, 11, 12, 13, 14)

 

튜플 함수

a = (5,2,3,1,4)
print('a - ', a)
print('a - ', a.index(3)) # 위치을 알려주는 함수
print('a - ', a.count(2)) # 개수을 알려주는 함수

a -  (5, 2, 3, 1, 4)
a -  2
a -  1

 

팩킹 언팩(Packing, and Unpacking)

팩킹

 

t = ('foo','bar','baz','qux')

print(t)
print(t[0])
print(t[-1])

('foo', 'bar', 'baz', 'qux')
foo
qux

 

언팩킹

 

(x1,x2,x3,x4) = t

print(type(x1),type(x2),type(x3),type(x4))
print(x1,x2,x3,x4)


<class 'str'> <class 'str'> <class 'str'> <class 'str'>
foo bar baz qux

 

#  팩킹 & 언 팩킹
t2 = 1, 2, 3 ## 팩킹
t3 = 4, ## 팩킹
x1,x2,x3 = t2 ## 언팩킹
x4,x5,x6 = 4,5,6 ## 언팩킹

print(t2)
print(t3)
print(x1,x2,x3)
print(x4,x5,x6)


(1, 2, 3)
(4,)
1 2 3
4 5 6

 

 

파이썬 딕셔너리
범용적으로 가장 많이 사용
딕셔너리 자효형(순서x, 키 중복 x, 수정o,  삭제o)
[] 리스트 () 튜플 {} 딕셔너리

 

선언

 

a = {'name':'kim','phone' : '01012341234','birth' : '34555'}
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
)

 

 

print('a - ', type(a),a)
print('b - ', type(b),b)
print('c - ', type(c),c)
print('d - ', type(d),d)
print('e - ', type(e),e)
print('f - ', type(f),f)

print('a - ', a['name'])    # 존재 x --> 에러 발생
print('a - ', a.get('name1')) # 존재 x --> none
print('b - ', b[0])
print('b - ', b.get(0))
print('f - ', f.get('City'))
print('f - ', f.get('Age'))



a -  <class 'dict'> {'name': 'kim', 'phone': '01012341234', 'birth': '34555'}
b -  <class 'dict'> {0: 'Hello python'}
c -  <class 'dict'> {'arr': [1, 2, 3, 4]}
d -  <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33, 'Grade': 'A', 'Status': True}
e -  <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33, 'Grade': 'A', 'Status': True}
f -  <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33, 'Grade': 'A', 'Status': True}
a -  kim
a -  None
b -  Hello python
b -  Hello python
f -  Seoul
f -  33

 

 

딕셔너리 추가

 

a['address'] = 'seoul'
print('a - ', a)
a['rank'] = [1,2,3]
print('a - ', a)


a -  {'name': 'kim', 'phone': '01012341234', 'birth': '34555', 'address': 'seoul'}
a -  {'name': 'kim', 'phone': '01012341234', 'birth': '34555', 'address': 'seoul', 'rank': [1, 2, 3]}

 

 

딕셔너리 길이

print('a - ',len(a))
print('b - ',len(b))
print('c - ',len(c))
print('d - ',len(d))

a -  5
b -  1
c -  1
d -  5

 

dict_Keys, Dict_values, dict_items: 반복(__iter__)에서 사용 불가

 

 

키값 찾기

print('a - ', a.keys()) #키을 알려주는 함수
print('b - ', b.keys())
print('c - ', c.keys())
print('d - ', d.keys())
print('a - ', list(a.keys()))
print('b - ', list(b.keys()))

dict_keys(['name', 'phone', 'birth', 'address', 'rank'])
b -  dict_keys([0])
c -  dict_keys(['arr'])
d -  dict_keys(['Name', 'City', 'Age', 'Grade', 'Status'])
a -  ['name', 'phone', 'birth', 'address', 'rank']
b -  [0]

 

 

키의 값 찾기

 

print('a - ', a.values()) #키의 값을 알려주는 함수
print('b - ', b.values())
print('c - ', c.values())
print('a - ', list(a.values()))
print('b - ', list(b.values()))

a -  dict_values(['kim', '01012341234', '34555', 'seoul', [1, 2, 3]])
b -  dict_values(['Hello python'])
c -  dict_values([[1, 2, 3, 4]])
a -  ['kim', '01012341234', '34555', 'seoul', [1, 2, 3]]
b -  ['Hello python']

 

키와 키의 값 찾기

 

print('a - ', list(a.items())) # 키와 키 값을 알려주는 함수
print('b - ', list(b.items()))
print('c - ', list(c.items()))

a -  [('name', 'kim'), ('phone', '01012341234'), ('birth', '34555'), ('address', 'seoul'), ('rank', [1, 2, 3])]
b -  [(0, 'Hello python')]
c -  [('arr', [1, 2, 3, 4])]

 

값 꺼내기

 

print('a - ', a.pop('phone')) # pop 값을 꺼내는 함수
print('a - ',a)

print('c - ', c.pop('arr'))
print('c - ', c)

print('f - ', f.popitem()) # 무작위로 꺼내는 함수
print('f - ', f)
print('f - ', f.popitem())
print('f - ', f)

a -  01012341234
a -  {'name': 'kim', 'birth': '34555', 'address': 'seoul', 'rank': [1, 2, 3]}
c -  [1, 2, 3, 4]
c -  {}
f -  ('Status', True)
f -  {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33, 'Grade': 'A'}
f -  ('Grade', 'A')
f -  {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33}

 

 

디셔너리 키 있는지 물어보기

 

print('a - ', 'birth' in a)
print('d - ', 'City' in d)


a -  True
d -  True

 

수정 & 추가

 

a['test'] = 'test_dict'
print('a = ', a)

a['address'] = 'dj'
print('a- ', a)

a.update(birth='910904')
print('a- ', a)
temp = {'address':'Busan'}

a.update(temp)
print(a)


a =  {'name': 'kim', 'birth': '34555', 'address': 'seoul', 'rank': [1, 2, 3], 'test': 'test_dict'}
a-  {'name': 'kim', 'birth': '34555', 'address': 'dj', 'rank': [1, 2, 3], 'test': 'test_dict'}
a-  {'name': 'kim', 'birth': '910904', 'address': 'dj', 'rank': [1, 2, 3], 'test': 'test_dict'}
{'name': 'kim', 'birth': '910904', 'address': 'Busan', 'rank': [1, 2, 3], 'test': 'test_dict'}

'python' 카테고리의 다른 글

python 공부3일차 - 2  (0) 2020.12.22
python 공부 3일차  (0) 2020.12.22
python 공부 2일차  (0) 2020.12.21
python 공부 1일차  (0) 2020.12.21
python 설치하기  (0) 2020.12.21