Python Built-in Functions

A Guide to Python’s pow() Function

Exponents are superscript numbers that describe how many times you want to multiply a number by itself. Calculating a value raised to the power of another value is a fundamental operation in applied mathematics such as finance, machine learning, statistics, and data science. This tutorial shows you how to do it in Python! Definition For …

A Guide to Python’s pow() Function Read More »

Python exec() — A Hacker’s Guide to A Dangerous Function

Python’s exec() function executes the Python code you pass as a string or executable object argument. This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime. This way, you can run programmatically-created Python code. Have you ever wondered about the limits of a …

Python exec() — A Hacker’s Guide to A Dangerous Function Read More »

Python complex() — A Useless Python Feature?

The Python complex() method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those. This article shows you how to use Python’s built-in complex() constructor. You’ll not only learn …

Python complex() — A Useless Python Feature? Read More »

Python staticmethod()

Static methods are special cases of class methods. They’re bound to a class rather than an instance, so they’re independent on any instance’s state. Python’s built-in function staticmethod() prefixes a method definition as an annotation @staticmethod. This annotation transforms a normal instance method into a static method. The difference between static (class) methods and instance …

Python staticmethod() Read More »

Python classmethod()

Python’s built-in function classmethod() prefixes a method definition in a class as an annotation @classmethod. This annotation transforms a normal instance method into a class method. The difference between class and instance method is that Python passes the class itself as a first implicit argument of the method rather than the instance on which it …

Python classmethod() Read More »

Python range() Function — A Helpful Illustrated Guide

The Python range() function creates an iterable of subsequent integers within a given range of values. You can pass either only a stop argument in which case the range object will include all integers from 0 to stop (excluded). Or you can pass start, stop, and step arguments in which case the range object will …

Python range() Function — A Helpful Illustrated Guide Read More »

Python chr() Function

The Python chr() function takes one number as argument that is the specified Unicode and returns the character associated to this Unicode argument. For example, the call chr(101) returns the Unicode character ‘e’. The allowed range of arguments are all integers between 0 and 1,114,111 (included)—integers outside this interval will raise a ValueError. Here are …

Python chr() Function Read More »

Python sorted() Function

If you work in a data driven career, odds are you will at some point have to perform sorting on your data. Rather than writing your own sorting algorithm (which will most likely be far less efficient), Python provides a built-in function called sorted(). This function allows you to do basic sorting, such as arranging …

Python sorted() Function Read More »

Python callable() Function

Python’s built-in callable(object) returns True if you could call the object argument like a function with the trailing parentheses in object(). You can make any object callable by implementing the instance’s __call__() method. For example, callable(callable) returns True because callable is a function object. But callable(3) returns False because an integer is not a function …

Python callable() Function Read More »

Python bytes() Function

Python’s built-in bytes(source) function creates an immutable bytes object initialized as defined in the function argument source. A bytes object is like a string but it uses only byte characters consisting of a sequence of 8-bit integers in the range 0<=x<256. The returned byte object is immutable—you cannot change it after creation. If you plan …

Python bytes() Function Read More »