All builtin functions in python – Part 1

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.

  1. abs()
  2. all()
  3. any()
  4. ascii()
  5. bin ()
  6. bool()
  7. breakpoint()
  8. chr()
  9. complex()
  10. divmod()

abs()

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.

abs() in python

This output of this code snippet is,

1
1.0
1.0

all()

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.

all() method in python

any()

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”.

any() method in python

ascii()

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.

ascii() method in python

bin()

This method will convert the integer to its binary representation prefixed with a “0b”. The result is a string.

bin() method in python

bool()

Takes a value and return either True or False i.e, a boolean value.

bool() method in python

breakpoint()

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 pdb module.

Let’s look at a simple example of breakpoint() function usage. We have a python script

breakpoint()

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.

chr()

The chr() method takes an integer value and gives the corresponding character. This is opposite to the ord() method.

chr() in python

complex()

Complex method converts integers and strings to a complex number.

  1. In case of integer pass the real and imaginary number as arguments. like complex(2,1)
  2. In case of string takes single argument (No whitespaces) line complex(‘1+2j’)
complex() in python

divmod()

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.

divmod() in python

Leave a Reply

Your email address will not be published. Required fields are marked *