Set, String, Tuple
Set, String, Tuple
Return a set that contains the items that only exist in set x, and not in set y:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
{'cherry', 'banana'}
print(z)
Reverse the first example. Return a set that contains the items
that only exist in set y, and not in set x:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
print(x)
The difference_update() method removes the items that exist in both sets.
The difference_update() method is different from the difference() method, because
the difference() method returns a new set, without the unwanted items, and
the difference_update() method removes the unwanted items from the original set.
Returns a set, that is the intersection of two or more sets
Return a set that contains the items that exist in both set x, and set y:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
{'apple'}
print(z)
Compare 3 sets, and return a set with items that is present in all 3 sets:
result = x.intersection(y, z)
print(result)
{'c'}
Removes the items in this set that are not present in other, specified
set(s)
x.intersection_update(y)
{'apple'}
print(x)
Compare 3 sets, and return a set with items that is present in all 3 sets:
x.intersection_update(y, z) {'c'}
print(x)
Returns whether two sets have a intersection or not
print(z)
z = x.isdisjoint(y)
False
print(z)
Returns whether another set contains this set or not
z = x.issubset(y)
print(z) True
z = x.issubset(y)
False
print(z)
Returns whether this set contains another set or not
z = x.issuperset(y)
True
print(z)
z = x.issuperset(y) False
print(z)
Returns a set with the symmetric differences of two sets
Return a set that contains all items from both sets, except items that
are present in both sets:
z = x.symmetric_difference(y)
x.symmetric_difference_update(y)
print("Hello")
Hello
print('Hello')
Hello
Note: in the result, the line breaks are inserted at the same position as in the code.
Strings are Arrays
Like many other popular programming languages, strings in Python are arrays of bytes representing
unicode characters.
However, Python does not have a character data type, a single character is simply a string with a
length of 1.
Square brackets can be used to access elements of the string.
a = "Hello, World!"
print(a[1])
e
for x in "banana": b
print(x) a
n
a
n
a
String Length
To get the length of a string, use the len() function.
a = "Hello, World!"
print(len(a)) 13
Check String
To check if a certain phrase or character is present in a string, we can use the keyword in.
Check if NOT
To check if a certain phrase or character is NOT present in a string, we can use the
keyword not in.
txt = "The best things in life are free!"
print("expensive" not in txt)
True
Use it in an if statement:
txt = "The best things in life are free!" No, 'expensive' is NOT
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
present.
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Set, and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
Ordered
When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.
Allow Duplicates
Since tuples are indexed, they can have items with the same value:
thistuple = ("apple", "banana", "cherry") Note: The first item has index 0.
print(thistuple[1])
Negative Indexing
Negative indexing means start from the end.
-1 refers to the last item, -2 refers to the second last item etc.
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new tuple with the specified items.
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
Note: The search will start at index 2 (included) and end at index 5 (not included).
This example returns the items from the beginning to, but NOT included, "kiwi":
This example returns the items from "cherry" and to the end:
print(x)
Add Items
Since tuples are immutable, they do not have a built-in append() method, but there are other ways
to add items to a tuple.
1. Convert into a list: Just like the workaround for changing a tuple, you can convert it into a list,
add your item(s), and convert it back into a tuple.
print(thistuple)
Note: When creating a tuple with only one item, remember to include a comma after the item,
otherwise it will not be identified as a tuple.
Loop Through a Tuple