Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Python - Tuple

Back


Tuple


Tuple Method

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

Create a tuple


Access Items

print("\n--------Access Items--------\n")
# Access Items
xtuple = ("apple", "banana", "cherry")
print(xtuple[0])  # apple
print(xtuple[1])  # banana
# print(xtuple[3])  # Error: IndexError: list index out of range

# Negative Indexing
print(xtuple[-1])  # cherry
print(xtuple[-2])  # banana

# Range of Indexes
xtuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(xtuple[2:5])   # ['cherry', 'orange', 'kiwi']
print(xtuple[:5])   # exclusive ['apple', 'banana', 'cherry', 'orange', 'kiwi']
print(xtuple[5:])   # inclusive ['melon', 'mango']

# Range of Negative Indexes
xtuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(xtuple[-4:-1])  # ['orange', 'kiwi', 'melon']



Change Tuple Values

xtuple = ("apple", "banana", "cherrylist")
ylist = list(xtuple)
ylist[1] = "kiwi"
xtuple = tuple(ylist)

print(xtuple)   # ('apple', 'kiwi', 'cherrylist')


Add Items

  1. Convert into a list: tuple -> list -> tuple.

  2. Add tuple to a tuple. create a new tuple with the item(s), and add it to the existing tuple.


xtuple = ("apple", "banana", "cherrylist")
ylist = list(xtuple)
ylist.append("kiwi")
xtuple = tuple(ylist)

print(xtuple)   # ('apple', 'banana', 'cherrylist', 'kiwi')


xtuple = ("apple", "banana", "cherry")
ylist = ("orange",)
xtuple += ylist

print(xtuple)   # ('apple', 'banana', 'cherry', 'orange')


Remove Items

  1. Convert into a list: tuple -> list -> tuple.
  2. delete the tuple completely
xtuple = ("apple", "banana", "cherry")
ylist = list(xtuple)
ylist.remove("apple")
xtuple = tuple(ylist)

print(xtuple)   # ('banana', 'cherry')

xtuple = ("apple", "banana", "cherry")
del xtuple
print(xtuple)  # NameError: name 'xtuple' is not defined


Unpack

print("\n------Unpacking------\n")

print("\n------Equal------\n")
# number of variables must match number of items
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits

print(green)    # apple
print(yellow)   # banana
print(red)      # cherry

print("\n------Less than------\n")

#  if number of variables is less than number of items, Error
fruits = ("apple", "banana", "cherry")
# (green, yellow) = fruits    # ValueError: too many values to unpack (expected 2)

#  if number of variables is less than number of items, should Asterisk
fruits = ("apple", "banana", "cherry")
(green, *yellow) = fruits
print(green)    # apple
print(yellow)   # ['banana', 'cherry']


fruits = ("apple", "mango", "papaya", "pineapple", "cherry")
(green, *tropic, red) = fruits
print(green)    # apple
print(tropic)   # ['mango', 'papaya', 'pineapple']
print(red)      # cherry

print("\n------More than------\n")
fruits = ("apple", "banana", "cherry")
# ValueError: not enough values to unpack (expected 4, got 3)
(green, yellow, red, grey) = fruits



Check if Item Exists:in

xlist = ("apple", "banana", "cherry")
if "apple" in xlist:
    print("Yes, 'apple' is in the fruits list")

Loop Tuple


print("\n--------for in loop--------\n")
# for in loop
xtuple = ("apple", "banana", "cherry")
for x in xtuple:
    print(x)


print("\n--------for in range loop--------\n")
# for in range(len())
xtuple = ("apple", "banana", "cherry")
for i in range(len(xtuple)):     # [0, 1, 2]
    print(xtuple[i])


print("\n--------While Loop--------\n")
# While Loop
xtuple = ("apple", "banana", "cherry")
i = 0
while i < len(xtuple):   # 可以用于判断是否进入循环
    print(xtuple[i])
    i = i + 1


Join Tuple

print("\n--------Join tuple--------\n")
tuple1 = ("a", "b", "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)   # ('a', 'b', 'c', 1, 2, 3)


fruits = ("apple", "banana", "cherry")
mytuple = fruits * 3

# ('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
print(mytuple)

count()

print("\n--------count()--------\n")
xlist = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

num = xlist.count(5)

print(num)  # 2


index()

print("\n--------index()--------\n")
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

x = thistuple.index(8)

print(x)    # 3


TOP