×

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}) 

Related Topics

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Python Operator Precedence

Before knowing about the operator precedence in Python, we have to know about the operators in Python. So let's have a look at it. According to one definition, the operator is...

3 minutes read.

Isreal() Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

Idle python download for Windows

Here, we will be discussing how to get the answer to all questions related to installing Python on Windows. What is IDLE? Integrated Development and Learning Environment, or IDLE, is a shorthand....

3 minutes read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Make Notepad using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

5 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

Periodogram in Python

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

How To Compare Two Strings In Python

In this article, we will discuss how to compare two strings in Python. So, before that, let’s have a quick revision on “What are strings?” Strings are a sequence of characters that...

5 minutes read.

os.rename() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Implementing geometric shapes into the game in python

Geometric Drawings We are trying to draw various shapes of geometry by using pygame module to implement Geometric Drawings in game. Let us revise few syntax of basic shapes, to get started...

7 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

Python hex() function

Python hex() function The hex() function in Python  converts the specified number into a hexadecimal value. Syntax hex(x) Parameter x: The parameter represents an Integer value. Return This function Returns hexadecimal string. Example 1 # Python Program explaining # the hex() function print("The...

1 minute read.