ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python tutorial] 8. Data type - Tuple
    코딩/Python 2022. 12. 21. 17:13
    728x90

    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
    cherry

    Asterisk*

    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']
    cherry

    Loop 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

    댓글

Designed by Tistory.