Found 10533 Articles for Python

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