Photo by Hitesh Choudhary on Unsplash
3 min read
I will be pointing out things that seem unique to me here in this article except for semicolons. I know we don't need semicolons in python.
We will be using REPL to learn basic concepts for now.
➜ python3 Python 3.9.5 (default, May 4 2021, 03:36:27) [Clang 12.0.0 (clang-1184.108.40.206)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.sqrt(81) 9.0 >>>
Here, We are importing math as the module and the
sqrt is the attribute inside it, which is basically function.
We may know that
sqrt belongs to the math module but what about other methods in the math module. We can ask for help with Python REPL for it.
>>> help Type help() for interactive help, or help(object) for help about the object. >>>
Let's ask for help for the math module.
Help on module math: NAME math DESCRIPTION This module provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(x, /) Return the arc cosine (measured in radians) of x. The result is between 0 and pi. acosh(x, /) Return the inverse hyperbolic cosine of x. asin(x, /) Return the arc sine (measured in radians) of x. The result is between -pi/2 and pi/2. asinh(x, /) Return the inverse hyperbolic sine of x. atan(x, /) Return the arc tangent (measured in radians) of x. The result is between -pi/2 and pi/2. atan2(y, x, /) Return the arc tangent (measured in radians) of y/x. Unlike atan(y/x), the signs of both x and y are considered. atanh(x, /) Return the inverse hyperbolic tangent of x. ceil(x, /) Return the ceiling of x as an Integral. This is the smallest integer >= x. comb(n, k, /) Number of ways to choose k items from n items without repetition and without order. Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates to zero when k > n. Also called the binomial coefficient because it is equivalent to the coefficient of k-th term in polynomial expansion of the : .....
The list is huge, you can scroll down your terminal to find a full list of the functions, properties, and file available on the module.
..... DATA e = 2.718281828459045 inf = inf nan = nan pi = 3.141592653589793 tau = 6.283185307179586 FILE /usr/local/Cellaremail@example.com/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload/math.cpython -39-darwin.so
Ok, a few properties seems to be available as properties like:
>>> math.pi 3.141592653589793 >>> math.inf inf >>> math.e 2.718281828459045 >>> math.nan nan >>> math.tau 6.283185307179586 >>>
You can even ask for help for the specific functions
help(math.factorial) Help on built-in function factorial in module math: factorial(x, /) Find x!. Raise a ValueError if x is negative or non-integral.
>>> math.factorial(5) 120 >>>
Let's do more calculation
>>> n=5 >>> k=3 >>> math.factorial(n) / (math.factorial(k) * math.factorial(n-k)) 10.0 >>>
Different ways of importing modules
- We can import the attribute directly from the module
>>> from math import factorial >>> factorial(5) 120 >>>
- We can even rename the attribute imported
>>> from math import factorial as fac >>> fac(5) 120 >>>
# / = "Floating division" # // = "Inter division" fac(n) / (fac(k) * fac(n-k)) 10.0 fac(n) // (fac(k) * fac(n-k)) 10
Thank you and to be contained.