| Method | Meaning |
|---|---|
| index() | finds the first occurrence of a value. |
| count() | returns the number of times a value is in a tuple. |
index(value)¶required argument (value).thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.index(8)
print(x)
3
count(value)¶returns the number of times a value is in a tuple.
It takes one required argument (value).
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5)
print(x)
2
| Method | Meaning |
|---|---|
| add() | adds an element to the set. |
| update() | updates the current set, by adding items from another iterable. |
| pop() | removes a random item from the set. |
| remove() | removes a specified element from the set. It requires one argument (item). |
| discard() | removes the specified item from the set. |
| clear() | removes all elements in a set. |
| clear() | removes all elements in a set. |
add(elmnt)¶required argument (the element we want to add)colors = {"red", "blue", "green"}
colors.add("orange")
print(colors)
{'red', 'green', 'blue', 'orange'}
update(iterable)¶# notice: red is repeated but it will appear once in the new updated set
x = {"red", "blue", "green"}
y = {"brown", "red"}
x.update(y)
print(x)
{'brown', 'red', 'green', 'blue'}
pop()¶colors = {"red", "blue", "green"}
colors.pop()
print(colors)
{'green', 'blue'}
remove(item)¶colors = {"red", "blue", "green"}
colors.remove("blue")
print(colors)
{'red', 'green'}
discard(value)¶colors = {"red", "blue", "green"}
colors.discard("red")
print(colors)
{'green', 'blue'}
clear()¶colors = {"red", "blue", "green"}
colors.clear()
print(colors)
set()
union(set1, set2)¶x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.union(y)
print(z)
intersection()¶x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
difference()¶x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z)