Skip to content

Commit f4be8fb

Browse files
committed
Add python sets
1 parent 3cc287b commit f4be8fb

5 files changed

Lines changed: 548 additions & 0 deletions

File tree

README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ All contributions are welcome:
7979
- [The get() Method](#the-get-method)
8080
- [The setdefault() Method](#the-setdefault-method)
8181
- [Pretty Printing](#pretty-printing)
82+
- [sets](#sets)
83+
- [Initializing a set](#initializing-a-set)
84+
- [sets: unordered collections of unique elements](#sets-unordered-collections-of-unique-elements)
85+
- [set add() and update()](#set-add-and-update)
86+
- [set remove() and discard()](#set-remove-and-discard)
87+
- [set union()](#set-union)
88+
- [set intersection](#set-intersection)
89+
- [set difference](#set-difference)
90+
- [set symetric_difference](#set-symetricdifference)
8291
- [itertools Module](#itertools-module)
8392
- [accumulate()](#accumulate)
8493
- [combinations()](#combinations)
@@ -1529,6 +1538,143 @@ Using `setdefault` we could make the same code more shortly:
15291538

15301539
[*Return to the Top*](#python-cheatsheet)
15311540

1541+
## sets
1542+
1543+
From the Python 3 [documentation](https://docs.python.org/3/tutorial/datastructures.html)
1544+
1545+
> A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
1546+
1547+
### Initializing a set
1548+
1549+
There are two ways to create sets: using curly braces `{}` and the bult-in function `set()`
1550+
1551+
```python
1552+
>>> s = {1, 2, 3}
1553+
>>> s = set([1, 2, 3])
1554+
```
1555+
1556+
When creating an empty set, be sure to not use the curly braces `{}` or you will get an empty dictionary instead.
1557+
1558+
```python
1559+
>>> s = {}
1560+
>>> type(s)
1561+
<class 'dict'>
1562+
```
1563+
1564+
### sets: unordered collections of unique elements
1565+
1566+
A set automatically remove all the duplicate values.
1567+
1568+
```python
1569+
>>> s = {1, 2, 3, 2, 3, 4}
1570+
>>> s
1571+
{1, 2, 3, 4}
1572+
```
1573+
1574+
And as an unordered data type, they can't be indexed.
1575+
1576+
```python
1577+
>>> s = {1, 2, 3}
1578+
>>> s(0)
1579+
Traceback (most recent call last):
1580+
File "<stdin>", line 1, in <module>
1581+
TypeError: 'set' object is not callable
1582+
>>>
1583+
```
1584+
1585+
### set add() and update()
1586+
1587+
Using the `add()` method we can add a single element to the set.
1588+
1589+
```python
1590+
>>> s = {1, 2, 3}
1591+
>>> s.add(4)
1592+
>>> s
1593+
{1, 2, 3, 4}
1594+
```
1595+
1596+
And with `update()`, multiple ones .
1597+
1598+
```python
1599+
>>> s = {1, 2, 3}
1600+
>>> s.update([2, 3, 4, 5, 6])
1601+
>>> s
1602+
{1, 2, 3, 4, 5, 6} # remember, sets automatically remove duplicates
1603+
```
1604+
1605+
### set remove() and discard()
1606+
1607+
Both methods will remove an element from the set, but `remove()` will raise a `key error` if the value doesn't exist.
1608+
1609+
```python
1610+
>>> s = {1, 2, 3}
1611+
>>> s.remove(3)
1612+
>>> s
1613+
{1, 2}
1614+
>>> s.remove(3)
1615+
Traceback (most recent call last):
1616+
File "<stdin>", line 1, in <module>
1617+
KeyError: 3
1618+
```
1619+
1620+
update()
1621+
1622+
```python
1623+
>>> s = {1, 2, 3}
1624+
>>> s.discard(3)
1625+
>>> s
1626+
{1, 2}
1627+
>>> s.discard(3)
1628+
>>>
1629+
```
1630+
1631+
### set union()
1632+
1633+
`union()` or `|` will create a new set that contains all the elements from the sets provided.
1634+
1635+
```python
1636+
>>> s1 = {1, 2, 3}
1637+
>>> s2 = {3, 4, 5}
1638+
>>> s1.union(s2) # or 's1 | s2'
1639+
{1, 2, 3, 4, 5}
1640+
```
1641+
1642+
### set intersection
1643+
1644+
`intersection` or `&` will return a set containing only the elements that are common to all of them.
1645+
1646+
```python
1647+
>>> s1 = {1, 2, 3}
1648+
>>> s2 = {2, 3, 4}
1649+
>>> s3 = {3, 4, 5}
1650+
>>> s1.intersection(s2, s3) # or 's1 & s2 & s3'
1651+
{3}
1652+
```
1653+
1654+
### set difference
1655+
1656+
`difference` or `-` will return only the elements that are in one of the sets.
1657+
1658+
```python
1659+
>>> s1 = {1, 2, 3}
1660+
>>> s2 = {2, 3, 4}
1661+
>>> s1.difference(s2) # or 's1 - s2'
1662+
{1}
1663+
```
1664+
1665+
### set symetric_difference
1666+
1667+
`symetric_difference` or `^` will return all the elements that are different between them.
1668+
1669+
```python
1670+
>>> s1 = {1, 2, 3}
1671+
>>> s2 = {2, 3, 4}
1672+
>>> s1.symmetric_difference(s2) # or 's1 ^ s2'
1673+
{1, 4}
1674+
```
1675+
1676+
[*Return to the Top*](#python-cheatsheet)
1677+
15321678
## itertools Module
15331679

15341680
The *itertools* module is a colection of tools intented to be fast and use memory efficiently when handling iterators (like [lists](#lists) or [dictionaries](#dictionaries-and-structuring-data)).

blog_files/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [Exception Handling](#exception-handling)
55
- [Lists](#lists)
66
- [Dictionaries and Structuring Data](#dictionaries-and-structuring-data)
7+
- [sets](#sets)
78
- [itertools Module](#itertools-module)
89
- [Comprehensions](#comprehensions)
910
- [Manipulating Strings](#manipulating-strings)

blog_files/pysheet.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,141 @@ Using `setdefault` we could make the same code more shortly:
12261226
'y': 1}
12271227
```
12281228

1229+
## sets
1230+
1231+
From the Python 3 [documentation](https://docs.python.org/3/tutorial/datastructures.html)
1232+
1233+
> A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
1234+
1235+
### Initializing a set
1236+
1237+
There are two ways to create sets: using curly braces `{}` and the bult-in function `set()`
1238+
1239+
```python
1240+
>>> s = {1, 2, 3}
1241+
>>> s = set([1, 2, 3])
1242+
```
1243+
1244+
When creating an empty set, be sure to not use the curly braces `{}` or you will get an empty dictionary instead.
1245+
1246+
```python
1247+
>>> s = {}
1248+
>>> type(s)
1249+
<class 'dict'>
1250+
```
1251+
1252+
### sets: unordered collections of unique elements
1253+
1254+
A set automatically remove all the duplicate values.
1255+
1256+
```python
1257+
>>> s = {1, 2, 3, 2, 3, 4}
1258+
>>> s
1259+
{1, 2, 3, 4}
1260+
```
1261+
1262+
And as an unordered data type, they can't be indexed.
1263+
1264+
```python
1265+
>>> s = {1, 2, 3}
1266+
>>> s(0)
1267+
Traceback (most recent call last):
1268+
File "<stdin>", line 1, in <module>
1269+
TypeError: 'set' object is not callable
1270+
>>>
1271+
```
1272+
1273+
### set add() and update()
1274+
1275+
Using the `add()` method we can add a single element to the set.
1276+
1277+
```python
1278+
>>> s = {1, 2, 3}
1279+
>>> s.add(4)
1280+
>>> s
1281+
{1, 2, 3, 4}
1282+
```
1283+
1284+
And with `update()`, multiple ones .
1285+
1286+
```python
1287+
>>> s = {1, 2, 3}
1288+
>>> s.update([2, 3, 4, 5, 6])
1289+
>>> s
1290+
{1, 2, 3, 4, 5, 6} # remember, sets automatically remove duplicates
1291+
```
1292+
1293+
### set remove() and discard()
1294+
1295+
Both methods will remove an element from the set, but `remove()` will raise a `key error` if the value doesn't exist.
1296+
1297+
```python
1298+
>>> s = {1, 2, 3}
1299+
>>> s.remove(3)
1300+
>>> s
1301+
{1, 2}
1302+
>>> s.remove(3)
1303+
Traceback (most recent call last):
1304+
File "<stdin>", line 1, in <module>
1305+
KeyError: 3
1306+
```
1307+
1308+
update()
1309+
1310+
```python
1311+
>>> s = {1, 2, 3}
1312+
>>> s.discard(3)
1313+
>>> s
1314+
{1, 2}
1315+
>>> s.discard(3)
1316+
>>>
1317+
```
1318+
1319+
### set union()
1320+
1321+
`union()` or `|` will create a new set that contains all the elements from the sets provided.
1322+
1323+
```python
1324+
>>> s1 = {1, 2, 3}
1325+
>>> s2 = {3, 4, 5}
1326+
>>> s1.union(s2) # or 's1 | s2'
1327+
{1, 2, 3, 4, 5}
1328+
```
1329+
1330+
### set intersection
1331+
1332+
`intersection` or `&` will return a set containing only the elements that are common to all of them.
1333+
1334+
```python
1335+
>>> s1 = {1, 2, 3}
1336+
>>> s2 = {2, 3, 4}
1337+
>>> s3 = {3, 4, 5}
1338+
>>> s1.intersection(s2, s3) # or 's1 & s2 & s3'
1339+
{3}
1340+
```
1341+
1342+
### set difference
1343+
1344+
`difference` or `-` will return only the elements that are in one of the sets.
1345+
1346+
```python
1347+
>>> s1 = {1, 2, 3}
1348+
>>> s2 = {2, 3, 4}
1349+
>>> s1.difference(s2) # or 's1 - s2'
1350+
{1}
1351+
```
1352+
1353+
### set symetric_difference
1354+
1355+
`symetric_difference` or `^` will return all the elements that are different between them.
1356+
1357+
```python
1358+
>>> s1 = {1, 2, 3}
1359+
>>> s2 = {2, 3, 4}
1360+
>>> s1.symmetric_difference(s2) # or 's1 ^ s2'
1361+
{1, 4}
1362+
```
1363+
12291364
## itertools Module
12301365

12311366
The *itertools* module is a collection of tools intented to be fast and use memory efficiently when handling iterators (like [lists](#lists) or [dictionaries](#dictionaries-and-structuring-data)).

0 commit comments

Comments
 (0)