How to Convert an Octal Escape Sequence in Python – And Vice Versa?

This tutorial will show you how to convert an But let’s quickly recap what an octal escape sequence is in the first place! πŸ‘‡ What Is An Octal Escape Sequence?πŸ’‘ An Octal Escape Sequence is a backslash followed by 1-3 octal digits (0-7) such as \150 which encodes the ASCII character ‘h’. Each octal escape … Read more

Python Int to Hex | String Formatting

πŸ’¬ Question: How to use Python’s string formatting capabilities — f-strings, percentage operator, format(), string.format() — to convert an integer to a hexadecimal string? Lowercase Solution without ‘0x’ Prefix You can convert an integer my_int to a simple lowercase hex string without ‘0x’ prefix by using any of the four string formatting variants—all based on … Read more

Python Hex String to Little Endian (Bytes/Integer)

What Is Big/Little Endian? Computers perform computations on data that is represented with bytes, i.e., sequences of 0s and 1s. A computer program manipulates data by loading data from memory (RAM, Cache, Disk, SSD), performing computations based on that data, and storing the resulting data back into the memory. A computer program loads and stores … Read more

Python Convert Hex String to Binary

πŸ’¬ Question: How to convert a given hex string such as ‘0xF’ to a binary number such as ‘0b1111′? There are multiple variants of this problem depending on how you want the conversion and from which type to which type. In this article, we’ll go over the different methods from simple to more sophisticated. Let’s … Read more

Hex String to Hex Integer in Python

πŸ’¬ Question: Given a hexadecimal string such as ‘0xf’ in Python. How to convert it to a hexadecimal number in Python so that you can perform arithmetic operations such as addition and subtraction? The hexadecimal string representation with the ‘0x’ prefix indicates that the digits of the numbers do have a hexadecimal base 16. In … Read more

How to Sum the Digits of a Number in Python?

Problem Formulation Given an integer number. How to sum over all digits of this number to compute the crossfoot (cross total)? Consider the following examples: 12 –> 1+2 = 3 123 –> 1+2+3 = 3 244 –> 2+4+4 = 10 981223 –> 9+8+1+2+2+3 = 25 Method 1: sum() with Generator Expression The Python built-in sum(iterable) … Read more

3 Best Ways to Generate a Random Number with a Fixed Amount of Digits in Python

Coding Challenge βš”οΈ Challenge: Given an integer d representing the number of digits. How to create a random number with d digits in Python? Here are three examples: my_random(2) generates 12 my_random(3) generates 389 my_random(10) generates 8943496710 I’ll discuss three interesting methods to accomplish this easily in Python—my personal favorite is Method 2! Shortest Solution … Read more

How to Randomly Sample from a Python List?

In data science, you will need to learn how to generate random numbers, which in Python, we can do with the random module. In this tutorial, you’ll learn how to solve the following programming challenge: βš”οΈ Challenge: How to do random sampling from a list in Python? Without further ado, let’s dive right into it! … Read more