# Setanimals={'cat','dog'}print'cat'inanimals# Check if an element is in a set; prints "True"print'fish'inanimals# prints "False"animals.add('fish')# Add an element to a setprint'fish'inanimals# Prints "True"printlen(animals)# Number of elements in a set; prints "3"animals.add('cat')# Adding an element that is already in the set does nothingprintlen(animals)# Prints "3"animals.remove('cat')# Remove an element from a setprintlen(animals)# Prints "2"

# Dictionaryd={'cat':'cute','dog':'furry'}# Create a new dictionary with some dataprintd['cat']# Get an entry from a dictionary; prints "cute"print'cat'ind# Check if a dictionary has a given key; prints "True"d['fish']='wet'# Set an entry in a dictionaryprintd['fish']# Prints "wet"printd['monkey']# KeyError: 'monkey' not a key of d

printd.get('monkey','N/A')# Get an element with a default; prints "N/A"printd.get('fish','N/A')# Get an element with a default; prints "wet"deld['fish']# Remove an element from a dictionaryprintd.get('fish','N/A')# "fish" is no longer a key; prints "N/A"

# Numpy array indexingimportnumpyasnp# Create the following rank 2 array with shape (3, 4)# [[ 1 2 3 4]# [ 5 6 7 8]# [ 9 10 11 12]]a=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])# Use slicing to pull out the subarray consisting of the first 2 rows# and columns 1 and 2; b is the following array of shape (2, 2):# [[2 3]# [6 7]]b=a[:2,1:3]# A slice of an array is a view into the same data, so modifying it# will modify the original array.printa[0,1]# Prints "2"b[0,0]=77# b[0, 0] is the same piece of data as a[0, 1]printa[0,1]# Prints "77"

2
77

In [75]:

#Integer array indexingimportnumpyasnpa=np.array([[1,2],[3,4],[5,6]])# An example of integer array indexing.# The returned array will have shape (3,) and printa[[0,1,2],[0,1,0]]# Prints "[1 4 5]"# The above example of integer array indexing is equivalent to this:printnp.array([a[0,0],a[1,1],a[2,0]])# Prints "[1 4 5]"# When using integer array indexing, you can reuse the same# element from the source array:printa[[0,0],[1,1]]# Prints "[2 2]"# Equivalent to the previous integer array indexing exampleprintnp.array([a[0,1],a[0,1]])# Prints "[2 2]"

[1 4 5]
[1 4 5]
[2 2]
[2 2]

In [77]:

importnumpyasnp# Create a new array from which we will select elementsa=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])printa# prints "array([[ 1, 2, 3],# [ 4, 5, 6],# [ 7, 8, 9],# [10, 11, 12]])"# Create an array of indicesb=np.array([0,2,0,1])# Select one element from each row of a using the indices in bprinta[np.arange(4),b]# Prints "[ 1 6 7 11]"# Mutate one element from each row of a using the indices in ba[np.arange(4),b]+=10printa# prints "array([[11, 2, 3],# [ 4, 5, 16],# [17, 8, 9],# [10, 21, 12]])

# Boolean array indexingimportnumpyasnpa=np.array([[1,2],[3,4],[5,6]])bool_idx=(a>2)# Find the elements of a that are bigger than 2;# this returns a numpy array of Booleans of the same# shape as a, where each slot of bool_idx tells# whether that element of a is > 2.printbool_idx# Prints "[[False False]# [ True True]# [ True True]]"# We use boolean array indexing to construct a rank 1 array# consisting of the elements of a corresponding to the True values# of bool_idxprinta[bool_idx]# Prints "[3 4 5 6]"# We can do all of the above in a single concise statement:printa[a>2]# Prints "[3 4 5 6]"