In [1]:
def another_atom(seen, atom):
for i in range(len(seen)):
if seen[i] == atom:
return # atom is already present, so do not re-add
seen.append(atom)
In [1]:
primes = {3, 5, 7}
set()
to create an empty set instead of {}
In [2]:
even_primes = set()
In [4]:
ten = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
lows = {0, 1, 2, 3, 4}
odds = {1, 3, 5, 7, 9}
In [6]:
print lows
In [7]:
print lows.union(odds)
In [8]:
print lows.intersection(odds)
In [9]:
print lows.difference(odds)
In [10]:
print lows.symmetric_difference(odds)
In [11]:
print lows.issubset(ten)
In [12]:
print lows.issuperset(odds)
In [13]:
print len(odds)
In [15]:
print 6 in odds
In [16]:
lows.add(9)
print lows
In [17]:
lows.remove(0)
print lows
In [18]:
lows.clear()
print lows
In [19]:
ten = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
lows = {0, 1, 2, 3, 4}
odds = {1, 3, 5, 7, 9}
In [20]:
print lows - odds # difference
In [21]:
print lows & odds # intersection
In [22]:
print lows <= ten # subset
In [23]:
print lows < ten # strict subset
In [24]:
print lows >= ten # superset
In [25]:
print lows > ten # strict superset
In [26]:
print lows ^ odds # symmetric difference
In [27]:
print lows | odds # union
In [28]:
def unique_atoms(filename):
atoms = set()
with open(filename, 'r') as source:
for line in source:
name = line.strip()
atoms.add(name)
return atoms
In [2]:
!cat some_atoms.txt
In [30]:
print unique_atoms('some_atoms.txt')
In [31]:
print set('lithium')