Python Modulo

 When I studied computer science, the professors pushed us to learn the theory behind modulo operations and residual classes. But many of us lacked motivation. We could not see why calculating the remainder of the division, i.e., modulo, is such an important concept. Yet, many practical code projects later, I have gained the experience that … 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 … 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 … 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 … 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 … 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 … Read more

Python bin() Function

Python’s built-in bin(integer) function takes one integer argument and returns a binary string with prefix “0b”. If you call bin(x) on a non-integer x, it must define the __index__() method that returns an integer associated to x. Otherwise, it’ll throw a TypeError: object cannot be interpreted as an integer. Argument integer An integer value or … Read more

Python ascii() Function

Python’s built-in ascii(object) function takes one object argument and returns the string representation of that object. The function calls the repr() built-in function and replaces non-ASCII characters with the character code \x. For example, calling ascii(‘MΓΌnchen’) results in the ascii string ‘M\xfcnchen’ by replacing character ΓΌ with character code \xfc. Argument object Iterable such as … Read more

Python any() Function

Python’s built-in any(x) function takes one iterable as an argument x such as a list, tuple, or dictionary. It returns True if at least one of the elements in the iterable evaluates to True using implicit Boolean conversion, otherwise it returns False. If the iterable is empty, e.g., any([]), it returns False because the condition … Read more

Python all() Function

Python’s built-in all(x) function takes one iterable as an argument x such as a list, tuple, or dictionary. It returns True if all iterable elements evaluate to True using implicit Boolean conversion, otherwise it returns False. If the iterable is empty, all() returns True because the condition is satisfied for all elements. Argument x -> … Read more