Advanced Built-In
During development, developers face situations of repetitive patterns of problems. One solution is they implement the functions to solve the issues themselves, but this would defeat the purpose of programming in a high-level programming language like Python.
Another issue is that the developers might have a bug in the implementation, ultimately leading to unstable software. Hence, Python developers have provided a set of functions to avoid the re-implementation of common functions.
Python's in-built abs() method should be used when you want the absolute value of an int type.
1234.5677
1234
1234
This in-built all() method in Python will return True when all the elements of an iterable type evaluate to True. Any non-zero, non-None value evaluates to True.
False
True
This in-built any() method in Python will return True even if one item in the iterable evaluates to True. Any non-zero, non-None value evaluates to True.
True
This in-built method ascii() in Python will return the ASCII printable version of the text. It will escape any non-ascii character.
'Hello, Juan Dani\xe9l'
This in-built method bin() in Python will return the binary representation of an integer.
0b1000
This in-built bool() method in Python will return the boolean representation of an object.
False
True
False
This in-built bytearray() method in Python will return an array of bytes.
bytearray(b'ABCD')
This in-built bytes() method in Python will return a bytes object.
b'ABCD'
This in-built callable() method in Python will return the boolean value of True if the specified object is callable; otherwise, it will return a False.
True
This in-built chr() method in Python will return a character from specified Unicode characters.
A
This in-built classmethod() method in Python will convert a function into a method that can access class properties and methods. It will become another class method.
28
This in-built compile() method in Python will return the code object.
8
You can read more about the compile method here.
This in-built complex() method in Python will return a complex number.
(1.23+0j)
Python's in-built delattr() method will delete the specified attribute, either a property or a method, from the object.
John
'Person' object has no attribute 'name'
This in-built dict() method in Python will return a dictionary.
{'name': 'John', 'age': 46}
This in-built dir() method in Python will return a list of specified object methods and properties.
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
This in-built divmod() method in Python will return the quotient and remainder when args1 is divided by args2.
(2, 1)
This in-built enumerate() in Python takes a collection and returns the same as an enumerate object.
(0, 'Toyota')
(1, 'Audi')
(2, 'Mercedes-Benz')
This in-built eval() method in Python evaluates and executes a valid Python code.
Hello, World!
This method is powerful and dangerous as it allows for arbitrary code execution.
This in-built exec() method in Python executes a specified code object.
8
This in-built filter() method in Python excludes items in an iterable.
2
4
6
8
10
This in-built float() method in Python will return a floating-point value of the numerical value passed.
10.0
This in-built format() method in Python will format a specified value.
Hello, John!
This in-built frozenset() method in Python will return a frozen set object.
frozenset({'kiwi', 'grape', 'peach'})
<class 'frozenset'>
This in-built getattr() method returns the value of the specified attribute, which can be a method or a property.
no_name
This in-built globals() method in Python will return a dictionary containing all the global symbols.
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00000262340B0F70>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\x\\Desktop\\my\\test.py', '__cached__': None}
This in-built hasattr() method in Python will return True if the specified object has the specified method or property.
True
This in-built hash() method in Python will return the unique numeric representation of a specified object.
The output will probably be different from session from session.
230584300921356469
This in-built help() method in Python will print out a message containing helpful information.
Welcome to Python 3.9's help uti...(redacted to avoid plagiarism)
Python's in-built hex() method will take a base-2 or base-10 number and convert it into a hexadecimal value.
0xa
0xa
This in-built id() method in Python will return a unique numeric representation of an object. This id is globally unique.
The id generated will be different from session to session.
1900882392064
This in-built int() method in Python will return an integer value.
1234
This in-built isinstance() method in Python will return True if the specified object is an instance of an object.
True
This in-built issubclass() method in Python will True if the specified class is a subclass.
True
This in-built iter() method in Python will return an iterable object.
<list_iterator object at 0x000001EF2FDF6FD0>
This in-built len() method in Python will return the length of an object.
4
This in-built list() method in Python will return a List.
[1, 2, 3, 4]
This in-built locals() method in Python will return the local symbols table.
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000016951290F70>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\Users\x\Desktop\my\test.py', '__cached__': None}
This in-built map() method in Python will return the specified iterator.
4
4
5
This in-built max() method in Python will return the largest item in an iterable.
45
This in-built memoryview() method in Python will return a memory view object. A memory view object can be indexed.
74
This in-built min() method in Python will return the smallest item in a list.
1
This in-built next() method in Python will return the next item in an iterable.
1
2
This in-built object() method in Python will return a new object.
<object object at 0x000001E6184E8190>
This in-built oct() method in Python will format the number into octal representation. This method will return a string.
0o10 <class 'str'>
This in-built open() method in Python will attempt to establish a connection to a file. If successful, it will return a reference object; else, an exception will be raised.
This in-built ord() method in Python will convert an integer representing the Unicode of the character.
65
This in-built pow() method in Python will return the value of a to the power of b.
8
Python's in-built print() method will print the text to the standard output device, i.e., the screen terminal.
learnpython.tech
This in-built property() method in Python will gets, sets, and deletes a property.
This in-built range() method in Python will return a sequence of a number.
0
1
2
This in-built repr() method in Python will return the readable representation of an object. This method will convert the object into its string representation.
[1, 2] <class 'str'>
This in-built reverse() method in Python will reverse the iterator and return it.
3
2
1
This in-built round() method in Python will round a number and return it.
1
This in-built set() method in Python returns a new set object. This method accepts an iterable.
{1, 2, 3, 4}
This in-built method in Python will set an attribute(method or property) to an object.
Hello!
James Bond
This in-built slice() method in Python returns a slice object.
(1, 2)
This in-built sorted() method in Python accepts an iterable and returns a sorted list.
[0, 1, 2, 3, 5, 6, 7]
This in-built str() method in Python will convert the object into a string.
1234
{1: 1}
This in-built sum() method in Python will return the sum of all items in an iterator.
10
This in-built super() method in Python returns the instance of the superclass. This method is discussed in classes.
This in-built tuple() method in Python return accepts an iterable and returns a tuple object.
(1, 2, 3, 4)
This in-built type() method in Python returns the type of the object passed.
<class 'int'>
<class 'dict'>
This in-built vars() method in Python returns the object's __dict__ property.
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000016C212C0F70>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\Users\x\Desktop\my\test.py', '__cached__': None}
This in-built zip() method in Python will return an iterator from more than one iterators.
(1, 3)
(2, 4)
In this chapter, we learned about the importance of using the in-built methods in reducing the workload by using a standard set of methods to avoid potential errors or bugs. Additionally, we briefed upon all in-built methods.