Click here to view code image
>>> a = numpy.array(([1, 2, 3], [4, 5, 6]))
>>> b = numpy.array(([7, 8, 9], [0, 1, 2]))
>>> result1 = a + b
>>> print(reuslt1)
[[ 8 10 12]
[ 4 6 8]]
>>> result2 = a * b
>>> print(result2)
[[ 7 16 27]
[ 0 5 12]]
>>>
Now working with arrays in Python is a breeze!
Summary
Python supports a wide range of mathematical features for just about any type of calculations you
need to perform in your scripts. You can perform standard math functions such as addition,
subtraction, and division directly by using the standard Python math operators.
If you need to incorporate more advanced math functions in your calculations, you can import the
math module into your script. The math module provides functions for number theory, trigonometry,
and basic statistics.
Finally, you may at some point need to get into advanced scientific or statistical calculations. Python
users have created some handy libraries for you. The most popular is the NumPy library, which
contains the tools required to perform calculations using multidimensional arrays for linear algebra,
advanced statistics, and signal processing.
In the next hour, we’ll take a look at how to add control to your Python programs using the if family
of control statements. That allows you to add dynamic features to your Python programs!
Q&A
Q. What data type should you use to store monetary values in Python?
A. You should use the floating point data type so that the value can contain two decimal places
for the cents value.
Q. Does Python support the incrementor (++) and decrementor (--) operators?
A. While the incrementor and decrementor operators are popular in other programming
languages, currently Python doesn’t provide support for those operators.
Workshop
Quiz
1. What math function should you use to find the square root of a number?
a. pow()
b. sqrt()
c. log2()
d. sin()