“is” vs “==” Python Identity and Equality

πŸ’¬ Question: What is the difference between Python’s is and == operators? Answer The Python “==” operator compares equality of values whereas the Python “is” operator compares identity of objects, i.e., do the two operands point to the same object in memory. For example, the expression [1, 2, 3] == [1, 2, 3] returns True … 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

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 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

Python getattr() and setattr() Nested

Understanding Python’s setattr() and getattr() Functions Next, you’ll learn about the “normal”, non-nested and non-recursive get and set attribute functions. If you already know them well, there’s no need to read this section and you can skip ahead right to the problem formulation and solution. Let’s start with the setattr() function, followed by getattr(). setattr() … Read more

Python Create List of Objects

You can create a list of objects using the list comprehension statement [MyObject() for _ in range(n)] that repeats n times the creation of a new object using the MyObject() constructor. The result is a list of n new objects (not copies). Here’s a minimal example: You can see that all of them point to … Read more

Python bytes vs bytearray

What’s the Difference Between bytes() and bytearray()? The difference between bytes() and bytearray() is that bytes() returns an immutable and bytearray() returns a mutable object. So you can modify a bytearray but not bytes type. Here’s a minimal example that nicely demonstrates the difference of the two functions: You create two variables a and b. … Read more

The Fasting Cure [Book Summary + Free Download]

Fasting has many scientifically proven benefits for your body and overall health: Intermittent fasting improved blood pressure and resting heart rates as well as other heart-related measurements. Physical performance. Young men who fasted for 16 hours showed fat loss while maintaining muscle mass. Mice who were fed on alternate days showed better endurance in running. … Read more

How to Remove ‘\x’ From a Hex String in Python?

Problem Formulation + Examples πŸ’¬ Question: Given a string of hexadecimal encoded string such as ‘\x00\xff\xf2’. How to remove the ‘\x’ prefixes or characters from the hex string? Here are a few examples of what you want to accomplish: Hex String Desired Output ‘\x00\xff\xf2′ ’00fff2’ ‘\x41\x42\x43’ ‘414243’ ‘\x53′ ’53’ ‘\xff\xff\xff\xff\xff’ ‘ffffffffff’ ‘\x00\x6a\x6f’ ‘006a6f’ Strawman Solutions … Read more

Python Hex to Float Conversion

Problem Formulation πŸ’¬ Question: Given a hexadecimal string. How to convert it to a float? Examples Here are a few example conversions where each hex string represents four bytes, i.e., two hex characters per byte. Hex String Float ‘0f0f0f0f’ 7.053344520075142e-30 ‘4282cbf1’ 65.39832305908203 ‘43322ffb’ 178.1874237060547 ‘534f11ab’ 889354649600.0 Solution – Convert Hex to Float The expression struct.unpack(‘!f’, … Read more