Python built in functions

Hacking Truth
0

 

Python built in functions

 

 

Filter() 

 

The filter function is used to filter elements from an iterable according to a condition. It accepts two parameters: a function and an iterable. The function must return a boolean value, which will be used to filter the iterable. For example, you can use the following code to filter all even numbers from a list.

 

                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat filter.py
#!/usr/bin/python3

numbers = [1,2,3,4,5,6,7,8,9]

even_numbers = list(filter(lambda x:x % 2 ==0, numbers))
print(even_numbers)
odd_numbers = list(filter(lambda x:x % 2 == 1, numbers))
print(odd_numbers)
                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ python3 filter.py
[2, 4, 6, 8]
[1, 3, 5, 7, 9]
                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ 




Reduce()

 

The retuce() function applies a function to all elements of an iterable and returns a single value. Because it is a component of the functools module, it must be imported before use. For example, you can use the following code to find the product of all elements in a list.

 

                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat reduce.py     
#!/usr/bin/python3

from functools import reduce

numbers = [1,2,3,4,5]
product = reduce(lambda x,y: x*y, numbers)
print(product)
                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ python3 reduce.py            
120
                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ 

 

 

isinstance()


The isinstance function is used to check if an object is an instance of a certain class or a subclass. It takes two argument: an object and a class. For example, you can use the following code to check if a variable is a list.

 

                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat isinstance.py 
#!/usr/bin/python3

x = [1,2,3]

result = isinstance(x, list)

print(result)
                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ sudo python3 isinstance.py                                                                          
True
                                                                                                                                                                       
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat isinstance.py
#!/usr/bin/python3

x = (1,2,3)

result = isinstance(x, list)

print(result)
                                                                                                                                                                        
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ sudo python3 isinstance.py
False
                                                                                                                                                                        
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ 



 

zip()

 

The zip function is used to combine two or more list into a single list of tuples. For example you can use the following two lists.

 

 

                                                                                                                                                                        
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat zip.py
#!/usr/bin/python3

list1 = [1,2,3,4,5]

list2 = [6,7,8,9]

print(list(zip(list1, list2)))
                                                                                                                                                                        
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ python3 zip.py
[(1, 6), (2, 7), (3, 8), (4, 9)]
                                                                                                                                                                        
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ 




map()

 

The map() function is used to apply a function to all elements of an iterable. For example, you can use the following code to square all elements of a list.

 

numbers = [1, 2, 3]

 

 

numbers = [1, 2, 3, 4]

 

 

 

Disclaimer

All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.
 
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !