Built-in Functions in Python
Built in Function
Built in functions are the function(s) that are built into Python and can be accessed by a programmer. These are always available and for using them, we don’t have to import any module (file). Python has a small set of built-in functions as most of the functions have been partitioned to modules. This was done to keep core language precise.
Definition:
The Python interpreter has a number of functions that are always available for use. These functions are called built-in functions.
For example,
print() function prints the given object to the standard output device (screen) or to the text stream file.
In Python 3.6 (latest version), there are 68 built-in functions.
Built in Function
abs (x)
It returns distance between x and zero, where x is a numeric expression.
>>>abs(-45)
45
>>>abs(119L)
119
max( x, y, z, …. )
It returns the largest of its arguments: where x, y and z are numeric variable or expression.
>>>max(80, 100, 1000)
1000
>>>max(-80, -20, -10)
-10
min( x, y, z, …. )
It returns the largest of its arguments: where x, y and z are numeric variable or expression.
>>> min(80, 100, 1000)
80
>>> min(-80, -20, -10)
-80
cmp( x, y )
It returns the sign of the difference of two numbers: -1 if x < y, 0 if x == y, or 1 if x > y, where x and y are numeric variable/expression.
>>>cmp(80, 100)
-1
>>>cmp(180, 100)
1
divmod (x,y )
Returns both quotient and remainder by division through a tuple, when x is divided by y; where x & y are variable/expression.
>>> divmod (14,5)
(2,4)
>>> divmod (2.7, 1.5)
(1.0, 1.20000)
len (s)
Return the length
>>> a= [1,2,3]
>>>len (a)
3
>>> b= “Hello‟
>>> len (b)
5
range (start, stop[,step])
The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0.
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5)
[0, 5, 10, 15, 20, 25]
round( x [, n] )
It returns float x rounded to n digits from the decimal point, where x and n are numeric expressions.
If n is not provided then x is rounded to 0 decimal digits.
>>>round(80.23456, 2)
80.23
>>>round(-100.000056, 3)
-100.0
>>> round (80.23456)
80.0
Type Conversion Functions
int( ) – To convert the string into integer.
str( ) – To covert any value into string.
float( ) – To covert string into float.
>> print 10 + int(“20”)
>> str(10)
>>float(10)
Input Functions
This function is used to take input from user in the form of string.
Name =input(“Enter your name : “)
eval function:
This function is used to evaluate the value of the string
e.g. x=eval(“45+10“)
print(x)
# answer will be 55
Function | Description |
---|---|
abs() | Returns the absolute value of a number |
all() | Returns True if all items in an iterable object are true |
any() | Returns True if any item in an iterable object is true |
ascii() | Returns a readable version of an object. Replaces none-ascii characters with escape character |
bin() | Returns the binary version of a number |
bool() | Returns the boolean value of the specified object |
bytearray() | Returns an array of bytes |
bytes() | Returns a bytes object |
callable() | Returns True if the specified object is callable, otherwise False |
chr() | Returns a character from the specified Unicode code. |
classmethod() | Converts a method into a class method |
compile() | Returns the specified source as an object, ready to be executed |
complex() | Returns a complex number |
delattr() | Deletes the specified attribute (property or method) from the specified object |
dict() | Returns a dictionary (Array) |
dir() | Returns a list of the specified object’s properties and methods |
divmod() | Returns the quotient and the remainder when argument1 is divided by argument2 |
enumerate() | Takes a collection (e.g. a tuple) and returns it as an enumerate object |
eval() | Evaluates and executes an expression |
exec() | Executes the specified code (or object) |
filter() | Use a filter function to exclude items in an iterable object |
float() | Returns a floating point number |
format() | Formats a specified value |
frozenset() | Returns a frozenset object |
getattr() | Returns the value of the specified attribute (property or method) |
globals() | Returns the current global symbol table as a dictionary |
hasattr() | Returns True if the specified object has the specified attribute (property/method) |
hash() | Returns the hash value of a specified object |
help() | Executes the built-in help system |
hex() | Converts a number into a hexadecimal value |
id() | Returns the id of an object |
input() | Allowing user input |
int() | Returns an integer number |
isinstance() | Returns True if a specified object is an instance of a specified object |
issubclass() | Returns True if a specified class is a subclass of a specified object |
iter() | Returns an iterator object |
len() | Returns the length of an object |
list() | Returns a list |
locals() | Returns an updated dictionary of the current local symbol table |
map() | Returns the specified iterator with the specified function applied to each item |
max() | Returns the largest item in an iterable |
memoryview() | Returns a memory view object |
min() | Returns the smallest item in an iterable |
next() | Returns the next item in an iterable |
object() | Returns a new object |
oct() | Converts a number into an octal |
open() | Opens a file and returns a file object |
ord() | Convert an integer representing the Unicode of the specified character |
pow() | Returns the value of x to the power of y |
print() | Prints to the standard output device |
property() | Gets, sets, deletes a property |
range() | Returns a sequence of numbers, starting from 0 and increments by 1 (by default) |
repr() | Returns a readable version of an object |
reversed() | Returns a reversed iterator |
round() | Rounds a numbers |
set() | Returns a new set object |
setattr() | Sets an attribute (property/method) of an object |
slice() | Returns a slice object |
sorted() | Returns a sorted list |
staticmethod() | Converts a method into a static method |
str() | Returns a string object |
sum() | Sums the items of an iterator |
super() | Returns an object that represents the parent class |
tuple() | Returns a tuple |
type() | Returns the type of an object |
vars() | Returns the __dict__ property of an object |
zip() | Returns an iterator, from two or more iterators Thanks to W3school.com |