Python Built-in Functions ( PART -1 )

Hi, guys welcome back once again with some interesting and exciting code with Python Built in Functions.I assume you people have sound knowledge of the Beginner’s section of python, so with this understanding, the Intermediate level will be pretty much easy.In the coming sections, we will be learning more about the Python Built in Functions, So let pick them one by one Python Built in Functions and cover them all in may be about 5-6 articles ( divided into parts ).

Python Built in Functions

1. abs(X)

This function returns the absolute value of a number, the number can be a simple number, long integer, floating point number or even the complex number.

Lets look at the following Examples:

>>> a=complex(10,10)
>>> a
>>> abs(a)
Here we have a complex number and it returns us the magnitude of the number.
Few more Examples:
>>> a=10.323456789
>>> abs(a)
>>> a = -5
>>> abs(a)

2. all() and any()

I hope you all must be having basic understanding of Logical AND and OR operation, in this context too any() and all() operations can be thought as Logical OR and AND respectively.

any() will return True when atleast one of the elements is True.

all() will return True only when all the elements are True.

+———————————————————— +---------+---------+
| | any | all |
+———————————————————— -+---------+---------+
| All Truthy values | True | True |
+——————————————————— — +---------+---------+
| All Falsy values | False | False |
+——————————————————— — +---------+---------+
| One Truthy value (all others are Falsy) | True | False |
+——————————————————— — +---------+---------+
| One Falsy value (all others are Truthy) | True | False |
+——————————————————— —-+---------+---------+
| Empty Iterable | False | True |
+——————————————————— —-+---------+---------+

Python provides the flexibility that if the number is non zero then it is considered as True and obviously if ‘0’ then it is False.

Have a look at the Example :

>>> testList = [0,1,2,3,4,5]
>>> print all([m for m in testList])
>>> print "Now delete the number 0 from the list"
Now delete the number 0 from the list
>>> del testList[0]
>>> testList
[1, 2, 3, 4, 5]
>>> print all([m for m in testList])
>>> testList.append(0)
>>> testList
[1, 2, 3, 4, 5, 0]
>>> print "Now we have added again O and the run any function"
Now we have added again O and the run any function
>>> print any([m for m in testList])

3. bin(X)

This function coverts Integer number to a binary string. The result is a valid Python Expression.

Here is the Example :

>>> a=6
>>> c=bin(a)
>>> c
>>> type(c)
<type 'str'>

4. bool(X)

It returns a Boolean value, i.e. either TRUE or FALSE. If X is false or omitted, this returns FALSE; otherwise it returns TRUE.

Example :

>>> a=0
>>> bool(a)
>>> bool()
>>> a=99
>>> bool(a)
>>> st = ''
>>> bool(st)
>>> st = ''
>>> bool(st)

5. chr(i)

Returns a String of one character whose ASCII code is the integer. This function is reverse of ord(C) ( which will be dealt later ).


>>> chr(90)

6. dict()

This function creates a new Dictionary. There are number of ways of creating Dictionary in python but here we can do that using dict() function.

So if we go with raw definition it says “Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.

Here is a Example :

>>> newDictionary = dict(SiteName = "", Content = "Python Related")
>>> newDictionary {'Content': 'Python Related', 'SiteName': ‘’}

7. zip([iterable, …])

In genearl, this function returns a List of Tuples. This function takes two equal-length collections, and merge them together in pairs.

Note : With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

Have a look at the Example :

>>> m = ['Narendra','Sachin','MS']
>>> m = ['Yuvraj','Narendra','MS']
>>> n = ['Singh','Modi','Dhoni']
>>> zip(m,n)
[('Yuvraj', 'Singh'), ('Narendra', 'Modi'), ('MS', 'Dhoni')]
>>> for i,j in zip(m,n):
... print i,j
Yuvraj Singh
Narendra Modi
MS Dhoni

8. type()

This function returns the datatype of any arbitrary object. This is useful for helper functions that can handle several types of data.

Examples :

>>> b = 15
>>> type(b)
<type 'int'>
>>> st = "Hello PythonLover's"
>>> type(st)
<type 'str'>
>>> import facebook
>>> type(facebook)
<type ‘module’>
>>> dictionaryTest = dict(one=1,two=2,three=3)
>>> type(dictionaryTest)
<type 'dict'>
>>> l = []
>>> type(l)
<type 'list'>
>>> s = ()
>>> type(s)
<type 'tuple'>

and so on…

9. float(X)

This function converts a String ( Ex. “100” ) or number to floating point number. Attempting to covert a non-numeric value to float throws a ValueError.

Have a look at the below Example to gain little more Understanding on it :

>>> a=100
>>> type(a)
<type 'int'>
>>> b=float(a)
>>> b
>>> type(b)
<type 'float'>
>>> c="StringEvaluation"
>>> float(c)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: StringEvaluation

I hope you guys had amazing time learning the PART-1 of Python Built-in Function. PART-2 of the same will be published soon.

For more details on Python you can visit our Absolute Beginner Section OR

So in case of any queries or question do reach us, We will help you in best possible way to keep the things going in you favour, Thank you.