Found 35163 Articles for Programming

Why would you use the return statement in Python?

Rajendra Dharmkar
Updated on 02-May-2023 14:00:23

3K+ Views

The return statement in Python is used to return a value or a set of values from a function. When a return statement is encountered in a function, the function execution is stopped and the value specified in the return statement is returned to the caller. The main purpose of the return statement is to pass data from a function back to the caller. The caller can then use this data for further processing. Why use the return statement? The return statement is useful in many situations. Some common use cases include − Passing data back to the caller: As ... Read More

How variable scope works in Python function?

Sarika Singh
Updated on 19-Dec-2022 12:01:30

209 Views

In this article we will discuss about how a variable scope works in Python function. The scope of a name is the area of a program in which you can clearly access a name, such as variables. Hence, a variable scope is the region in which we can access a variable as shown in the following example - def multiplication(): product = 7*9 Here, the “product” variable is created inside the function, therefore it can only be accessed within it. In general, the python scope concept is presented using the rule called as the ... Read More

How to use *args and **kwargs in Python?

Sarika Singh
Updated on 19-Dec-2022 11:57:47

1K+ Views

In this article we will discuss about *args and **kwargs properties and their usage in Python. To pass unspecified number of arguments to a function usually *args and **kwargs properties are used. The *args property is used to send a non-keyword variable length argument to a function call whereas **kwargs keyword is used to pass a keyword length of arguments to a function. Keyword arguments (or arguments) are the values that is preceded by an identifier “=” in a function call for e.g. “name=”. The primary benefits of using *args and **kwargs are readability and convenience, but ... Read More

What is an anonymous function in Python?

Sarika Singh
Updated on 19-Dec-2022 12:35:50

18K+ Views

An anonymous function in Python is one that has no name when it is defined. In Python, the lambda keyword is used to define anonymous functions rather than the def keyword, which is used for normal functions. As a result, lambda functions are another name for anonymous functions. Syntax Following is the syntax of the lambda function - lambda [args] : expression Although a lambda function can have only one expression, it can have any number of arguments. A lambda can also be immediately invoked and is written in a single line of code. Example: Calling a lambda function ... Read More

How to use Lambda Function in Python?

Pranathi M
Updated on 16-Sep-2022 07:24:05

2K+ Views

The anonymous nature of Python Lambda Functions indicates that they lack a name. As we already know, a standard Python function is defined using the def keyword. Similar to this, the lambda keyword is utilized in Python to declare an anonymous function. Syntax The syntax of the lambda expression in python is as follows − lambda arguments: expression There is only one expression that is evaluated and returned in this function, regardless of the number of parameters. Lambda functions can be used anywhere that function objects are required. The fact that lambda functions are syntactically limited to a single ... Read More

Can Python functions run in html as javascript does?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

160 Views

It is not possible to run Python in any modern browser because none contain a python interpreter. Javascript is the only language which runs in a browser without plugins like Flash or ActiveX.One way to write Python code that will run in the browser is to use a "transpiler". This is a tool that will compile the python code into Javascript. So the browser is ultimately running the language it knows, but you're writing Python. There are already a number of languages like CoffeeScript, TypeScript and even React JSX templates that compile down to raw javascript.An example of a Python ... Read More

How to write recursive Python Function to find factorial?

Rajendra Dharmkar
Updated on 02-May-2023 13:49:46

429 Views

Factorial of a number is the product of all the positive integers from 1 to that number. For example, the factorial of 4 is 4*3*2*1 = 24. To find the factorial of a number using recursive Python function, we can define a function that calls itself with a smaller input until it reaches the base case, which is the factorial of 1, which is 1. Here is the code to find the factorial of a number using recursive Python function − def factorial(n): if n == 1: return 1 ... Read More

How to pass Python function as a function argument?

Rajendra Dharmkar
Updated on 02-May-2023 12:32:13

18K+ Views

In Python, you can pass a function as an argument to another function. This is known as a higher-order function. In other words, a function is called a higher-order function if it contains other functions as parameters or returns functions. Python supports the usage of higher-order functions as it is a very versatile programming language. Few examples of higher-order functions in Python are map(), filter(), sorted(), and reduce(). For example the function map() applies a function to all the items in an input list and returns a new list containing all the function call results and similarly other higher order ... Read More

How can a Python function return a function?

Rajendra Dharmkar
Updated on 31-Aug-2023 02:34:02

14K+ Views

Python supports first-class functions. In fact, all functions in python are first- class functions. Python may return functions from functions, store functions in collections such as lists and generally treat them as you would any variable or object. A function can return a function in Python because functions are treated as first-class objects. This means that you can assign a function to a variable, pass it as an argument to another function, or use it as a return value in a function. Defining functions in other functions and returning functions are all possible. In this code, the outer function defines ... Read More

How to retrieve source code from Python objects?

Rajendra Dharmkar
Updated on 31-Aug-2023 02:44:11

12K+ Views

Using the inspect module, you can retrieve the source code of a Python function, method, class, or module. The following example demonstrates how to retrieve a function's source code − Example import inspect def my_function(x, y): return x + y source_code = inspect.getsource(my_function) print(source_code) Output def my_function(x, y): return x + y This code defines a simple function my_function that accepts two arguments and returns the sum of those arguments. We then retrieve the source code of the my_function function using the inspect.getsource() function and store it in the source code variable. ... Read More

Advertisements