Functions

The Reduce Function in Python 3: Simply Explained

💡 The reduce() function from Python’s functools module aggregates an iterable to a single element. It repeatedly merges two iterable elements into a single one as defined in the function argument. By repeating this, only a single element will remain — the return value. Minimal Example Here’s the minimal example: The code performs the following steps: …

The Reduce Function in Python 3: Simply Explained Read More »

How to Dynamically Create a Function in Python?

Problem Formulation There are different variants of this problem that all ask the same thing: How to create a function dynamically in Python? How to define a function at runtime? How to define a function programmatically? How to create a function from a string? There are many ways to answer these questions—most web resources provide …

How to Dynamically Create a Function in Python? Read More »

How to Count the Number of Function Arguments in Python?

💡Introduction You might have a code in Python such that certain functions have no arguments, whereas the others have numerous arguments. While there are times, we have functions with arguments unknown. Thus, we may have n number of arguments in a function, and sometimes we don’t even have a clue about the input size of …

How to Count the Number of Function Arguments in Python? Read More »

Python – How to Check if a Dict Key Exists?

Summary: To check whether a key exists in a dictionary, you can use: The in keyword The keys() method The get() method The has_key() method Overview Mastering dictionaries is one of the things that differentiates the expert coders from the intermediate coders. Why? Because dictionaries in Python have many excellent properties in terms of runtime—and …

Python – How to Check if a Dict Key Exists? Read More »

Python iter() — A Simple Illustrated Guide with Video

Python’s built-in iter() function returns an iterator for the given object. For example, iter([1, 2, 3]) creates an iterator for the list [1, 2, 3]. You can then iterate over all elements in the iterator, one element at a time, in a for or while loop such as: for x in iter([1, 2, 3]). Basic …

Python iter() — A Simple Illustrated Guide with Video Read More »

Python memoryview() — Tame That Strange Beast!

The Python memoryview(arg) function returns a memoryview object of the given bytes or bytearray argument. This exposes the argument’s internal data (buffers) to help you access the data without intermediate copying. Syntax: memoryview(object) Arguments object Bytes or Bytearray object for which the memoryview should be returned Return Value memoryview Returns memoryview of the object. Python …

Python memoryview() — Tame That Strange Beast! Read More »

NumPy arange(): A Simple Illustrated Guide

The np.arange() function appears in 21% of the 35 million Github repositories that use the NumPy library! This illustrated tutorial shows you the ins and outs of the NumPy arange function. So let’s get started! What’s the NumPy Arange Function? The np.arange([start,] stop[, step]) function creates a new NumPy array with evenly-spaced integers between start …

NumPy arange(): A Simple Illustrated Guide Read More »

Python vars() Function

Python’s built-in vars() function returns the __dict__ attribute of an object—a dictionary containing the object’s changeable attributes. Without argument, it returns the local symbol table similar to locals(). Python’s built-in vars() function returns a dictionary of name: value mappings of all the names defined in the local scope or the scope of the optional object …

Python vars() Function Read More »