## How to Normalize a NumPy Matrix

In this blog post, we’ll discuss how to normalize a matrix using the popular Python library NumPy. But first things first: 👇 What is Normalization? In mathematics, normalizing refers to making something standardized or regular. Normalization of a matrix is a process of scaling the matrix so that the elements of the matrix have a … Read more

## Tilde (~) Operator in Python

What is the Meaning of the Tilde Operator ~ in Python? Python’s Tilde ~n operator is the bitwise negation operator: it takes the number n as binary number and “flips” all bits 0 to 1 and 1 to 0 to obtain the complement binary number. For example, the tilde operation ~1 becomes 0 and ~0 … Read more

## NumPy Array Slicing – A Helpful Guide

Python NumPy array slicing is used to extract parts of data from an array. Array Slicing is often used when working with NumPy. In this article, we will go over the methods of array slicing, from basic to more advanced techniques. We will use the np.array() function to create our array examples. Before practicing any … Read more

## How to Create a DataFrame From Lists?

Pandas is a great library for data analysis in Python. With Pandas, you can create visualizations, filter rows or columns, add new columns, and save the data in a wide range of formats. The workhorse of Pandas is the DataFrame. 👉 Recommended: 10 Minutes to Pandas (in 5 Minutes) So the first step working with … Read more

## Why Use NumPy Instead of List Operations?

NumPy vs Lists NumPy is an important Python library used for numerical operations and data science. Its mathematical operations on arrays are faster and more efficient than on lists. Since NumPy is primarily written in C and C++, it can perform faster operations. NumPy arrays are homogeneous arrays that can store only one type of … Read more

## How to Write a Hex String as Binary Data & Binary File in Python?

Hex String as Binary Data To convert a hex string such as ‘FF0001AF’ to binary data, use the binascii.unhexlify(hex_str) function that returns the binary data represented by the hexadecimal string. Note that this converts two hex string digits to one byte, i.e., hex string ‘F0’ is converted to the byte representation 11110000, or in binary … Read more

## Python Hex String to Big 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

## Format Integer List to Hex String – Six Pythonic Ways

Problem Formulation 💬 Question: How to create a string of hex digits from a list of integers (0–255) so that each hex digit consists of two digits such as “00”, “01”, …, “fe”, “ff”? Here’s an example input/output pair: In: [0, 1, 2, 3, 255, 254, 253] Out: ‘00010203fffefd’ Method 1: Bytearray The easiest way … Read more

## 7 Pythonic Ways to Copy a Dictionary

Problem Formulation and Solution Overview This article will show you how to copy a Dictionary in Python 7 different ways. To make it more interesting, we have the following running scenario: Mr. Smith, a High School Math Teacher, has developed an exciting way to grade multiple-choice exams. He has designed a Dictionary with the exam … Read more

## Python – Hex String to Bytearray

Problem Formulation Given a string in hexadecimal form: How to convert the hex string to a bytearray object in Python? Here are a few examples: Hex String Bytearray Object ’01’ bytearray(b’\x01′) ’04’ bytearray(b’\x04′) ’08’ bytearray(b’\x08′) ’01 0a’ bytearray(b’\x01\n’) ’01 02 0e 0f 0f’ bytearray(b’\x01\x02\x0e\x0f\x0f’) ‘0f 0f’ bytearray(b’\x0f\x0f’) Hex String to Bytearray using bytearray.fromhex(hex_string) To convert … Read more

## Print Hex with Uppercase Letters in Python

Quick answer: 👇 Use the f-string expression f’0x{val:X}’ to convert an integer val to a hexadecimal string using uppercase hex letters as set by the capitalized hex format specifier X. Optionally, you can add the prefix ‘0x’ in front of this. Example: 💡 Note: This is Method 3 of this tutorial, and it is presented … Read more

## Python Hex String to Integer Array or List

Problem Formulation Given a hex string, i.e., a string of hexadecimal digits like so: 01ffa87135affcd45deebe You want to convert a batch of k hex digits to an integer. For example with k=2, you obtain the following batches of two hex digits each: 01 ff a8 71 35 af fc d4 5d ee be Now, convert … Read more