How to Split a Byte String into Lines?

Problem Formulation: Given a byte string that contains new-line characters ‘\n’. How to split the byte string into a list of lines? Example: You want to transform the byte string b’your\nbyte\nstring’ into the list of byte strings [b’your’, b’byte’, b’string’] using b’\n’ as a newline separator. Given: b’your\nbyte\nstring’ Goal: [b’your’, b’byte’, b’string’] Solution: To split … Read more

How to Calculate Percentiles in Python

This article deals with calculating percentiles. Percentiles are statistical indicators that are used to describe specific portions of a sample population. The following sections will explain what percentiles are, what they are used for and how to calculate them, using Python. As you will see, Python allows solving this problem in multiple ways, either by … Read more

Python frozenset() — A Simple Guide with Video

Python’s built-in frozenset() function creates and returns a new frozenset object. A frozenset is an immutable set—so you cannot change the frozenset after creation and set methods such as add() or remove() don’t work on the frozenset. Without an argument, frozenset() returns an empty frozenset. With the optional argument, frozenset(iter) initializes the new frozenset with … Read more

How to Print an Integer with Commas as Thousands Separators in Python?

Problem Formulation: Given an integer number. How to convert the integer to a string representation for printing or other use that has thousand separators? Example: Given an integer number 1000000. You want the string representation ‘1,000,000’. Method 1: f-Strings Using the modern f-strings is, in my opinion, the most Pythonic solution to add commas as … Read more

My Business and Coding Book Recommendations

“Readers are leaders.” Many Finxters seek mentorship—and write in asking me for book recommendations. I read about one business or programming book per week and this simple habit has completely transformed my life. In this article, I’ve compiled my top list of business and programming books—ordered by how great I think the book is. All … Read more

How to Install a Python Package with a .whl File?

Problem Formulation: Given a file yourPackage.whl that resides in the folder C:\your\folder\. How to install it on your Windows machine? Background .whl Files A .whl file (read: wheel file) is a zip archive that contains all the files necessary to run a Python application. What is a wheel? It’s a built-package format for Python, i.e., … Read more

Python set() Function — A Simple Guide with Video

Python’s built-in set() function creates and returns a new set object. A set is an unordered collection of unique elements. Without an argument, set() returns an empty set. With the optional argument, set(iter) initializes the new set with the elements in the iterable. Read more about sets in our full tutorial about Python Sets. Usage … Read more

How Does Tuple Comparison Work In Python?

A Quick Introduction To Tuples Python consists of 4 built-in data types that are used to store collections of data. These data types are: List Set Dictionary Tuple A tuple allows you to store multiple items within a single variable. Hence, it is a collection that is ordered and unchangeable/immutable. Also, tuples are heterogeneous as … Read more

Logistic Regression Scikit-learn vs Statsmodels

What’s the difference between Statsmodels and Scikit-learn? Both have ordinary least squares and logistic regression, so it seems like Python is giving us two ways to do the same thing. Statsmodels offers modeling from the perspective of statistics. Scikit-learn offers some of the same models from the perspective of machine learning. So we need to … Read more

How to Index 1D NumPy Arrays?

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. Problem Formulation: Given a one-dimensional NumPy array arr. How to access the i-th value in the array? How to access all values between the i-th value (included) and the j-th value (excluded) from the array? How to access all values … Read more