Python Built-in Functions
Python Built-in Functions
The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow:
Methods | Explaining |
abs() | The abs() function returns the absolute value of a number |
all() | The all() function returns True if all items in an iterable object are true. |
any() | The any() function returns True if any item in an iterable object is true |
ascii() | This function returns a readable version of an object. |
bin() | The bin() function convert an integer number to a binary string prefixed with “0b”. |
bool() | The bool()class returns the boolean value for the given object |
bytearray() | The bytearray()class returns an array of bytes |
bytes() | The byte()class returns a bytes object. |
chr() | The chr() function returns a character from the specified Unicode code. |
compile() | This function returns the specified source as an object, ready to be executed |
complex() | This function returns a complex number |
delattr() | The delattr() function deletes the specified attribute (property or method) from the specified object |
dict() | The dict() function returns a dictionary (Array) |
dir() | This function returns the list of names in the current local scope |
divmod() | The divmod() function returns the quotient and the remainder when argument1 is divided by argument2. |
enumerate() | The enumerate() function returns an enumerate object. |
eval() | The eval() function evaluates and executes an expression. |
filter() | The filter() function constructs an iterator from those elements of iterable for which function returns true. |
float() | This class returns a floating point number constructed from a number or string x. |
format() | This function Formats the specified value. |
frozenset() | The frozenset() function returns a frozenset object |
getattr() | This function returns the value of the named attribute of object. |
globals() | The globals() function returns a dictionary representing the current global symbol table. |
hasattr() | The hasattr() function returns True if the specified object has the specified attribute (property/method) |
hash() | The hash() function returns the hash value of the object. |
hex() | This function converts an integer number to a lowercase hexadecimal string prefixed with “0x”. |
id() | The id() function returns the “identity” of an object. |
input() | The input() function reads a line from input, converts it to a string and returns that string. |
int() | The int() function returns an integer object constructed from a number or string x, or return 0 if no arguments are given. |
isinstance() | This function returns a boolean value true if the object argument is an instance of the classinfo argument, or of a subclass thereof. |
issubclass() | The issubclass() returns a boolean value true if class is a subclass (direct, indirect or virtual) of classinfo. |
iter() | This function returns an iterator object. |
len() | The len() function returns the length (the number of items) of an object. |
list() | This list() function returns a list. |
locals() | The locals() updates and returns a dictionary representing the current local symbol table. |
map() | This function returns the given iterator with the specified function applied to each item. |
max() | The max()returns the largest item in an iterable or the largest of two or more arguments. |
memoryview() | This function returns a “memory view” object created from the given argument. |
min() | The min() function returns the smallest item in an iterable or the smallest of two or more arguments. |
next() | The next()function returns the next item in an iterable |
object() | The object()function returns a new object |
oct() | The oct() function converts a number into an octal |
open() | This function opens a file and returns a file object |
ord() | This function converts an integer representing the Unicode of the specified character |
pow() | The pow()function returns x to the power y; if z is present, return x to the power y, modulo z |
print() | This function prints objects to the text stream file. |
property() | The property() function returns a property attribute. |
range() | The range()function returns a sequence of numbers, starting from 0 and increments by 1 (by default) |
repr() | This function returns a string containing a printable representation of an object. |
reversed() | The reversed() function returns a reverse iterator. |
round() | The round() function returns the number by rounding to ndigits precision after the decimal point. |
set() | The set() function returns a new set object, optionally with elements taken from iterable. |
setattr() | This function sets an attribute (property/method) of an object. |
slice() | The slice() function returns a slice object. |
sorted() | This function returns a new sorted list from the items in iterable. |
str() | The str() function returns a str version of object. |
sum() | This function sums the items of the specified iterator |
tuple() | The tuple() function returns a tuple |
type() | The type() function returns the type of an object. |
vars() | This function returns the __dict__ property of an object. |
zip() | This function returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. |
|
|
Example 1
# Python Program explaining # built-in function # abs() built-in function # passing floating point number float_val = -54.26 print('Absolute value for integer number:', abs(float_val)) # passing An integer number int_val = -94 print('Absolute value for float number:', abs(int_val)) # passing a complex number complex_val = (3 - 4j) print('Absolute value for complex number:', abs(complex_val)) #all() function # will return True and the same will be printed print ('The all() function will return: ',all([True, True, True])) # any() function # item (True) and will return True. print ('The any() function will return: ',any([False, True, False, False, False, True])) # ascii() function inp_str = "H ë ê L O ? W O R L D" print ("The ascii() function:",ascii(inp_str)) # bin() built-in function x = bin(136) print('The bin() function returns: ',x)
Output
Absolute value for integer number: 54.26 Absolute value for float number: 94 Absolute value for complex number: 5.0 The all() function will return: True The any() function will return: True The ascii() function: 'H \xeb \xea L O ? W O R L D' The bin() function returns: 0b10001000
Example 2
# Python Program explaining # built-in function # bool class # Returns False as val1 is not equal to val2 val1 = 5 val2 = 10 print("Both the values are equal:",bool(val1==val2)) # bytearray() class inp_str = "HelloWorld" # encoding the string with unicode 8 and 16 inp_array1 = bytearray(inp_str, 'utf-8') print("The bytearray() function returns:",inp_array1) # chr() function # returning the char value print(chr(72), chr(101), chr(101), chr(108), chr(111), chr(32), chr(87), chr(111), chr(114),chr(108), chr(100)) # complex() class val1 = complex(11) # Passing single parameter # passing the output print(“The complex value:”,val1)
Output
Both the values are equal: False The bytearray() function returns: bytearray(b'HelloWorld') H e e l o W o r l d The complex value: (11+0j)
Example 3
# Python Program explaining # built-in function # the filter() function # a list contains both even and odd numbers. values = [10, 11, 12, 13, 15, 18, 103] print("List: ",values) # filter() returning odd numbers of the list even_val = filter(lambda x: x % 2!=0, values) print("Even Values: ",list(even_val)) #Applying float() funcion x = float(6) print("After float() function: ",x) # the format() function val= 0.09 print("Number: ",val) #formating the specified value x = format(val, '%') print("After format() function: ",x) #frozenset() function # tuple of numbers val = (11, 12, 13, 14, 15, 16, 17, 18, 19) # converting tuple to frozenset f_num = frozenset(val) # printing details print("frozenset returns: ", f_num)
Output
List: [10, 11, 12, 13, 15, 18, 103] Even Values: [11, 13, 15, 103] After float() function: 6.0 Number: 0.09 After format() function: 9.000000% frozenset returns: frozenset({11, 12, 13, 14, 15, 16, 17, 18, 19})