In [1]:
!python --version
Let us define a function which returns single value.
In [2]:
def demoReturnOne(m):
return(m)
In [3]:
result = demoReturnOne(7)
In [4]:
print(result)
In [5]:
print(type(result))
Notice above the type of object returned, it is int.
In [6]:
def demoReturnTwo(m,n):
return(m,n)
In [7]:
result = demoReturnTwo(1,2)
In [8]:
result
Out[8]:
In [9]:
print(type(result))
Note the type 'tuple'.
we can also collect the result in two variables since function is returning a tuple of two elements.
In [10]:
r1, r2 = demoReturnTwo(1,2)
In [11]:
r1
Out[11]:
In [12]:
r2
Out[12]:
Let us define a function which returns list.
In [13]:
def listF(m,n):
return([m,n])
In [14]:
result = listF(1,2)
In [15]:
result
Out[15]:
In [16]:
print(type(result))
We can also assign data to two variables since list contains two elements.
In [17]:
a,b = listF(1,2)
In [18]:
a
Out[18]:
In [19]:
b
Out[19]:
Similarly we can extend the above concept to function with more than two variables.
In [20]:
def listF(m,n,k):
return([m,n,k])
In [21]:
result = listF(4,5,6)
In [22]:
result
Out[22]:
In [23]:
a,b,c = listF(4,5,6)
In [24]:
a
Out[24]:
In [25]:
b
Out[25]:
In [26]:
c
Out[26]:
Related Notebooks
- Pandas group by multiple custom aggregate function on multiple columns
- Five Ways To Remove Characters From A String In Python
- PySpark Replace Values In DataFrames
- Remove An Item From A List In Python Using Clear Pop Remove And Del
- How to Plot a Histogram in Python
- A Study of the TextRank Algorithm in Python
- What is LeakyReLU Activation Function
- How To Take String Input From Command Line In Python
- How To Take Integer Input From Command Line In Python