LC 263 / LING 398
Elsayed Issa
Fall 2024

Tuple Methods

Method Meaning
index() finds the first occurrence of a value.
count() returns the number of times a value is in a tuple.

index(value)¶

  • finds the first occurrence of a value. It takes one required argument (value).
In [8]:
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).

In [9]:
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

x = thistuple.count(5)

print(x) 
2

Set Methods

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)¶

  • adds an element to the set. It takes one required argument (the element we want to add)
In [2]:
colors = {"red", "blue", "green"}

colors.add("orange")

print(colors) 
{'red', 'green', 'blue', 'orange'}

update(iterable)¶

  • updates the current set, by adding items from another any other iterable.
In [3]:
# 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()¶

  • removes a random item from the set.
In [4]:
colors = {"red", "blue", "green"}

colors.pop()

print(colors) 
{'green', 'blue'}

remove(item)¶

  • removes a specified element from the set. It requires one argument (item).
In [5]:
colors = {"red", "blue", "green"}

colors.remove("blue")

print(colors)
{'red', 'green'}

discard(value)¶

  • removes the specified item from the set.
In [6]:
colors = {"red", "blue", "green"}

colors.discard("red")

print(colors) 
{'green', 'blue'}

clear()¶

  • removes all elements in a set.
In [7]:
colors = {"red", "blue", "green"}

colors.clear()

print(colors) 
set()

Some other methods

union(set1, set2)¶

In [ ]:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.union(y)

print(z) 

intersection()¶

In [ ]:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.intersection(y)

print(z) 

difference()¶

In [ ]:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.difference(y)

print(z) 
In [ ]:
 
In [ ]: