Python is a dynamic and multi-paradigm programming language that possesses a lengthy history of success and community support. Some of the fundamental characteristics of Python are its simplicity, readability and flexibility, making it among the most popular and sought-after skills to learn as a software engineer.
Python has a huge community of developers that have contributed hundreds of thousands of third-party packages and application frameworks to the Python Package Index (PyPI). But Python also includes several built-in packages and functions out of the box that simplify adoption, increase developer productivity and promote code consistency. As a result, teams can be productive from day one, and start working on business features much faster than with Java or Go.
Let’s explore what’s included in Python the moment you install a distribution like ActiveState Python 3.7, and how it differs from other popular programming languages like Java or Go.
Key Built-in Elements in Python
As you would expect, Python provides support for all of the standard data types you would expect, and then builds on them to deliver some fairly sophisticated programming tools. And like any mature programming language, Python has adopted third-party contributions over time, pulling them into the core language. Let’s take a look at some of them.
Built-in Constants in Python
A small number of built-in constants exist in the default namespace, including:
>>> None is None True >>> True == 1 True >>> False == 0 True >>> [1,...,10] [1, Ellipsis, 10]
Built-in Functions in Python
Python exports several handy functions for day-to-day programming tasks. The majority of them work on lists and iterators as they’re important data structures. There are also a few functions that work with types and meta-programming. Here are a few examples:
Help
Print help for an object.
>>> help(Ellipsis) Help on ellipsis object: class ellipsis(object) | Methods defined here: …
Locals
Shows the local symbol table, which is useful for debugging scope variables.
>>> locals() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
List + Range
Range creates an iterable sequence of numbers. List converts those numbers to a list. For example, to create a list of numbers from 1 up to (but not including) 10:
>>> list(range(1,10)) [1, 2, 3, 4, 5, 6, 7, 8, 9]
Map + Filter
While you could first sort and then filter a list, you can more efficiently use map and filter, which performs both functions with a single iteration of the list. For example:
>>> list(filter(lambda x: x < 2, map(int, "1,2,3,4,5,6".split(",")))) [1]
Divmod
Gets the quotient and remainder of x divided by y, which can be very handy for some calculations.
>>> divmod(6, 4) (1, 2)
Sorted + Enumerate
Sorted performs an ascending sort of an iterable list or set of tuples. You can also use custom key functions to implement special sorting criteria. In the following example, we have a list of tuples (index, value) that we need to sort by the second item, so we’ll configure a custom key function:
>>> sorted(enumerate([5,3,2,6]), key=lambda x: x[1]) [(2, 2), (1, 3), (0, 5), (3, 6)]
Built-in Types in Python
Python has an array of built-in types that cover a host of useful applications, such as converting from one type to another, doing comparisons, metaclass information, etc. Let’s see some examples:
Converting integers to bytes:
>>> (8).to_bytes(2, byteorder='big') b'\x00\x08'
Count the number of occurrences of an item in a list:
>>> [1,2,3,4,1,2,3,4].count(1) 2
Sort in place
>>> x = [6,5,4,3,1] >>> x.sort() >>> x [1, 3, 4, 5, 6]
Built-in Packages in Python
The majority of the utilities that Python offers are part of the Python Standard Library. Here are some examples of the packages that are commonly used in professional development:
Dataclasses
Dataclasses offer convenient decorators for working with Data Objects. Decorators take functions as inputs and extend their functionality without modification.
>>> from dataclasses import dataclass >>> @dataclass ... class RequestResponse: ... code: int ... message: str ... >>> r = RequestResponse(404, 'Not Found') >>> repr(r) "RequestResponse(code=404, message='Not Found')"
gettext
gettext is useful for handling localization (l10n) and internationalization (i18n) of strings
>>> import gettext >>> _ = gettext.gettext >>> print(_('Translate me')) Translate me
json
json converts to and from JSON objects
>>> json.loads('{"id":"1", "name":"Alex"}') {'id': '1', 'name': 'Alex'} >>> json.dumps({'id':1, 'name': 'Alex'}) '{"id": 1, "name": "Alex"}'
glob
glob searches for files in paths
>>> import glob >>> glob.glob('.*') ['.vue-cli-ui', '.config', '.idea-build', '.wakatime.cfg', '.yarn', '.docker', '.ivy2', …]
hmac
hmac implements algorithms for hashing messages
>>> hmac_object = hmac.new('secret'.encode('utf-8'), 'hello'.encode('utf-8'), 'sha256') >>> key = hmac_object.hexdigest() >>> key '88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b
How Does Python Differ from Java or Go?
One distinct advantage that Python offers over Java or Go is the number of built-in types, functions and packages that are available. Coupled with indentation-level syntax, Python programs are just easier to read and maintain.
While Go may not need semicolons at the end of each statement, it suffers from verbose syntax for typing functions, error handling and abstractions. Go also relies on compile-time code generation facilities to handle dependency injection (for example https://github.com/google/wire), or other advanced patterns. With Python, it can be implemented easily using pre-existing language features.
Java can be even worse when it comes to verbose syntax, especially if you try to apply all the reusable object-oriented patterns from the Gang of Four Book. The power of Java lies in the JDK ecosystem, but each JDK distribution (Java Platform, Standard Edition, Java Platform, Enterprise Edition, or Java Platform, Micro Edition) offers a diverse set of packages. In Python, you get the same set of built-in packages and libraries – consistently.
Next Steps
Python has a vast ecosystem of libraries and packages covering areas like Machine learning, AI, Network programming, Big Data, Web app development, and more. If you need functionality that is not covered by the built-in packages, you can search the official Python Package Index for the desired option.
ActiveState also includes a list of popular and ready-to-use community packages for myriad applications. The advantages of using ActiveState’s Python distribution is that you get commercial support, something that many businesses that use Python tools for their core infrastructure will find very useful. To obtain a list of all the platforms ActiveState offers, visit their products page.