Udemy Code Review Flashcards
num = 12 name= Sam
Print ‘My number is 12 and my name is Sam’ in two ways
print(‘My number is {} and my name is {}’.format(num, name)) print(‘My number is {one} and my name is {two}’.format(one=num, two=name))
d = {‘k1’:[1,2,3]}
Grab number 2
d[‘k1’][1]
What is a set?
set function
A collection of unique elements, eg. {1, 2, 3}
If put in multiples, will return only uniques. e.g. {1, 1, 2, 2, 3, 3} will return {1, 2, 3}
You can pass a list to a set function to get the unique elements:
set([1, 1, 2, 3, 4 ,4, 6, 6])
Returns: {1, 2, 3, 4, 6}
Add 5 to this set:
s = {1, 2, 3}
s.add(5)
split method
string.split(‘delimiter’)
It splits a string into a list based on the delimiter, which is space by default
e.g.
s = ‘hello my name is Sam’
s.split() returns: [‘hello’, ‘my’, ‘name’, ‘is’, ‘Sam’]
Tuple unpacking
x = [(1, 2), (3, 4), (5, 6)]
print 1, 3, 5
for a, b in x:
print(a)
numpy equivalent of range function?
np.arange(start, stop, increment)
Make an 1x3 array of zeros
Make a 5x5 array of zeros
pass array dimensions as a tuple
np. zeros(3)
np. zeros((5, 5))
linspace
np.linspace(start, stop, numberpoints)
Creates an evenly spaced sequence of numbers of desired length (numberpoints)
Create a 4x4 identity matrix
np.eye(4)
Create 1x5 array with random numbers from uniform distribution
5x5 array same
This returns random numbers between 0 and 1
np. random.rand(5)
np. random.rand(5, 5)
Create 2x3 array of random numbers from the normal distribution
np.random.randn(2, 3)
Create an array of 10 random integers from 1 to 99
low number is included but high number is not
np. random.randint(1, 100, 10)
np. random.randint(low, high, numberpoints)
How do you reshape a 1x25 array into a 5x5 array? Use array ‘arr’
arr.reshape(5, 5)
Get the maximum value of the array ‘arr’
Get the minimum value of the array ‘arr’
Get the location of the maximum value of the array ‘arr’
returns index value of max value
arr. max()
arr. min()
arr. argmax()