Found 27759 Articles for Server Side Programming

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

158 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

416 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

17K+ 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

How to use a global variable in a Python function?

Tarun Chandra
Updated on 31-Aug-2023 02:48:43

3K+ Views

There are 2 types of variables in Python namely Local variables and Global variables. Local variables mean the variables that are declared inside a function or inside a method whose impact or scope is only present inside that specific block and it doesn't affect the program outside that block. Global variables mean the variables that are declared outside any function or method and these variables have an impact or scope through the entire program. We can also instantiate global variables inside a function by using a global keyword, if we want to declare global variables outside a function then we ... Read More

How can we overload a Python function?

Rajendra Dharmkar
Updated on 28-Sep-2023 01:15:57

3K+ Views

In Python, you can define a method in such a way that there are multiple ways to call it. Depending on the function definition, it can be called with zero, one, two or more parameters. This is known as method overloading. In the given code, there is a class with one method, sayHello(). We rewrite as shown below. The first parameter of this method is set to None, which gives us the option to call it with or without a parameter. An object is created based on the class, and we call its method using zero and one parameter. To ... Read More

How can we create recursive functions in Python?

Rajendra Dharmkar
Updated on 28-Sep-2023 01:19:18

3K+ Views

Recursion is a programming technique, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition follows recursion, we call this function a recursive function. A recursive function has to be terminated before it can be used in a program. It terminates, if, with every recursive call, the solution of the problem becomes smaller and moves towards a base case, where the problem can be solved without further recursion. A recursion can lead to an infinite loop, if the base case is not ... Read More

Advertisements