## How to End a Function Def in Python?

Problem Formulation Given a function definition in Python, starting with the keyword def: How to know when a “def” of a function ends? For example, in Java and C++, functions are enclosed with opening and closing parentheses {…}, so the ending of a function is not ambiguous. Ending a Function Syntactically In Python, whitespace indentation … Read more

## 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 … Read more

## How to Get the Current Time in Python?

[toc] Introduction As a programmer, you need to work on different time-based tasks in your program like logging and saving the records to the database and accessing the documents. In each of those cases, date and time assume a significant part in saving the importance and integrity of the data and information. So, you must … Read more

## How to Limit Floats to Two Decimal Points in Python?

[toc] 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 … Read more

## Python Operators Overview

What Are Python Operators? Python operators are special syntactical sugar to run basic operations without calling their respective methods. For example, you can use the + operator in a + b instead of the more clunky .add() method in a.add(b). Each operator has a unique symbol that is placed between the two arguments called operands. … 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 … Read more

## How To Apply A Function To Each Element Of A List?

Summary: This blog explains the ways to apply a given function to each element of a list. The best way to apply a function to each element of a list is to use the Python built-in map() function that takes a function and one or more iterables as arguments. It then applies the function to … 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: … Read more

## Python Set intersection()

Python’s set.intersection(sets) creates and returns a new set consisting of the elements that are members of all sets — this and the set argument(s). The resulting set has at most as many elements as any other set given in the argument list. Here’s a minimal example that creates a new set arising from the intersection … 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 … 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 … Read more

## How to Check if a Key Exists in a Python Dictionary?

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 … Read more