Open In App

Assign Function to a Variable in Python

Last Updated : 17 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to assign a function to a variable in Python. In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. 

Implementation

Simply assign a function to the desired variable but without () i.e. just with the name of the function. If the variable is assigned with function along with the brackets (), None will be returned.

Syntax:

def func():
{
..
}

var=func

var()
var()

Example:

Python3




def a():
  print("GFG")
   
# assigning function to a variable
var=a
 
# calling the variable
var()


Output: 

GFG

The following programs will help you understand better:

Example 1: 

Python3




# defined function
x = 123
 
def sum():
    x = 98
    print(x)
    print(globals()['x'])
 
 
# drivercode
print(x)
 
# assigning function
z = sum
 
# invoke function
z()
z()


Output:

123
98
123
98
123

Example 2: parameterized function

Python3




# function defined
def even_num(a):
    if a % 2 == 0:
        print("even number")
    else:
        print("odd number")
 
 
# drivercode
# assigning function
z = even_num
 
# invoke function with argument
z(67)
z(10)
z(7)


Output:

odd number
even number
odd number

Example 3:

Python3




# function defined
def multiply_num(a):
    b = 40
    r = a*b
    return r
 
 
# drivercode
# assigning function
z = multiply_num
 
# invoke function
print(z(6))
print(z(10))
print(z(100))


Output:

240
400
4000


Previous Article
Next Article

Similar Reads

Python | Assign multiple variables with list values
We generally come through the task of getting certain index values and assigning variables out of them. The general approach we follow is to extract each list element by its index and then assign it to variables. This approach requires more line of code. Let's discuss certain ways to do this task in compact manner to improve readability. Method #1
4 min read
Python | Assign value to unique number in list
We can assign all the numbers in a list a unique value that upon repetition it retains that value retained to it. This is a very common problem that is faced in web development when playing with id's. Let's discuss certain ways in which this problem can be solved. Method #1: Using enumerate() + list comprehension List comprehension can be used to p
7 min read
Python | Assign ids to each unique value in a list
Sometimes, while working in web development domain or in competitive programming, we require to assign a unique id to each of the different values to track its occurrence for the count or any other required use case. Let's discuss certain ways in which this task can be performed. Method #1: Using defaultdict + lambda + list comprehension The combin
5 min read
Python | Assign range of elements to List
Assigning elements to list is a common problem and many varieties of it have been discussed in the previous articles. This particular article discusses the insertion of range of elements in the list. Let's discuss certain ways in which this problem can be solved. Method #1 : Using extend() This can be solved using the extend function in which the i
5 min read
Python - Assign values to initialized dictionary keys
Sometimes, while working with python dictionaries, we can have a problem in which we need to initialize dictionary keys with values. We save a mesh of keys to be initialized. This usually happens during web development while working with JSON data. Lets discuss certain ways in which this task can be performed. Method #1 : Using dict() + zip() The c
5 min read
Python - Assign list items to Dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to assign list elements as a new key in dictionary. This task can occur in web development domain. Lets discuss certain ways in which this task can be performed. Method #1 : Using zip() + loop The combination of above functions can be used to solve this proble
10 min read
Python - Assign K to Non Max-Min elements in Tuple
Sometimes, while working with Python data, we can have a problem in which we need to assign particular value as per certain condition. One of condition can be non-max, min element. This kind of task can occur in many application of day-day programming and competitive programming. Lets discuss certain ways in which this task can be performed. Input
7 min read
Python - Assign Reversed Values in Dictionary
Given a dictionary, assign each key, values after reverting the values of dictionary. Input : {1 : 4, 2 : 5, 3 : 6} Output : {1 : 6, 2 : 5, 3 : 4} Explanation : Value order changed, 4, 5, 6 to 6, 5, 4. Input : {1 : 5, 2 : 5, 3 : 5} Output : {1 : 5, 2 : 5, 3 : 5} Explanation : Same values, no visible change. Method #1 : Using values() + loop + rever
4 min read
Python - Assign pair elements from Tuple Lists
Given a tuple list, assign each element, its pair elements from other similar pairs. Input : test_list = [(5, 3), (7, 5), (8, 4)] Output : {5: [3], 7: [5], 8: [4], 4: []} Explanation : 1st elements are paired with respective 2nd elements from all tuples. Input : test_list = [(5, 3)] Output : {5: [3]} Explanation : Only one tuples, 5 paired with 3.
7 min read
Python - Assign values to Values List
Given 2 dictionaries, assign values to value list elements mapping from dictionary 2. Input : test_dict = {'Gfg' : [3, 6], 'best' :[9]}, look_dict = {3 : [1, 5], 6 : "Best", 9 : 12} Output : {'Gfg': {3: [1, 5], 6: 'Best'}, 'best': {9: 12}} Explanation : 3 is replaced by key 3 and value [1, 5] and so on. Input : test_dict = {'Gfg' : [3, 6]}, look_di
6 min read
Practice Tags :