Built-in Function in Python

abs() Function:

abs() function in python return the absolute value of a number.

Syntax:
abs(value)

Example:

a=50
b=90
c=a-b
print("The subtraction is:",abs(c))

Output:

The subtraction is: 40

On the above example the value of variable a is 50 and the value of variable b is 90, after subtract the result will be came -40 (50-90 = -40). But here we used abs () function, which return it into positive value. So the result is only 40.


all () function

The all () function in python accept the iterated objects such as (list, tuple,dictionary etc.). 

The all () function return true when all the iterable objects are true,
The all () function return false when all the iterable objects are false.
The all() function return false when any one iterable is false but rest of iterable are true.
The all() function return false when any one iterable is true but rest of iterable are false.
The all () function return true when the iterable object is empty.

Syntax:
all (iterable) 

Example:

list1=[1,2,3,4,5]#All iterable are true
for i in list1:
    print(i)
print("List1 is:",all(list1))

list2=[0,1,2,3,4]#one iterable false
for x in list2:
    print(x)
print("List2 is:",all(list2))

tuple1=(False,0)#All iterable are false
for a in tuple1:
    print(a)
print("Tuple1 is:",all(tuple1))

tuple2=()#iterable is empty
for b in tuple2:
    print(b)
print("Tuple2 is:",all(tuple2))

Output:

1
2
3
4
5
List1 is: True
0
1
2
3
4
List2 is: False
False
0
Tuple1 is: False
Tuple2 is: True


bin() function:

The bin () function used to return binary equivalent string of a specified integer.  The result always start with prefix 0b to represent it as binary string.

Syntax:
bin(number)

Example:

n=int(input("Enter no"))
a=bin(n)
print("The binary is:",a)

Output:

Enter no5
The binary is: 0b101


bool () function:

The bool () function used to convert a value to boolean (i.e. true and false) using the standard truth testing procedure.

Syntax:
bool(number)

Example:

test = []
print(test,'is',bool(test))

test = [0]
print(test,'is',bool(test))

test = 0.0
print(test,'is',bool(test))

test = None
print(test,'is',bool(test))

test = True
print(test,'is',bool(test))

test = 'Easy string'
print(test,'is',bool(test))

Output:

[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True


callable () function:

The callable () function used to call something. It return true if the object passed appears to be callable otherwise it return false.

Syntax:
callable(objects)

Example:


class call1:

    def _call_(self):

        print("Hello")

print(callable(call1))


Output:

True
>>> 



compile () function:

The compile () function used to compile the source code . It takes the source code as input and return it as a code objects which is executed by exec () function. It is used when you want to convert the source code which is in string form or in AST object into code object.

Syntax:
compile(source code)

Example:

c='list=[1,2,3,4]\nfor i in list:\tprint(i)'
code=compile(c,'comp.py','exec')
exec(code)

Output:

1
2
3
4
>>>


exec () function:

The exec() function used to execute dynamic created code, which is either a string or  a code object.

Syntax:
exec(object)

Example:


p='a = 5\nb=10\nprint("Sum =", a+b)'
exec(p)



Output:


Sum = 15
>>> 


sum () function:

The sum () function used to get sum of the numbers of the iterable, i.e list, tuple etc.

Syntax:
sum(iterable)

Example:

s=sum([1,2,3,4,5,6,7,8,9,10])
print(s)

Output:

55
>>>

Comments

Popular Posts