In this article i will be talking about all the builtin functions in python with an example for each. The following is the list of functions that we will be dealing with.
- bin ()
The abs() function will return an absolute value of a number. In other words a positive value will be returned for the negative value passed into this function. This will work for integer, float and also complex number. However for a complex number its magnitude is returned.
This output of this code snippet is,
1 1.0 1.0
This method will return “True” if all the elements in a list or iterable is true. This will also return true if the iterable is empty. You can use this method to check if a list is empty.
This method is the exact opposite of the all() method. This method will return “True” if any of the element in an iterable is true. In case of an empty iterable it returns “False”.
ascii() returns a string containing a printable representation of an object and escape the non-ASCII characters in the string using \x, \u or \U escapes.
This method will convert the integer to its binary representation prefixed with a “0b”. The result is a string.
Takes a value and return either True or False i.e, a boolean value.
Python breakpoint() function calls
sys.breakpointhook() function. By default, sys.breakpointhook() calls
pdb.set_trace() function. So at the very least, using breakpoint() provide convinience in using a debugger because we don’t have to explicitly import
Let’s look at a simple example of breakpoint() function usage. We have a python script
If we execute this after printing y the code will enter into pdb() which is python debugger module.
Hi > /Users/example.py(6)<module>() -> print(z) (Pdb) x 10 (Pdb) y 'Hi' (Pdb) z 'Hello' (Pdb) c Hello Process finished with exit code 0
we can check the values in x,y,z there. After that i am pressing “c” which means continue which continues with the next statement.
The chr() method takes an integer value and gives the corresponding character. This is opposite to the ord() method.
Complex method converts integers and strings to a complex number.
- In case of integer pass the real and imaginary number as arguments. like complex(2,1)
- In case of string takes single argument (No whitespaces) line complex(‘1+2j’)
The divmod(a, b) function takes two integers ‘a’ and ‘b’ and returns their quotient and remainder as a tuple. The first value of the tuple is the quotient whereas the second value is the remainder.