0% found this document useful (0 votes)
24 views

Set, String, Tuple

The document discusses various methods for working with sets and tuples in Python. It explains that sets can be used to perform operations like unions, intersections, differences etc. on other sets. Tuples are ordered and unchangeable collections of items that can contain duplicate values. The document demonstrates how to access, slice and iterate through tuple elements. However, once created, tuple values cannot be changed unlike lists.

Uploaded by

aakimbekov20
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Set, String, Tuple

The document discusses various methods for working with sets and tuples in Python. It explains that sets can be used to perform operations like unions, intersections, differences etc. on other sets. Tuples are ordered and unchangeable collections of items that can contain duplicate values. The document demonstrates how to access, slice and iterate through tuple elements. However, once created, tuple values cannot be changed unlike lists.

Uploaded by

aakimbekov20
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Returns a set containing the difference between two or more sets

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"}

z = y.difference(x) {'google', 'microsoft'}


print(z)
Removes the items in this set that are also included in another, specified
set

Remove the items that exist in both sets:

x = {"apple", "banana", "cherry"}


y = {"google", "microsoft", "apple"}
{'banana', 'cherry'}
x.difference_update(y)

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:

x = {"a", "b", "c"}


y = {"c", "d", "e"}
z = {"f", "g", "c"}

result = x.intersection(y, z)

print(result)
{'c'}
Removes the items in this set that are not present in other, specified
set(s)

Remove the items that is not present in both x and y:


x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.intersection_update(y)
{'apple'}
print(x)

Compare 3 sets, and return a set with items that is present in all 3 sets:

x = {"a", "b", "c"}


y = {"c", "d", "e"}
z = {"f", "g", "c"}

x.intersection_update(y, z) {'c'}

print(x)
Returns whether two sets have a intersection or not

Return True if no items in set x is present in set y:


x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "facebook"}
True
z = x.isdisjoint(y)

print(z)

x = {"apple", "banana", "cherry"}


y = {"google", "microsoft", "apple"}

z = x.isdisjoint(y)
False
print(z)
Returns whether another set contains this set or not

Return True if all items in set x are present in set y:


x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}

z = x.issubset(y)

print(z) True

x = {"a", "b", "c"}


y = {"f", "e", "d", "c", "b"}

z = x.issubset(y)
False
print(z)
Returns whether this set contains another set or not

Return True if all items set y are present in set x:


x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}

z = x.issuperset(y)
True
print(z)

x = {"f", "e", "d", "c", "b"}


y = {"a", "b", "c"}

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:

x = {"apple", "banana", "cherry"}


y = {"google", "microsoft", "apple"}

z = x.symmetric_difference(y)

print(z) {'google', 'banana', 'microsoft', 'cherry'}


inserts the symmetric differences from this set and another

Remove the items that are present in both sets,


AND insert the items that is not present in both sets:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.symmetric_difference_update(y)

print(x) {'banana', 'microsoft', 'google', 'cherry'}


Strings
Strings in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".
You can display a string literal with the print() function:

print("Hello")
Hello
print('Hello')
Hello

Assign String to a Variable


a = "Hello"
print(a)
Hello
Multiline Strings
You can assign a multiline string to a variable by using three quotes:

a = """Lorem ipsum dolor sit amet,


consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.""" Lorem ipsum dolor sit amet,
print(a) consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
a = ’’’Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.’’’
print(a)

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

Looping Through a String


Since strings are arrays, we can loop through the characters in a string, with a for loop.

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.

txt = "The best things in life are free!"


print("free" in txt)
True
Use it in an if statement:
txt = "The best things in life are free!"
if "free" in txt: Yes, 'free' is present.
print("Yes, 'free' is present.")

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", "apple", "cherry")


print(thistuple)
Access Tuple Items
You can access tuple items by referring to the index number, inside square brackets:

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.

thistuple = ("apple", "banana", "cherry")


print(thistuple[-1])

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":

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")


print(thistuple[:4])

This example returns the items from "cherry" and to the end:

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")


print(thistuple[2:])
Change Tuple Values
Once a tuple is created, you cannot change its values. Tuples are unchangeable,
or immutable as it also is called.
But there is a workaround. You can convert the tuple into a list, change the list, and convert the
list back into a tuple.

x = ("apple", "banana", "cherry")


y = list(x)
y[1] = "kiwi"
x = tuple(y)

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.

thistuple = ("apple", "banana", "cherry")


y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
2. Add tuple to a tuple. You are allowed to add tuples to tuples, so if you want to add one item,
(or many), create a new tuple with the item(s), and add it to the existing tuple:

thistuple = ("apple", "banana", "cherry")


y = ("orange",)
thistuple += y

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

thistuple = ("apple", "banana", "cherry")


for x in thistuple:
print(x)
Join Two Tuples
To join two or more tuples you can use If you want to multiply the content of a tuple a given number
the + operator: of times, you can use the * operator:

tuple1 = ("a", "b" , "c") fruits = ("apple", "banana", "cherry")


tuple2 = (1, 2, 3) mytuple = fruits * 2

tuple3 = tuple1 + tuple2 print(mytuple)


print(tuple3)

You might also like