1. sets.py
#A set is an unordered collection of unique objects: a = {1, 2, 3} b = {3, 4, 5} b.add(6) b.remove(6) print(a) print(b) print(a.union(b)) #Python 3.X: (a | b) print(a.intersection(b)) #Python 3.X: (a & b) print(a.difference(b)) #Python 3.X: (a - b) print(b.difference(a)) #Python 3.X: (b - a) print(a.symmetric_difference(b)) #Python 3.X: (a ^ b) # Union = {1, 2, 3, 4, 5} # Intersection = {3} # Difference of a - b = {1, 2} # Difference of b - a = {4, 5} # Symmetric difference = {1, 2, 4, 5}
moves = set() moves.add("Barrel Roll") moves.add("Zerg Rush") print("Moves remaining:", moves) if "Zerg Rush" in moves: print("Now doing a Zerg Rush!") moves.remove("Zerg Rush") print("Moves remaining:", moves)
set
to add each of my guesses in a game of Battleships. If i have already guessed the co-ords, then the program should tell me:
Guess: A3
Targeting A3
Guess: C4
Targeting C4
Guess: A3
You've chosen those co-ordinates already
Guess: B2
Targeting B2