PythonForDataSciences_Week2
PythonForDataSciences_Week2
These can offer unique functionalities for the variables to contain and handle more than one data datatype at a time. Supports
operations such as indexing, slicing, concatenation, multiplication, etc.
learning
print(lstnumbers)
[1, 2, 3, 3, 3, 4, 5]
print(lstsample)
(1, 2, 3, 4, 3, 'py')
print(tuplesample)
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 1 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
(1, 2, 'sample')
print(rangesample)
for x in rangesample: print(x)
#print the values of 'rangesample'
range(1, 12, 4)
1
5
9
index() method finds the first occurrence of the specified value and returns its position
String Indexing
Out[27]: 0
In [30]: strSample.index('ning')
#to find the index of substring 'ning' from the string 'learning'
Out[30]: 4
In [35]: strSample[4]
#to find the substring corresponding to the 5th position
Out[35]: 'n'
In [34]: strSample[-3]
#to find the substring corresponding to 3rd last position
Out[34]: 'i'
In [37]: strSample[-9]
#IndexError: string index out of range
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 2 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[37], line 1
----> 1 strSample[-9]
List Indexing
Syntax: list_name.index(element, start, end)
Out[38]: 3
In [39]: lstSample[2]
#to find the element corresponding to the 3rd position in the list
Out[39]: 'a'
In [40]: lstSample[-1]
Out[40]: 2
Array Indexing
1
2
3
4
In [54]: arrSample.index(2)
Out[54]: 1
In [49]: arrSample[-2]
Out[49]: 3
Tuple Indexing
Out[50]: 5
Out[55]: 3
Set Indexing
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[58], line 2
1 setSample = {'example', 24, 87.5, 'data', 24, 'data'} #sets
----> 2 setSample[4]
Dictionary Indexing
The Python Dictionary object provides a key-value indexing facility The values in the dictionary are indexed by keys, they are not held
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 3 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
in any order
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[59], line 2
1 dictSample = {1:'first', 'second':2, 3:3, 'four':'4'} #dictionary
----> 2 dictSamplep[2]
Out[62]: 'first'
Out[63]: 2
Range Indexing
1
5
9
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[65], line 1
----> 1 rangeSample.index(0)
Out[67]: 2
In [68]: rangeSample[1] #given the index, returns the element at that index
Out[68]: 5
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[69], line 1
----> 1 rangeSample[9]
In [72]: print(strSample)
strSample[slice(1,4,2)] #getting substring 'er'
learning
Out[72]: 'er'
Out[73]: 'learning'
In [74]: print(lstSample)
print(lstSample[:3]) #slicing starts at the beginning index of the list
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 4 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
In [75]: lstSample[2:4]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[77], line 1
----> 1 dictSample[1:'second']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[78], line 1
----> 1 setSample[1:2]
1
2
3
4
Out[79]: array('i', [2, 3, 4])
learning python
learning
('learning ', 'python')
In [89]: print(arrSample)
arrSample+[50,60]
#TypeError: can only append array (not 'list') to array
In [98]: arrSample+array('i',[50,60])
#array('i',[1,2,3,4,50,60])
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 5 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
In [100… print(setSample)
setSample = setSample,24
#Converts to a tuple with comma-separated elements of set, dict, range
print(setSample)
learninglearninglearning
In [103… lstSample[1]*2 #4
Out[103… 4
In [105… print(tupSample)
tupSample[2:4]*2
#(3,4,3,4) : Concatenate sliced tuple twice
In [107… rangeSample*2
#TypeError: unsupported operand type(s) for *: 'range' and 'int'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[107], line 1
----> 1 rangeSample*2
learning is fun !
In [109… strSample.capitalize()
#returns the string with its
#first character capitalised and the rest lowercased
In [110… strSample.casefold()
#return a casefold copy of the string
#it is intended to remove all case distinctions in a string
In [111… strSample.title()
#to capitalise the first character of each word
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 6 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
In [112… strSample.swapcase()
#to swap the case of strings
In [115… strSample.find('n')
#to find the index of the given letter
Out[115… 4
In [116… strSample.count('a')
#to count total number of 'a' in the string
Out[116… 1
In [117… strSample.replace('fun','joyful')
#to replace the letters/word
In [118… strSample.isalnum()
#returns true if all bytes in the sequence are
#alphabetical ASCII characters or ASCII decimal digits
#false otherwise
Out[118… False
The below code will show all the functions that we can use for the particular variable:
In [123… print(dir(name))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '
__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_sub
class__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__
reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'ca
pitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'in
dex', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintab
le', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix
', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines'
, 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
In [124… print(help(str))
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __format__(self, format_spec, /)
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 7 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 8 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
| encoding
| The encoding in which to encode the string.
| errors
| The error handling scheme to use for encoding errors.
| The default is 'strict' meaning that encoding errors raise a
| UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
| 'xmlcharrefreplace' as well as any other name registered with
| codecs.register_error that can handle UnicodeEncodeErrors.
|
| endswith(...)
| S.endswith(suffix[, start[, end]]) -> bool
|
| Return True if S ends with the specified suffix, False otherwise.
| With optional start, test S beginning at that position.
| With optional end, stop comparing S at that position.
| suffix can also be a tuple of strings to try.
|
| expandtabs(self, /, tabsize=8)
| Return a copy where all tab characters are expanded using spaces.
|
| If tabsize is not given, a tab size of 8 characters is assumed.
|
| find(...)
| S.find(sub[, start[, end]]) -> int
|
| Return the lowest index in S where substring sub is found,
| such that sub is contained within S[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Return -1 on failure.
|
| format(...)
| S.format(*args, **kwargs) -> str
|
| Return a formatted version of S, using substitutions from args and kwargs.
| The substitutions are identified by braces ('{' and '}').
|
| format_map(...)
| S.format_map(mapping) -> str
|
| Return a formatted version of S, using substitutions from mapping.
| The substitutions are identified by braces ('{' and '}').
|
| index(...)
| S.index(sub[, start[, end]]) -> int
|
| Return the lowest index in S where substring sub is found,
| such that sub is contained within S[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Raises ValueError when the substring is not found.
|
| isalnum(self, /)
| Return True if the string is an alpha-numeric string, False otherwise.
|
| A string is alpha-numeric if all characters in the string are alpha-numeric and
| there is at least one character in the string.
|
| isalpha(self, /)
| Return True if the string is an alphabetic string, False otherwise.
|
| A string is alphabetic if all characters in the string are alphabetic and there
| is at least one character in the string.
|
| isascii(self, /)
| Return True if all characters in the string are ASCII, False otherwise.
|
| ASCII characters have code points in the range U+0000-U+007F.
| Empty string is ASCII too.
|
| isdecimal(self, /)
| Return True if the string is a decimal string, False otherwise.
|
| A string is a decimal string if all characters in the string are decimal and
| there is at least one character in the string.
|
| isdigit(self, /)
| Return True if the string is a digit string, False otherwise.
|
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 9 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
| A string is a digit string if all characters in the string are digits and there
| is at least one character in the string.
|
| isidentifier(self, /)
| Return True if the string is a valid Python identifier, False otherwise.
|
| Call keyword.iskeyword(s) to test whether string s is a reserved identifier,
| such as "def" or "class".
|
| islower(self, /)
| Return True if the string is a lowercase string, False otherwise.
|
| A string is lowercase if all cased characters in the string are lowercase and
| there is at least one cased character in the string.
|
| isnumeric(self, /)
| Return True if the string is a numeric string, False otherwise.
|
| A string is numeric if all characters in the string are numeric and there is at
| least one character in the string.
|
| isprintable(self, /)
| Return True if the string is printable, False otherwise.
|
| A string is printable if all of its characters are considered printable in
| repr() or if it is empty.
|
| isspace(self, /)
| Return True if the string is a whitespace string, False otherwise.
|
| A string is whitespace if all characters in the string are whitespace and there
| is at least one character in the string.
|
| istitle(self, /)
| Return True if the string is a title-cased string, False otherwise.
|
| In a title-cased string, upper- and title-case characters may only
| follow uncased characters and lowercase characters only cased ones.
|
| isupper(self, /)
| Return True if the string is an uppercase string, False otherwise.
|
| A string is uppercase if all cased characters in the string are uppercase and
| there is at least one cased character in the string.
|
| join(self, iterable, /)
| Concatenate any number of strings.
|
| The string whose method is called is inserted in between each given string.
| The result is returned as a new string.
|
| Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
|
| ljust(self, width, fillchar=' ', /)
| Return a left-justified string of length width.
|
| Padding is done using the specified fill character (default is a space).
|
| lower(self, /)
| Return a copy of the string converted to lowercase.
|
| lstrip(self, chars=None, /)
| Return a copy of the string with leading whitespace removed.
|
| If chars is given and not None, remove characters in chars instead.
|
| partition(self, sep, /)
| Partition the string into three parts using the given separator.
|
| This will search for the separator in the string. If the separator is found,
| returns a 3-tuple containing the part before the separator, the separator
| itself, and the part after it.
|
| If the separator is not found, returns a 3-tuple containing the original string
| and two empty strings.
|
| removeprefix(self, prefix, /)
| Return a str with the given prefix string removed if present.
|
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 10 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 11 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
| When set to None (the default value), will split on any whitespace
| character (including \n \r \t \f and spaces) and will discard
| empty strings from the result.
| maxsplit
| Maximum number of splits (starting from the left).
| -1 (the default value) means no limit.
|
| Note, str.split() is mainly useful for data that has been intentionally
| delimited. With natural text that includes punctuation, consider using
| the regular expression module.
|
| splitlines(self, /, keepends=False)
| Return a list of the lines in the string, breaking at line boundaries.
|
| Line breaks are not included in the resulting list unless keepends is given and
| true.
|
| startswith(...)
| S.startswith(prefix[, start[, end]]) -> bool
|
| Return True if S starts with the specified prefix, False otherwise.
| With optional start, test S beginning at that position.
| With optional end, stop comparing S at that position.
| prefix can also be a tuple of strings to try.
|
| strip(self, chars=None, /)
| Return a copy of the string with leading and trailing whitespace removed.
|
| If chars is given and not None, remove characters in chars instead.
|
| swapcase(self, /)
| Convert uppercase characters to lowercase and lowercase characters to uppercase.
|
| title(self, /)
| Return a version of the string where each word is titlecased.
|
| More specifically, words start with uppercased characters and all remaining
| cased characters have lower case.
|
| translate(self, table, /)
| Replace each character in the string using the given translation table.
|
| table
| Translation table, which must be a mapping of Unicode ordinals to
| Unicode ordinals, strings, or None.
|
| The table must implement lookup/indexing via __getitem__, for instance a
| dictionary or list. If this operation raises LookupError, the character is
| left untouched. Characters mapped to None are deleted.
|
| upper(self, /)
| Return a copy of the string converted to uppercase.
|
| zfill(self, width, /)
| Pad a numeric string with zeros on the left, to fill a field of the given width.
|
| The string is never truncated.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| maketrans(...)
| Return a translation table usable for str.translate().
|
| If there is only one argument, it must be a dictionary mapping Unicode
| ordinals (integers) or characters to Unicode ordinals, strings or None.
| Character keys will be then converted to ordinals.
| If there are two arguments, they must be strings of equal length, and
| in the resulting dictionary, each character in x will be mapped to the
| character at the same position in y. If there is a third argument, it
| must be a string, whose characters will be mapped to None in the result.
None
In [125… print(help(str.find))
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 12 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
Help on method_descriptor:
find(...)
S.find(sub[, start[, end]]) -> int
Return -1 on failure.
None
1
5
9
In [140… lstSample.reverse()
#reverses the order of the list
print(lstSample)
In [142… lstSample.clear()
print(lstSample)
[]
In [143… dictSample.clear()
print(dictSample)
{}
In [144… setSample.clear()
print(setSample)
set()
In [152… print(lstSample)
lstSample.append([2,4])
#adding [2,4] list to lstSample
In [155… setSample.append(20)
#AttributeError: 'set' object has no attribute 'append'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[155], line 1
----> 1 setSample.append(20)
In [269… setSample.add(20)
#add() takes single parameter(element) which needs to be added in the set
print(setSample)
update() function in set adds elements from a set (passed as an argument) to the set
This method takes only a single argument
The single argument can be a set, list, tuples or a dictionary
It automatically converts into a set and adds to the set
In [156… setSample.update([5,10])
#adding a list of elements to the set
Dictionary Methods
In [157… print(dictSample)
dictSample["five"] = 5
print(dictSample)
In [272… dictSample.update(five = 5)
#update the dictionary with the key/value pairs from other, overwriting existing keys
print(dictSample)
#updated dictionary
In [160… list(dictSample)
#returns a list of all the keys used in the dictionary dictSample
In [161… len(dictSample)
#returns the number of items in the dictionary
Out[161… 5
In [162… dictSample.get("five")
#it is a conventional method to access a value for a key
Out[162… 5
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 14 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
In [163… dictSample.keys()
#returns list of keys in dictionary
In [164… dictSample.items()
#returns a list of (key,value) tuple pairs
Out[164… dict_items([(1, 'first'), ('second', 2), (3, 3), ('four', '4'), ('five', 5)])
In [166… print(arrSample)
In [167… arrSample.insert(1,100)
#inserting the element '100' at 2nd position
In [168… lstSample.insert(5,24)
#inserting the element '24' at 6th position
print(lstSample)
#printing list
pop() - Removes the element at the given index from the object and prints the same
Syntax: object.pop(index)
Default value is -1 if index not specified, which returns the last item
Supported datatypes: array, list, set, dictionary
In [169… arrSample.pop()
#deleting the last element and prints the same
Out[169… 3
In [176… print(lstSample)
lstSample.pop(4)
#deleting the 5th element
In [172… print(dictSample)
dictSample.pop('second')
#deleting the key 'second'
Out[173… 3
remove() - Removes the first occurrence of the element with the specified value
Syntax: object.remove(value) Supported Data types: array, list, dictionary, set
In [177… print(arrSample)
In [179… arrSample.remove(0)
#ValueError: array.remove(x): x not in array
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 15 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[179], line 1
----> 1 arrSample.remove(0)
In [180… arrSample.remove(2)
#removes the element 2 from the array
In [181… print(arrSample)
In [182… print(lstSample)
lstSample.remove('sam')
#removes the element 'sam' from the list
print(lstSample)
In [183… print(setSample)
setSample.remove(57)
#KeyError: 57
KeyError: 57
In [184… setSample.discard(57)
#The set remains unchanged if
#the element passed to discard() method doesnt exist
print(setSample)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[185], line 2
1 del setSample #deleting the set 'setSample'
----> 2 print(setSample)
print(arrSample)
#NameError: name 'arrSample' is not defined
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[186], line 2
1 del arrSample #deleting the array 'arrSample'
----> 2 print(arrSample)
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 16 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
print(lstSample)
print(lstSample)
[1]
print(lstSample)
[]
print(dictSample)
#NameError: name 'dictSample' is not defined
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[191], line 1
----> 1 del dictSample #deleting the dictionary 'dictSample'
2 print(dictSample)
extend() method adds the specified list elements (or any iterable - list, set, tuple etc.) to the end of the current
list
In [193… print(lstSample)
lstSample.extend([1,2,3])
print(lstSample)
[]
[1, 2, 3]
In [196… arrSample.extend((4,5,3,5))
#add a tuple to the 'arrSample' array
print(arrSample)
In [198… arrSample.extend(['sam'])
print(arrSample)
#TypeError: an integer is required (got type str)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[198], line 1
----> 1 arrSample.extend(['sam'])
2 print(arrSample)
In [199… arrSample.fromlist([3,4])
#add values from a list to an array
print(arrSample)
In [200… arrSample.tolist()
#to convert an array into an ordinary list with the same items
Out[200… [1, 2, 3, 4, 4, 5, 3, 5, 3, 4]
Set Operations
A set is an unordered collection of items
Every element is unique (no duplicates)
Sets can be used to perform mathematical set operations like union, intersection, symmetric difference, etc.
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 17 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
print(A)
In [203… B = {24,100}
#set of integers
print(B)
{24, 100}
In [212… print(A | B)
#Union of A and B is a set of all elements from both sets
In [208… A.union(B)
#using union() function on B
{24}
In [214… A.intersection(B)
#using intersection() function on B
Out[214… {24}
Introduction to NumPy
NumPy is a Python package and it stands for Numerical Python
Fundamental package for numerical computations in Python
Supports N-dimensional array objects that can be used for processing multidimensional data
Supports different data types
Array
An array is a data structure that stores values of same data type
Lists can contain values corresponding to different data types
Arrays in Python can only contain values corresponding to same data type
NumPy Array
A NumPy Array is a grid of values, all of the same type, and is indexed by a tuple of non-negative integers
The number of dimensions is the rank of the array
The shape of an array is a tuple of integers giving the size of the array along each dimension
Creation of Array
[1, 2, 3, 4, 5, 6]
[1 2 3 4 5 6]
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 18 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
<class 'numpy.ndarray'>
6
1
(6,)
reshape() - Reshapes the NumPy array into 'i' number of rows containing 'j' number of elements
Syntax: array.reshape(i,j)
print(array2)
print(array2.shape)
[[1 2]
[3 4]
[5 6]]
(3, 2)
print(array3)
print(array3.ndim)
[[1 2]
[3 4]
[5 6]]
2
my_list2 = [1,2,3,4,5]
my_list3 = [2,3,4,5,6]
my_list4 = [9,7,6,8,9]
print(mul_arr.shape)
print(mul_arr.ndim)
[[1 2 3 4 5]
[2 3 4 5 6]
[9 7 6 8 9]]
(3, 5)
2
In [241… print(mul_arr.reshape(1,15))
[[1 2 3 4 5 2 3 4 5 6 9 7 6 8 9]]
NumPy Attributes
In [247… a = np.array([[1,2,3],[4,5,6]])
print(a)
print(a.shape)
[[1 2 3]
[4 5 6]]
(2, 3)
[[1 2]
[3 4]
[5 6]]
[[1 2]
[3 4]
[5 6]]
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 19 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
In [250… r = range(24)
print(r)
#all the values of range are not printed
range(0, 24)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
1
[[[ 0]
[ 1]
[ 2]
[ 3]]
[[ 4]
[ 5]
[ 6]
[ 7]]
[[ 8]
[ 9]
[10]
[11]]
[[12]
[13]
[14]
[15]]
[[16]
[17]
[18]
[19]]
[[20]
[21]
[22]
[23]]]
[[1. 2.]
[3. 4.]]
[[5. 6.]
[7. 8.]]
In [257… print(x+y)
print(np.add(x,y))
[[ 6. 8.]
[10. 12.]]
[[ 6. 8.]
[10. 12.]]
In [258… print(x - y)
print(np.subtract(x,y))
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 20 of 21
PythonForDataSciences_Week2 18/01/25, 5:11 PM
[[-4. -4.]
[-4. -4.]]
[[-4. -4.]
[-4. -4.]]
In [259… print(x * y)
print(np.multiply(x,y))
[[ 5. 12.]
[21. 32.]]
[[ 5. 12.]
[21. 32.]]
In [260… print(x.dot(y))
print(np.dot(x,y))
[[19. 22.]
[43. 50.]]
[[19. 22.]
[43. 50.]]
In [262… print(x / y)
print(np.divide(x,y))
[[0.2 0.33333333]
[0.42857143 0.5 ]]
[[0.2 0.33333333]
[0.42857143 0.5 ]]
numpy.floor_divide Returns the largest integer smaller or equal to the division of the inputs
numpy.power First array elements raised to powers from second array, element-wise
numpy.sum() - Returns sum of all array elements or sum of all array elements over a given axis
Syntax: numpy.sum(array,axis)
In [263… print(np.sum(x))
#Computes overall sum (axis = None)
10.0
[4. 6.]
[3. 7.]
file:///Users/abi/Downloads/PythonForDataSciences_Week2.html Page 21 of 21