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

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

Python format() Function: No-BS Guide by Example

The web is filled with shitty tutorials about Python’s formatting feature. At times, it can become really confusing—and it’s hard to see the forest for the trees. In this tutorial, I’ll try to gradually build a basic understanding of the built-in format() function, what it does, and how you can use it to become a … Read more

Python float() Function

Python’s built-in float(value) function converts the argument value to a float number. For example, float(’42’) converts the string value ’42’ into the float number 42.0. Argument value A Python object to be converted into a float number. The value object must have an __float__() method that returns the associated float number—otherwise a ValueError will be … Read more

Python int() Function

Python’s built-in int(value) function converts the argument value to an integer number. For example, int(’42’) converts the string value ’42’ into the integer number 42. The int() function on a float argument rounds down to the closest integer. Argument value A Python object to be converted into an integer number. The value object must have … Read more

How to Remove Everything After the Last Character in a String?

Problem Formulation Given string s, and character c. How to remove all characters in s after the first occurrence of c? Example Given: – string s = ‘hello world’, and – empty space character c = ‘ ‘. Desired result: ‘hello’ Method 1: string.index() + slicing To remove everything after the first occurrence of character … Read more

How to Get the Command History in Python?

If you’re working with the command line in Windows or the terminal in Linux and macOS, you know about the feature of the command-line history. You can access all previously issued commands with the arrow up or arrow down keys. As a Python coder, you want to be able to control everything from your Python … Read more

EZGmail and Python — Managing Your Emails Programmatically

Hey Finxters! Among the many daily tasks you can achieve with Python, there is one Siri-like task that comes quite handy: managing your emails in a programmatic way.  Of course, many emails need your human understanding to be processed properly, and this article is not about implementing a neural network to fine-tune every single email … Read more

How to Get the Duration of a Video in Python?

Problem Formulation: Given a video file. How to get its duration in seconds using Python code? The video can have any of the following video formats: MP4 AVI Flash Video Mov Method 1: Using Subprocess + ffprobe Within your Python shell, you can run any external command with the module subprocess. The external tool ffprobe … Read more