Python Built-in Functions

  1. - Returns the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.
    Example:

    print(abs(5))
    print(abs(-5.23))
    print(abs(-5+6j))

    Output:

    5
    5.23
    7.810249675906654

  2. - Return True if all elements of the iterable are true or if the iterable is empty. Otherwise, return False.
    Example:

    print(all([1,2,3]))
    print(all([1,2,0,3]))
    print(all([]))

    Output

    True
    False
    True

  3. - Return True if any element of the iterable is true. If the iterable is empty, return False.
    Example:

    print(any([1,2,3,0]))
    print(any([]))

    Output:

    True
    False

  4. - Returns a readable version of an object. Replaces none-ascii characters with escape character.
    Example:

    str1 = ascii("I'm Govardhan")
    str2 = ascii("I'm GovÄrdhan")
    print(str1)
    print(str2)

    Output:

    "I'm Govardhan"
    "I'm Gov\xe5rdhan"

  5. - Returns a binary version of a given number.
    Example:

    binary = bin(9)
    print(binary)

    Output:

    0b1001

  6. - Returns the boolean value of given object.
    Example:

    print(bool(1))
    print(bool(0))
    print(bool(-1))
    print(bool(''))
    print(bool('A'))
    print(bool(None))

    Output:

    True
    False
    True
    False
    True
    False

  7. - Returns True if specified object is callabel, otherwise it returns False.
    Example:

    def fun():
        n = 9

    print(callabel(fun))

    Output:

    True

  8. - Returns the character that represents the specified Unicode.
    Example:

    print(chr(71))

    Output:

    G

  9. - Returns a complex number by specifying a real number and an imaginary number.
    Example:

    print(complex(9, 7))

    Output:

    (9+7j)

  10. - Used to create a dictionary.
    Example:

    details = dict(name = "Govardhan", age = 33, country = "India")
    print(details)

    Output:

    {'name': 'Govardhan', 'age': 33, 'country': 'India'}

  11. - returns a tuple containing the quotient and the remainder when argument1 (divident) is divided by argument2 (divisor).
    Example:

    qr = divmod(5, 3)
    print(qr)

    Output:

    (1, 2)

  12. - Takes a collection and returns it as an enumerate object. Default start value is 0(zero).
    Example:

    x = ['C', 'Java', 'Python']
    y = enumerate(x,11)
    print(list(y))

    Output:

    [(11, 'C'), (12, 'Java'), (13, 'Python')]

  13. - Which is to evaluates the specified expression, if the expression is legal python statement.
    Note: expression must be in string format.
    Example:

    print(eval('10+5'))

    Output:

    15

  14. - Converts a specified value into a floating point number.
    Example:

    f1 = float(3)
    f2 = float('3.14')
    print(f1)
    print(f2)

    Output:

    3.0
    3.14

  15. - Converts the specified number into a hexadecimal value.
    Example:

    hexanumber = hex(255)
    print(hexanumber)

    Output:

    0xff

  16. - Returns a unique id for the specified object, which is nothing but object's memory address and will be different for each time you run the program.
    Example:

    x = 5
    y = 'Hello'
    print(id(x))
    print(id(y))

    Output:

    9079136
    47472264951600

  17. - Converts the specified value into an integer number. Example:

    n1 = int(3.5)
    n2 = int('12')
    print(n1)
    print(n2)

    Output:

    3
    12

  18. - Returns True if the specified object is of the specified type, otherwise False.
    Example:

    b1 = isinstance("Hello", (str, int, float))
    b2 = isinstance("Hello", (int,float))
    print(b1)
    print(b2)

    Output:

    True
    False

  19. - Returns number of items in an object.
    Example:

    lang = "python"
    print(len(lang))

    Output:

    6

  20. - Returns a symbol table which contains necessary information about current program as a dictionary.
    Example:

    print(locals())

    Output:

    {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': }

  21. - executes a specified function for each item in a iterable.
    Example:

    def languages(lang):
        return len(lang)

    lang = map(languages, ('C', 'Java', 'Python'))
    print(lang)
    print(list(lang))

    Output:

    <map object at 0x2b98d1c74eb8>
    [1, 4, 6]

  22. - Returns item with the highest value.
    Example:

    lang = max('C','Java','Python')
    print(lang)

    Output:

    Python

  23. - Returns item with the highest value.
    Example:

    lang = max('C','Java','Python')
    print(lang)

    Output:

    C

  24. - Converts an integer into octal string.
    Example:

    octStr = oct(19)
    print(octStr)

    Output:

    0o23

  25. - Returns the unicode code of a specified character.
    Example:

    unicode = ord("A")
    print(unicode)

    Output:

    65

  26. - Returns a reversed iterator object.
    Example:

    lang = "Python"
    rev = reversed(lang)
    for ch in rev:
        print(ch,end="")

    Output:

    nohtyP