Hello everyone! Welcome to our Blog HIGH TECHDROID and this is Lavanya. Today we are going to see about built-in functions.
Introduction:
First, we'll have a short demo on what are built-in functions. Let me assign a=’good’. If I want to print it, I give as print(a). Thus, I need a function called print() to print the value of a. This is called as built-in functions. Like this, there are many built-in functions supported in Python like input(), min(), max(), len(), replace() and so on. Let’s have a look on it now.
Here, I have described few built-in functions along with their functionalities. First is import, that is, __import__. In C, we use header files like #include<stdio.h> whereas we use this import to import modules in Python. abs() is to find the absolute value of a variable or a number. Next is, print() to print the value of a variable or display the output. Input() is to get input from user. When a set of attributes or parameters are given, max()/min() can be used to ind the maximum and minimum of all respectively. Next, int(), float() and str() are used for typecasting. Converting from one datatype to another is called as typecasting and above are used for it. Next is id(). Every object has an identity in its lifetime. If u want to know the identity of an object, it can be found by giving the object as the parameter to id(). In C, we use strlen(). Here, we use len() to find the length of the variable. In Python, we don’t declare the variable with its datatype instead we declare it with its value. Hence, you can know the datatype of any variable by giving it as parameter. Next is pow(a,b). If it is 2^3 or 5^4 it can be computed easily whereas it is 50^100, it is tedious. Hence, we use this function. If it has 3 parameters, then a^b is computed first followed by computation of modulo c. Next is sum() which is usually used in excel sheets for computation for calculating the sum of many variables. Round()… the value of pi(22/7) goes on like 3.142…which can be rounded off to certain digits using round(). Next is eval() which evaluates large arithmetic expression using BODMAS without getting confused.
Not only these functions, also there are many built-in functions present which helps to denote the errors that occur. First is ImportError which is displayed when the required modules are not imported. SyntaxError occurs when there is an error in syntax of code. We know that 5/0 is equal to infinity which cannot be determined. In such cases ZeroDivisionError is displayed to denote it. TypeError occurs in case of mistakes made regarding datatype. When you request for read/write command to a file and when the corresponding file is not found, there raises the FileNotFoundError. If you tend to make any mistakes in input or output, that is, giving inappropriate input, then IOError is displayed. RunTimeError occurs to denote the errors that occur in run-time of the program.
Available builtin functions in python:
Let’s take a look at few examples now. Before that, to know all the built-in functions available in Python, there is a command to help us. That is , dir(__builtins__). On pressing Enter after this command, all the available built-in functions regarding Error, Integer and String are displayed. Okay, there is a command to find all those available built-in functions but to know each of their functionalities, there is a function called help(). For example, help(max) where max is a built-in unction lets you know that max() collects a list of arguments and returns the biggest of them.
Examples:
For example, I give few parameters as max(3754,83,5423,58785) out of which the highest one ie., 58785 is returned. Not only this, typecasting can be done as, I assign a=5.325 and on giving int(a), the integer part ie.,5 alone is returned. Many manipulations can also be done in string using the built-in functions. I assign a=’good’. If I want to replace the letter ‘g’ with ‘f’ and convert it to ‘food’, it can be done as a.replace(‘g’,’f’). Thus, replace() can be used to replace a letter or sub-string in a word. If I give len(‘food’), it returns 4 which is the length of the string. Also, there is a function called swapcase() which helps to return the string in opposite case. ie., upper to lower or lower to upper. I assign a=’good’ and on giving a.swapcase(), I’ll get the output as ‘GOOD’ with lowercase swapped to uppercase.
The overall Builtin's of python is given below:
Python abs()
|
returns absolute value of a number
|
Python all()
|
returns true when all elements in iterable is true
|
Python any()
|
Checks if any Element of an Iterable is True
|
Python ascii()
|
Returns String Containing Printable Representation
|
Python bin()
|
converts integer to binary string
|
Python bool()
|
Converts a Value to Boolean
|
Python bytearray()
|
returns array of given byte size
|
Python bytes()
|
returns immutable bytes object
|
Python callable()
|
Checks if the Object is Callable
|
Python chr()
|
Returns a Character (a string) from an Integer
|
Python classmethod()
|
returns class method for given function
|
Python compile()
|
Returns a Python code object
|
Python complex()
|
Creates a Complex Number
|
Python delattr()
|
Deletes Attribute From the Object
|
Python dict()
|
Creates a Dictionary
|
Python dir()
|
Tries to Return Attributes of Object
|
Python divmod()
|
Returns a Tuple of Quotient and Remainder
|
Python enumerate()
|
Returns an Enumerate Object
|
Python eval()
|
Runs Python Code Within Program
|
Python exec()
|
Executes Dynamically Created Program
|
Python filter()
|
constructs iterator from elements which are true
|
Python float()
|
returns floating point number from number, string
|
Python format()
|
returns formatted representation of a value
|
Python frozenset()
|
returns immutable frozenset object
|
Python getattr()
|
returns value of named attribute of an object
|
Python globals()
|
returns dictionary of current global symbol table
|
Python hasattr()
|
returns whether object has named attribute
|
Python hash()
|
returns hash value of an object
|
Python help()
|
Invokes the built-in Help System
|
Python hex()
|
Converts to Integer to Hexadecimal
|
Python id()
|
Returns Identify of an Object
|
Python input()
|
reads and returns a line of string
|
Python int()
|
returns integer from a number or string
|
Python isinstance()
|
Checks if a Object is an Instance of Class
|
Python issubclass()
|
Checks if a Object is Subclass of a Class
|
Python iter()
|
returns iterator for an object
|
Python len()
|
Returns Length of an Object
|
Python list()
|
creates list in Python
|
Python locals()
|
Returns dictionary of a current local symbol table
|
Python map()
|
Applies Function and Returns a List
|
Python max()
|
returns largest element
|
Python memoryview()
|
returns memory view of an argument
|
Python min()
|
returns smallest element
|
Python next()
|
Retrieves Next Element from Iterator
|
Python object()
|
Creates a Featureless Object
|
Python oct()
|
converts integer to octal
|
Python open()
|
Returns a File object
|
Python ord()
|
returns Unicode code point for Unicode character
|
Python pow()
|
returns x to the power of y
|
Python print()
|
Prints the Given Object
|
Python property()
|
returns a property attribute
|
Python range()
|
return sequence of integers between start and stop
|
Python repr()
|
returns printable representation of an object
|
Python reversed()
|
returns reversed iterator of a sequence
|
Python round()
|
rounds a floating point number to ndigits places.
|
Python set()
|
returns a Python set
|
Python setattr()
|
sets value of an attribute of object
|
Python slice()
|
creates a slice object specified by range()
|
Python sorted()
|
returns sorted list from a given iterable
|
Python staticmethod()
|
creates static method from a function
|
Python str()
|
returns informal representation of an object
|
Python sum()
|
Add items of an Iterable
|
Python super()
|
Allow you to Refer Parent Class by super
|
Python tuple()
|
Creates a Tuple
|
Python type()
|
Returns Type of an Object
|
Python vars()
|
Returns __dict__ attribute of a class
|
Python zip()
|
Returns an Iterator of Tuples
|
Python __import__()
|
Advanced Function Called by import
|