Very useful Built-in functions of Python - Part 1

Very useful Built-in functions of Python - Part 1

Hi all, Previously we discussed python tricks which can be very useful in daily work or coding competition. Today We are going to discuss built-in functions in Python which can be very useful.

all(iterable) and any(iterable)

all and any functions expect iterable as input. all returns True if all elements of the iterable are true or if the iterable is empty. any returns True if any element of the iterable is true. If the iterable is empty, return False.

>>> test_all = []
#This will return True as we are passing empty list
>>> all(test_all)
True
>>> test_all = [True, False]
#This will return False as all elements in list are not True
>>> all(test_all)
False
#This will return True as one element in list is True
>>> any(test_all)
True

callable(object)

It expects an object as an argument and returns True is passed object is callable. If this returns True, it is still possible that a call fails, but if it is False, calling the object will never succeed. Note this function was first removed in Python 3.0 and then brought back in Python 3.2

>>> var_a = "this is variable so it is not callable"
>>> callable(var_a)
False
>>>
>>> test_lambda = lambda x: x+5
#callable on lambda function will return
>>> callable(a)
True
>>> 
>>>  callable(print)
True
>>> class Test_class:
...     pass
#classes are callable
>>>  callable(Test_class)
True

dir(object)

dir() is a powerful inbuilt function in Python3, which returns list of the attributes and methods of any object (functions, modules, string, list, dict, classes). The dir() is usually used for debugging purposes in simple day to day programs, and even in large projects taken up by a team of developers. The capability of dir() to list out all the attributes of the parameter passed, is really useful when handling a lot of classes and functions, separately.

The dir() function can also list out all the available attributes for a module/list/dictionary. So, it also gives us information on the operations we can perform with the available list or module, which can be very useful when having little to no information about the module. It also helps to know new modules faster.

# Without arguments, return the list of names in the current local scope
# We have not imported anything or declared any function or varibale
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__',
 '__name__', '__package__', '__spec__']

# lets import math and create variable
>>> import math
>>> test_str = "Hello world!"
# returns module named added to local scope
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__',
 '__package__', '__spec__', 'math', 'test_str']

# dir on test_str will return a list of attributes and methods 
# we can call on any string as test_str is string
>>> dir(test_str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', 
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
 '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__',
 '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
 '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',
 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 
'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower',
 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex',
 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 
'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Thanks for reading this. I will introduce more helpful built-in functions in the next part. Share with your code mate if you find this helpful.

Happy Coding!!!