Python 3

Joy_all·2021년 3월 23일
0

Python

목록 보기
3/9

📒 튜플(tuple) 자료형

  • ( ) 사용해서 데이터를 저장
  • 튜플의 데이터를 수정, 삭제 불가능(변경x)
t1 = ()
t2 =(1,)  # 튜플타입의 데이터를 1개만 생성할 경우 [,] 사용해야함(필수)
t2_1 =(1) # 콤마가 없을경우 int 타입
print(type(t2), type(t2_1))

t3 = (1,2,3,4,5)
t4 = 1,2,3,4,5 # 튜플임 ( ) 생략가능 / 리스트는 [] 꼭 써야함
print(type(t4))

<class 'tuple'> <class 'int'>
<class 'tuple'>

t5 = (1,2,3,'a','b')
t6 = (1,2,3,'a','b',(1,2,3,4),[1,2,3,5])
print(t6)
print(t6[6][1])
t6[6][1] = 10
print(t6)

(1, 2, 3, 'a', 'b', (1, 2, 3, 4), [1, 2, 3, 5])
2
(1, 2, 3, 'a', 'b', (1, 2, 3, 4), [1, 10, 3, 5])

  • t6[6] = "test" 수정불가
  • print(t6)
  • del(t6[0]) 삭제불가

📍 코드를 실행하면서 데이터를 여러개 저장

값이 변경 가능 - 리스트 타입

값이 변경 불가 - 튜플 타입

print("t3",t3)
print(t3[2]) # 데이터 사용가능 (인덱싱 가능)
print(t3[2:3]) # 슬라이싱 가능 -> 튜플의 형태로 데이터 리턴

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

📍 튜플 연결(+)

print(t3 + t4)
t5 = t3+t4
print(t5)
print(t3[0:2]+t4)

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

📍 튜플 복사(*)

print(t3 * 2)
print(t3[0:2] * 2)
# print(t3[0] * 2) 불가능

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

📍 튜플에 추가

print("길이 : ", len(t3))
print(t3)
# t3 튜플에 6,7 추가
print(t3 +(6,7))
t3+=(8,9)
print( t3 )

길이 : 5
(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5, 6, 7)
(1, 2, 3, 4, 5, 8, 9)

📒 딕셔너리 자료형 (순차적접근 x)

  • key값을 사용해서 value를 사용
  • key값은 변경 X, value값은 변경 O
  • key값은 중복데이터를 사용X (가장마지막의 key값만 불러다 사용가능)
  • key값 - 튜플(o), 리스트(x)
  • { key:value, key:value,...}

📍

dic = { "name":"itwill","tel":"0518030909","addr":"부산진구" }

print(dic)
#  레퍼런스[' key값 ']
print("dic['name'] = ",dic['name'])

# 전화번호, 주소
print(dic["tel"],dic["addr"])

dic["addr"] = "부산진구 동천로109"

print(dic["addr"])

{'name': 'itwill', 'tel': '0518030909', 'addr': '부산진구'}
dic['name'] = itwill
0518030909 부산진구
부산진구 동천로109

📍 딕셔너리 타입에 데이터 추가

# dic[4]="0518030979"
dic["fax"]="0518030979"   # fax 키값, 0518030979 value값  저장  {"fax":"0518030979"}

print(dic)

# dic["classRoom"] = [1,2,3,4,5,6,7]
dic["classRoom"] = (1,2,3,4,5,6,7)

print(dic)

{'name': 'itwill', 'tel': '0518030909', 'addr': '부산진구 동천로109', 'fax': '0518030979'}
{'name': 'itwill', 'tel': '0518030909', 'addr': '부산진구 동천로109', 'fax': '0518030979', 'classRoom': (1, 2, 3, 4, 5, 6, 7)}

📍 fax 정보 삭제

del( dic['fax'] )
print(dic)
print( type(dic) )

{'name': 'itwill', 'tel': '0518030909', 'addr': '부산진구 동천로109', 'classRoom': (1, 2, 3, 4, 5, 6, 7)}
<class 'dict'>

📍 print( dic[1] ) 에러 딕셔너리 호출은 반드시 key값만 사용

dic["tel"]="010-1234-1234"
print(dic)

dic2= { "k":"125455", "k":"111111"}

print( dic2["k"] )

{'name': 'itwill', 'tel': '010-1234-1234', 'addr': '부산진구 동천로109', 'classRoom': (1, 2, 3, 4, 5, 6, 7)}
111111

📍 key값만 가져오기

print( dic.keys() )
print( type(dic.keys()) ) # <class 'dict_keys'> 타입

dict_keys(['name', 'tel', 'addr', 'classRoom'])
<class 'dict_keys'>

📍 <class 'dict_keys'> 타입 -> list 타입 변경후 사용

ls = list( dic.keys() )
print( type(ls),ls )

<class 'list'> ['name', 'tel', 'addr', 'classRoom']

📍 value 값만 가져오기

print(dic.values())
print( type(dic.values()))  # <class 'dict_values'> 타입

dict_values(['itwill', '010-1234-1234', '부산진구 동천로109', (1, 2, 3, 4, 5, 6, 7)])
<class 'dict_values'>

📍 key,value 쌍으로 가져오기

print( dic.items() )
print( type(dic.items()))  # <class 'dict_items'> 타입

dic.clear()
print( dic )
# print( dic['name'] ) 에러 (key값이 없기 때문)

dict_items([('name', 'itwill'), ('tel', '010-1234-1234'), ('addr', '부산진구 동천로109'), ('classRoom', (1, 2, 3, 4, 5, 6, 7))])
<class 'dict_items'>
{}

📍 딕셔너리 안에 key값이 있는지 체크

print('name' in dic )

if 'name' in dic:
    print("key값이 있습니다",dic['name'])
else:
    print("key값이 없습니다. (key 생성)")
    dic["name"]=""

print(dic)

False
key값이 없습니다. (key 생성)
{'name': ''}

📍 딕셔너리의 값이 있는지 없는지 확인

dic["tel"]="010-1234-1234"

print( dic.get("없는키") )  # 없는 key => None 리턴
print( dic.get("tel") )   # 있는 key => key에 해당하는 value를 리턴
# print( dic["addr"] ) -> key에러 발생

None
010-1234-1234

📒 집합 자료형 (set) 2.3~ 이후 지원

  • 데이터 중복 x, 데이터를 순서 없이 저장
  • => 인덱싱을 사용할수 없음

📍

s1 = set([1,2,3,4,5])
print(s1, type(s1))

s2 = set("itwill")
print(s2,type(s2))

# print( s1[0] )  순서가 없기 때문에 인덱싱 X
# => 리스트/튜플로 변경후 사용
l1 = list(s1)
print(l1[0])
t1 = tuple(s2)
print(t1[1])

{1, 2, 3, 4, 5} <class 'set'>
{'i', 'l', 'w', 't'} <class 'set'>
1
t

📍 자료형의 T/F

 문자열 - "test" (T) , "" (F)
 리스트 - [1,2] (T), [] (F)
 튜플 -  () (F)
 딕셔너리 - {} (F)
 숫자 -  0이 아닌숫자 (T), 0 (F)
        None (F)
ls = [1,2,3,4]
#  id(객체) -> 객체의 메모리 주소
print( ls , id(ls))

ls2 = ls

print( id(ls2), id(ls))
#  A  is  B  : A와 B가 동일한 객체 인가? 물어보는 연산자 is
print( ls is ls2 )

from copy import copy

a = copy(ls2)

print(a, id(a),id(ls2))

[1, 2, 3, 4] 2604960319936
2604960319936 2604960319936
True
[1, 2, 3, 4] 2604960380800 2604960319936

📍 변수 생성하기

a,b = ( '1','2' )
(a,b) = ( '1','2' )
[aa,bb] =[11,22]
a=b=100

a=100
b=200

a,b = b,a

print(a,b)

200 100

profile
beginner

0개의 댓글