Unit 4 PWP
Unit 4 PWP
Introduction:
Data type conversion functions converts the Py-type data into another form of data. It is
a conversion technique. Implicit type translation and explicit type converter are
Python's two basic categories of type conversion procedures.
Python has type conversion routines that allow for the direct translation of one data
type to another. This is very helpful for competitive programming as well as routine
programming
Eg1:
b=25
a=float(b)
print(a)#output will be 25.0
c=str(b)
print(c)#output will be 25
d=bin(b)
print(d)#output will be 0b11001
o=oct(b)
print(o)#output will be 0o31
Eg2:
x = 'javaTpoint'
y = tuple(x)
print ("tuple: ",end="")
print (y)
y = set(x)
print ("set: ",end="")
print (y)
y = list(x)
print ("list: ",end="")
print (y)
Output:
tuple: ('j,' 'a', 'v,' 'a', 'T,' 'p,' 'o', 'i', 'n,' 't')
set: {'n,' 'o', 'p,' 'j,' 'v,' 'T,' 'i', 'a', 't'}
list: ['j,' 'a', 'v,' 'a', 'T,' 'p,' 'o', 'i', 'n,' 't']
2.Map(): The map() function executes a specified function for each item in
an iterable. The item is sent to the function as a parameter.
Syntax
map(function, iterables)
Eg:
def add(n):
return n+n
num=(1,2,3,4)
result=map(add,num)
print(list(result))
3. filter()
The filter() method filters the given sequence with the help of a function that tests each element in
the sequence to be true or not.
Eg 1:
ages=[5,12,20,18,24]
def my(x):
if x>18:
return True
else:
return False
adults=filter(my,ages)
print(list(adults))
4. reduce():
The reduce(fun,seq) function is used to apply a particular function passed in its argument to all of
the list elements mentioned in the sequence passed.
Eg1:
from functool import reduce
numb=[1,2,3,4]
r=reduce(lambda x,y: x*y, numb)
print(r)