-
[Python tutorial] 8. Data type - Tuple코딩/Python 2022. 12. 21. 17:13728x90
Tuples
- ordered (indexed)
- unchangeable
- allow duplicate values
thistuple = ("apple", "banana", "cherry", "apple", "cherry") # allow duplicate values # Data Types: any data types tuple1 = ("apple", "banana", "cherry") tuple2 = (1, 5, 7, 9, 3) tuple3 = (True, False, False) tuple4 = ("abc", 34, True, 40, "male")
Create Tuple With One Item: (...,)
thistuple = ("apple",) thistuple = ("apple") # NOT a tuple print(type(thistuple)) <class 'str'>
Constructor: tuple((_,))
thistuple = tuple(("apple", "banana", "cherry"))
Access Tuple Items
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") # Indexing print(thistuple[1]) # banana # Negative Indexing print(thistuple[-1]) # mango # Range of Indexes print(thistuple[2:5]) # ('cherry', 'orange', 'kiwi') print(thistuple[:4]) # ('apple', 'banana', 'cherry', 'orange') print(thistuple[2:]) # ('cherry', 'orange', 'kiwi', 'melon', 'mango') print(thistuple[-4:-1]) # ('orange', 'kiwi', 'melon')
Check if Item Exists
if "apple" in thistuple: print("Yes, 'apple' is in the fruits tuple")
Update Tuples
Tuples are unchangeable
tuple -> list -> change item -> tuple
# Change Tuple Values x = ("apple", "banana", "cherry") y = list(x) # tuple -> list y[1] = "kiwi" # change item x = tuple(y) # list -> tuple # Add Items thistuple = ("apple", "banana", "cherry") y = list(thistuple) # tuple -> list y.append("orange") # append list thistuple = tuple(y) # list -> tuple # Remove Items thistuple = ("apple", "banana", "cherry") y = list(thistuple) # tuple -> list y.remove("apple") # remove item thistuple = tuple(y) # list -> tuple
Join Tuples
# + thistuple = ("apple", "banana", "cherry") y = ("orange",) thistuple += y
Multiply Tuples: *
fruits = ("apple", "banana", "cherry") mytuple = fruits * 2
del keyword
thistuple = ("apple", "banana", "cherry") del thistuple
Unpack Tuples
fruits = ("apple", "banana", "cherry") (green, yellow, red) = fruits # unpack print(green) print(yellow) print(red)
apple
banana
cherryAsterisk*
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry") (green, yellow, *red) = fruits # * list print(green) print(yellow) print(red) # list
apple
banana
['cherry', 'strawberry', 'raspberry']fruits = ("apple", "mango", "papaya", "pineapple", "cherry") (green, *tropic, red) = fruits print(green) print(tropic) print(red)
apple
['mango', 'papaya', 'pineapple']
cherryLoop Tuples
thistuple = ("apple", "banana", "cherry") # for loop for x in thistuple: print(x) # Index Numbers: range() & len() for i in range(len(thistuple)): print(thistuple[i]) # While Loop i = 0 while i < len(thistuple): print(thistuple[i]) i = i + 1
Tuple Methods
Method Description count() Returns the number of times a specified value occurs in a tuple index() Searches the tuple for a specified value and returns the position of where it was found 728x90'코딩 > Python' 카테고리의 다른 글
[Python tutorial] 10. Data type - Dictionary (0) 2022.12.21 [Python tutorial] 9. Data type - Sets (0) 2022.12.21 [Python tutorial] 7. Data type - List (0) 2022.12.21 [Python tutorial] 6. Data type - Boolean (0) 2022.12.21 [Python tutorial] 5. Data type - Strings (1) 2022.12.21