Python – Built-in Functions
Functions literally give the functionality to a programming language and python gives you many built-in functions.
A function in python is a block of code which only runs when it’s called, it is organized reusable and performs a single related action.
Functions are all about making it easier for the user with increasing modularity and improving code reusability.
Built-in Functions are the functions which are built into (already available) Python and can be accessed by end-users.
Built-in functions do not need to be imported. It can be accessed instantly in your script as they are installed into python by default.
Also you can import extra functionality through online packages or through built-in modules. Python truly becomes a powerhouse programming language when these external packages are used.
Let’s look at some built-in functions:
|sorted()||Returns a new sorted list from the items in iterable.|
|all()||The all() function returns True if all elements of the supplied iterable are true.|
|any()||The any() function is the converse of the all() function.|
|bool()||The bool function converts the value to a Boolean, using the standard Python truth testing procedure.|
|chr()||Returns the string representing a character whose Unicode code point is the integer.|
|open()||Opens file and return a corresponding file object.|
|abs()||The abs() function returns the absolute value of an integer, floating point, or complex number.|
|enumerate()||Returns an enumerate object with items and their index values.|
|int()||Returns an integer object constructed from a number or string x.|
|len()||len(s) returns the length of an object.|
|globals()||Returns a dictionary representing the current global symbol table.|
|bin()||Convert an integer number to a binary string prefixed with “0b”.|
|eval()||The arguments are a string and optional globals and locals If provided, globals must be a dictionary. If provided, locals can be any mapping objects.|
|sum()||sum(iterables) sums the numeric values in an iterable such as a list, tuple, or set.|
|reversed()||reversed() is a reverse iterator or an object of the type that you can loop through and process. The list and tuples type are supported with this function.|
Returns an enumerate object. Iterable must be a sequence, an iterator, or some other object which supports iteration.
Enumerate returns the list of tuples. Tuples consist index values and items from list.
grocery = ['bread', 'milk', 'butter'] enumerateGrocery = enumerate(grocery) print(type(enumerateGrocery)) #converting to list... print(list(enumerateGrocery)) #changing the default counter... enumerateGrocery = enumerate(grocery, 10) print(list(enumerateGrocery)) #OUTPUT <class 'enumerate'> [(0, 'bread'), (1, 'milk'), (2, 'butter')] [(10, 'bread'), (11, 'milk'), (12, 'butter')]