Python TutorialGetting Started with PythonPython Basic SyntaxPython DatatypesPython IndentationPython Collection TypesPython Basic Input and OutputPython Built in Modules and FunctionsPython FunctionsChemPy - python packageCreating Python packagesFunctional Programming in PythonIncompatibilities moving from Python 2 to Python 3IoT Programming with Python and Raspberry PIKivy - Cross-platform Python Framework for NUI DevelopmentMutable vs Immutable (and Hashable) in PythonPyInstaller - Distributing Python CodePython *args and **kwargsPython 2to3 toolPython Abstract Base Classes (abc)Python Abstract syntax treePython Alternatives to switch statement from other languagesPython and ExcelPython Anti-PatternsPython ArcPyPython ArraysPython Asyncio ModulePython Attribute AccessPython AudioPython Binary DataPython Bitwise OperatorsPython Boolean OperatorsPython Checking Path Existence and PermissionsPython ClassesPython CLI subcommands with precise help outputPython Code blocks, execution frames, and namespacesPython Collections modulePython Comments and DocumentationPython Common PitfallsPython Commonwealth ExceptionsPython ComparisonsPython Complex mathPython concurrencyPython ConditionalsPython configparserPython Context Managers (with Statement)Python Copying dataPython CountingPython ctypesPython Data SerializationPython Data TypesPython Database AccessPython Date and TimePython Date FormattingPython DebuggingPython DecoratorsPython Defining functions with list argumentsPython DeploymentPython Deque ModulePython DescriptorPython Design PatternsPython DictionaryPython Difference between Module and PackagePython DistributionPython DjangoPython Dynamic code execution with `exec` and `eval`Python EnumPython ExceptionsPython ExponentiationPython Files & Folders I/OPython FilterPython FlaskPython Functools ModulePython Garbage CollectionPython GeneratorsPython getting start with GZipPython graph-toolPython groupby()Python hashlibPython HeapqPython Hidden FeaturesPython HTML ParsingPython HTTP ServerPython IdiomsPython ijsonPython Immutable datatypes(int, float, str, tuple and frozensets)Python Importing modulesPython Indexing and SlicingPython Input, Subset and Output External Data Files using PandasPython Introduction to RabbitMQ using AMQPStorm

Python Built in Modules and Functions

From WikiOD

A module is a file containing Python definitions and statements. The function is a piece of code that execute some logic.

>>> pow(2,3)    #8

To check the built-in function in python we can use dir(). If called without an argument, return the names in the current scope. Else, return an alphabetized list of names comprising (some of) the attribute of the given object, and of attributes reachable from it.

>>> dir(__builtins__)

To know the functionality of any function, we can use built in function help .

>>> help(max)
Help on built-in function max in module __builtin__:
    max(iterable[, key=func]) -> value
    max(a, b, c, ...[, key=func]) -> value
    With a single iterable argument, return its largest item.
    With two or more arguments, return the largest argument.

Built-in modules contain extra functionalities. For example to get the square root of a number we need to include math module.

>>> import math
>>> math.sqrt(16) # 4.0

To know all the functions in a module we can assign the functions list to a variable, and then print the variable.

>>> import math
>>> dir(math)

   ['__doc__', '__name__', '__package__', 'acos', 'acosh', 
   'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 
   'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 
   'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 
   'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 
   'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 
   'tan', 'tanh', 'trunc']

it seems __doc__ is useful to provide some documentation in, say, functions

>>> math.__doc__
'This module is always available.  It provides access to the\nmathematical
 functions defined by the C standard.'

In addition to functions, documentation can also be provided in modules. So, if you have a file named like this:

"""This is the module docstring."""

def sayHello():
    """This is the function docstring."""
    return 'Hello World'

You can access its docstrings like this:

>>> import helloWorld
>>> helloWorld.__doc__
'This is the module docstring.'
>>> helloWorld.sayHello.__doc__
'This is the function docstring.'
  • For any user defined type, its attributes, its class's attributes, and recursively the attributes of its class's base classes can be retrieved using dir()
>>> class MyClassObject(object):
...     pass
>>> dir(MyClassObject)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Any data type can be simply converted to string using a built-in function called str. This function is called by default when a data type is passed to print

>>> str(123)    # "123"