인공지능/PYTHON

w3school (3) - Tuples

bibibig_data 2021. 6. 17. 17:34

튜플

- 튜플은 단일 변수에 여러 항목을 저장하는 데 사용됩니다.

- Tuple은 데이터 컬렉션을 저장하는 데 사용되는 Python의 4 가지 내장 데이터 유형 중 하나이며 나머지       3 개           는 List , Set , Dictionary 이며 모두 품질과 사용법이 다릅니다.

- 튜플은 순서가 지정되고 변경할 수없는 모음입니다 .

- 튜플은 둥근 ​​대괄호로 작성됩니다.

- 튜플 항목은 순서가 지정되고 변경할 수 없으며 중복 값을 허용합니다.

- 튜플 항목은 인덱싱되고 첫 번째 항목에는 index [0]가 있고 두 번째 항목에는 인덱스 [1]등이 있습니다.

- 튜플이 정렬되었다고 말하면 항목에 정의 된 순서가 있으며 해당 순서가 변경되지 않음을 의미합니다.

- 튜플은 변경할 수 없습니다. 즉, 튜플이 생성 된 후에는 항목을 변경, 추가 또는 제거 할 수 없습니다.

- 튜플은 인덱싱되므로 동일한 값을 가진 항목을 가질 수 있습니다

 

Python 튜플

thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple)) # <class 'tuple'>
                       # <class 'str'>
                       
                       
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)    


x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)


thistuple = ("apple", "banana", "cherry")
thistuple.append("orange") # This will raise an error
print(thistuple)


thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)


fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)    # apple
              # banana
              # ['cherry', 'strawberry', 'raspberry']
              
              
thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
  print(thistuple[i])
  
  
thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
  print(thistuple[i])
  i = i + 1
  
  
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)  # ('a', 'b', 'c', 1, 2, 3) 출력


thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)


thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)


thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)


thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)


thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)


thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)


thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)


thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)  # Error

튜플 액세스

 

튜플 업데이트

 

튜플 압축 해제

 

루프 튜플

 

튜플 결합

 

튜플 방법

 

튜플 연습