Functions

An Introduction To Closures and Decorators in Python

Today’s tutorial will introduce two slightly more advanced concepts of closures and decorators in Python. We’ll explain what they are, how each is defined, and where and how they will help in your coding. Nested Functions I’m sure you are all familiar with functions, and some of you may have used, or heard of, nested …

An Introduction To Closures and Decorators in Python Read More »

How to Limit Floats to Two Decimal Points in Python?

Introduction Problem Formulation: Given a floating-point value. How to limit it to two decimal places in Python? Example: Let’s have a look at a program where you have to calculate the value of pi. Output: Perfect! pi yields you a value = 3.141592653589793. But, what if you need the value of pi only up to …

How to Limit Floats to Two Decimal Points in Python? Read More »

How to Get a Function Name as a String in Python?

Problem Formulation Given a function object assigned to a name. How to get the name of the function as a string? For example, consider the following function your_function. How to get the name “your_function” from this? Your desired value of the result stored in string_name is the string “your_function”. Method 1: Use the __name__ Attribute …

How to Get a Function Name as a String in Python? Read More »

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 »