How to Apply a Function to NumPy Elements

Problem Formulation and Solution Overview As a Pythonista, coding issues may occur where you need to apply a function against NumPy elements. To make it more fun, we have the following running scenario: We have a NumPy array containing five (5) negative numbers related to an inconsistent inventory count. Therefore, the Vice-President of Rivers Clothing … Read more

How to Apply a Function to List Elements

Problem Formulation and Solution Overview As a Pythonista, coding issues may occur where you need to apply a function against array/matrix elements. To make it more fun, we have the following running scenario: The organization Happy Mortgages has six (6) different Mortgage Terms available: 30-Year, 20-Year, 15-Year, 10-Year, 7-Year, and 5-Year terms. The US Federal … Read more

CΓ³mo convertir una cadena hexadecimal en un entero en Python

Planteamiento del problema Dada una cadena en forma hexadecimal: CΓ³mo convertir una cadena hexadecimal en un entero en Python Por ejemplo, quieres convertir la cadena hexadecimal ‘0xff’ en el entero decimal 255. AquΓ­ hay otros ejemplos: 0x0 –> 0 0x4 –> 4 0x8 –> 8 0x12 –> 18 0x16 –> 22 0x20 –> 32 0x24 … Read more

Top 13 Python Tricks for Data Analysis

This article focuses on analyzing the coronavirus dataset using Python language. We are not using any of the Python data analysis libraries. Instead, we’ll use our raw Python skills to write a function, slicing, and indexing. Also, we’ll use Python arithmetic operators such as sum() and division. Finally, we’ll use a lambda expression to perform … Read more

How To Apply A Function To Each Element Of A Dictionary?

This article shows you how to apply a given function to each element of a Python dictionary. The most Pythonic way to apply a function to each element of a Python dict is combining the dictionary comprehension feature and the dict.items() method like so: {k:f(v) for k,v in dict.items()} Note: All the solutions provided below … Read more

How To Apply A Function To Each Element Of A Tuple?

This article shows you how to apply a given function to each element of a tuple. The best way to apply a function to each element of a tuple is the Python built-in map(function, iterable) function that takes a function and an iterable as arguments and applies the function to each iterable element. An alternate … Read more

Python __format__() Magic Method

Syntax object.__format__(self, spec) The Python __format__() method implements the built-in format() function as well as the string.format() method. So, when you call format(x, spec) or string.format(spec), Python attempts to call x.__format__(spec). The return value is a string. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list … Read more

Python __set_name__() Magic Method

Python’s magic method obj.__set_name__(self, owner, name) method is created on an attribute obj when the class owner holding the attribute is created. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this … Read more

Python __init_subclass__() Magic Method

The Python class.__init_subclass__(cls) method is called on a given class each time a subclass cls for that class is created. Syntax class.__init_subclass__(cls) We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this … Read more

Python __getattribute__() Magic Method

Python’s magic method __getattribute__() implements the built-in getattr() function that returns the value associated with a given attribute name. If the __getattribute__() error results in an AttributeError due to a non-existent attribute, Python will call the __getattr__() function for resolution. Thus, the __getattribute__() method takes precedence over the __getattr__() method. We call this a “Dunder … Read more