Python Built-in Functions

Python Dunder Methods Cheat Sheet

When searching for a list of dunder methods with explanations, i.e., a cheat sheet on Python magic methods, I only found a couple of resources all over the web—each covering only a fraction of dunder methods. So, here’s my collection of 127 dunder methods (also called “magic methods”) from those sources, sorted alphabetically. I provide …

Python Dunder Methods Cheat Sheet Read More »

What’s the Difference Between exit(0) and exit(1) in Python?

The function calls exit(0) and exit(1) are used to reveal the status of the termination of a Python program. The call exit(0) indicates successful execution of a program whereas exit(1) indicates some issue/error occurred while executing a program. What is the Exit Code? Let’s have a look at some examples to get a clear picture …

What’s the Difference Between exit(0) and exit(1) in Python? Read More »

dir() versus __dir__() – What’s the Difference?

Problem Formulation What’s the difference between the built-in dir() function and __dir__ dunder method in Python? Quick Answer Python’s built-in function dir(object) returns a list of the object’s attribute names and method names. The dir() function is a wrapper around the __dir__() method because it internally calls the object’s dunder method object.__dir__(). But the two …

dir() versus __dir__() – What’s the Difference? Read More »

Python List of Dunder Methods

When searching for a list of dunder methods, I only found a couple of resources all over the web—each covering only a fraction of dunder methods. So, here’s my collection of 127 dunder methods (also called “magic methods”) from those sources, sorted alphabetically: __abs__ __add__ __aenter__ __aexit__ __aiter__ __and__ __anext__ __await__ __bool__ __bytes__ __call__ __ceil__ …

Python List of Dunder Methods Read More »

How to Interleave Two Strings of Variable Lengths in Python?

Half an hour ago, my friend and coauthor of the textbook “Coffee Break NumPy” asked me the following question via WhatsApp: Problem Formulation How would you solve the problem of interleaving two strings in Python: Input: String s1= “AAA” and string s2 = “BBBBB” Output: String s=”ABABABBB” Being obsessed with finding the most Pythonic way …

How to Interleave Two Strings of Variable Lengths in Python? Read More »

How to Print a NumPy Array Without Brackets in Python?

Note that this tutorial concerns NumPy arrays. To learn how to print lists without brackets check out this tutorial: How to Print a List Without Brackets in Python? Problem Formulation Given a NumPy array of elements. If you print the array to the shell using print(np.array([1, 2, 3])), the output is enclosed in square brackets …

How to Print a NumPy Array Without Brackets in Python? Read More »

How to Print a Tuple Without Parentheses in Python?

Problem Formulation Given a tuple of elements. If you print the tuple to the shell using print((1, 2, 3)), the output is enclosed in parentheses like so: (1, 2, 3). But you want the list without brackets like so: 1, 2, 3. How to print the tuple without enclosing parentheses? Method 1: Unpacking The asterisk …

How to Print a Tuple Without Parentheses in Python? Read More »

How to Print a Dictionary Without Brackets in Python?

Problem Formulation Given a dictionary of key value pairs in Python. If you print the dictionary to the shell using print({‘a’: 1, ‘b’: 2}), the output is enclosed in curly brackets (braces) like so: {‘a’: 1, ‘b’: 2}. But you want the dictionary without brackets like so: ‘a’: 1, ‘b’: 2. How to print the …

How to Print a Dictionary Without Brackets in Python? Read More »