Found 10769 Articles for Python

How to use Lambda Function in Python?

Sarika Singh
Updated on 14-Apr-2025 15:39:42

3K+ Views

Lambda Function in Python Lamda functions are inline functions, i.e., functions that are written in a single line instead of using multiple lines. These are anonymous functions (functions without a name). We can define a lambda function using the lambda keyword. These are typically used when we need to return a function from another function or accept a function as a parameter. We can also use lambda functions with built-in functions like map(), filter(), and sorted(), where we need to perform a small operation quickly without writing a separate full function. Lamda function is basically a shortcut for writing a ... Read More

Can Python functions run in html as javascript does?

Sarika Singh
Updated on 11-Apr-2025 15:31:27

270 Views

Unlike JavaScript, we cannot run Python functions (or scripts) directly in HTML, but we can use tools to make it work. While creating web pages, we use HTML to structure the content and JavaScript to make the page interactive (directly in the browser). We can also write the JavaScript code within an HTML file using the tag. If you are learning Python, you might wonder if you can run Python code the same way inside HTML. The short answer is no. You can't run Python code directly in HTML like JavaScript. Web browsers are designed only to understand HTML, ... Read More

How to write recursive Python Function to find factorial?

Sarika Singh
Updated on 11-Apr-2025 10:24:37

905 Views

We can write a recursive function in Python to find the factorial of a number. Recursion means that a function calls itself repeatedly to work through different stages of the same task. This technique is useful for tasks that follow a repetitive pattern or have a step-by-step structure like calculating factorials, generating the Fibonacci series, or navigating tree structures (tree traversal). The factorial of a number is the product of all positive integers from 1 to that number. It is represented using the symbol n! and defined as − n! = n × (n - 1) × (n - ... Read More

How to pass Python function as a function argument?

Sarika Singh
Updated on 11-Apr-2025 14:43:30

27K+ Views

We can pass a Python function as a function argument by treating it like any other object. In Python, functions are first-class objects. This means you can treat them like any other value, assign them to variables, return them from other functions, and even pass them as arguments to other functions. Passing functions as arguments allows you to create flexible and reusable code. This is useful in cases like sorting, filtering, customizing behavior, and implementing callbacks. In this article, we will explore how you can pass a function to another function in Python and how it helps in building higher-order ... Read More

How can a Python function return a function?

Sarika Singh
Updated on 14-Apr-2025 15:45:33

18K+ Views

In Python, functions are treated as first-class objects, which means that all functions in Python are treated like any other object or variable. You can assign a function to a variable, pass it as an argument to another function, return it from a function, or even store it in data structures like lists. One very useful feature in Python is that a function can return another function. This means you can define a function inside another function and return it as a result. This is used in advanced programming concepts like closures, decorators, and higher-order functions. Returning a Simple Function ... Read More

How to retrieve source code from Python objects?

Sarika Singh
Updated on 11-Apr-2025 14:50:14

15K+ 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: Retrieve Function Source Code 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. Finally, the source code is output to the console. The inspect.getsource() function operates by reading the function's source code from the file in which it ... Read More

How to use a global variable in a Python function?

Sarika Singh
Updated on 11-Apr-2025 14:55:53

4K+ Views

We can use a global variable inside a Python function by declaring it using the global keyword. This allows us to read or modify a variable that is defined outside the function's local scope. By default, any variable you create or change inside a function is considered a local variable (only used inside that function). If we try to change a global variable without declaring it as global, Python will create a new local variable with the same name, and the actual global variable will remain unchanged. In this article, we will understand what global variables are, how to use ... Read More

How can we overload a Python function?

Sarika Singh
Updated on 11-Apr-2025 14:32:32

4K+ Views

Function overloading means defining multiple functions with the same name but different arguments. This is a built-in feature in many programming languages like C++ or Java. However, in Python, function overloading is not directly supported, but we can achieve it using some techniques. Python uses a concept called dynamic typing, which allows us to define functions that can take different types and numbers of arguments. We cannot create multiple functions with the same name, but we can still achieve similar functionality using methods like default arguments, variable-length arguments, or special modules like functools.singledispatch. Using Default Arguments One way to overload ... Read More

How can we create recursive functions in Python?

Sarika Singh
Updated on 11-Apr-2025 14:30:18

4K+ Views

Recursion is a process where a function calls itself repeatedly with new input values, slowly moving toward a condition where it stops. This stopping condition is called the base case. It prevents the function from running endlessly and allows it to return a final result. In Python, we can create recursive functions by simply defining a function that calls itself. Every recursive function has two main components − Recursive Case: This is when the function calls itself to solve a smaller part of the problem. It keeps the process going. Base ... Read More

Advertisements