• +1-617-874-1011 (US)
  • +44-117-230-1145 (UK)
Live Chat
Follow Us:

Functions in Python Help

In this tutorial we shall be looking on what are functions, how can we write a function in python and what are the uses of functions.

Functions are a collection of different operations that are to be performed on an object, or anything that you want to do. Instead of writing it again and again you can just write it in a function and call that function whenever you want to execute that task.

Let us see how we can write a function in python.

def function_name():

BODY OF THE FUNCTION

One important thing to remember is that there is no use of any kind of braces or parentheses to define the body of the function.

Here you need to work according to the indentation. In order to end the function, you just need to come out of its indentation.

Let us look at an example to get a clear understanding of the topic.

def one() :
print('-------------WELCOME------------')
a = input(print('Enter your Name:'))
b = input(print('Enter Your roll number'))
c = input(print('Enter Examination name'))
d = input(print('Enter Date of Examination'))
f1 = open('efile','w+')
f1.write(a+'\n')
f1.write(b+'\n')
f1.write(c+'\n')
f1.write(d+'\n')
f1.close()
f1 = open('efile','r')
print('Name Is :')
print(f1.readline())
print('Roll No Is:')
print(f1.readline())
print('Name Of The Examination Is:')
print(f1.readline())
print('Date Of Examination Is:')
print(f1.read())
f1.close()

Here, we are defining a function one () which performs some operations.

What if we want to call this function?

In Python, we just need to come out of the indentation of the function and call it.

For this example, we will be calling it as:

one ()

As the interpreter encounters the function call, it will jump to the function definition and execute it.

We can create a function which can take values as parameters and will execute the operations performed with that parameter
Let us take and example and understand it
def func(abc):
if int(abc):

For this example, we will be calling it as:

one ()

As the interpreter encounters the function call, it will jump to the function definition and execute it.

We can create a function which can take values as parameters and will execute the operations performed with that parameter

Let us take and example and understand it

def fff(b):
if b== int(b):
print('The Input Is An Integer Number')
elif b == float(b):
print('The Input Is Decimal Number')
fff(4.5)

Output: The Input Is Decimal Number

In the above example, we have a function fff() which takes a parameter and checks its value whether it is an integer of a floating point number.

Let us look at some of the predefined functions that are used in Python.

A pre-defined function is a function in which we just need to use it, we do not need to write anything inside it, rather it is a part of some package that you are importing in order to use that function. If you will get into that package, there you will find the definition of that function. Python is full of these pre- defined functions there are a lot of functions that we can use and get the desired output quite easily.

We can also give a value to a variable that is, giving it a default value. Incase the user is not giving the value to that variable or the user wants it to hold a value, then he can pass assign a value to that variable. let us look at an example to get a better understanding.

def printi(b, name = "william"):
if b== int(b):
print('The Input Is An Integer Number',", Name is", name)
elif b == float(b):
print('The Input Is Decimal Number',", Name is :",name)
printi(619)

Output: The Input Is An Integer Number , Name is william

In the above example, we have a function called printi() which has a variable and a name whose value we have already given as william. It will check the value of b and then print accordingly.

What if you want a variable to hold a value that remains with it all the time and cannot be changed?

We can make a variable as a global variable. A global variable is declared before creating a function. It will hold that value which is assigned to it till the scope of the program. Let us look at an example and see its functioning.

total = 5
def func(a,b):
total = a+b
print('Sum is:', total)
func(6,7)
print('Value of total is:',total)

In the above function, we are takin the input of two numbers and printing their sum. The sum is stored in a variable named “total”. After calling the function, we are printing the value of total and we can see that the value of total is not a+b, rather it is the value that we assigned to it before defining the function that is, 5. This is what a global variable behaves like.

The variable that we are using inside a function is called a local variable. It is so called because it holds the value that we assign to till the scope of the function is not over (till it is inside the function body, outside it we can again use that variable and with some other value).

Let us look at an example to understand it better.

def type():
g = 34
j = 42
print('Subtraction of g from j gives:',j-g)
def reg():
g = 50
j = 10
print('Division of g by j gives:',g/j)
type()
reg()

Output: Subtraction of g from j gives: 8

Division of g by j gives: 5.0

In the above code we are using two functions that are using the same variables that are being used to perform different operations. As you may note that the use of the same variable gives different results. This is because the scope of those variable ended in one function that is, g was holding the value 34 and j was holding the value 42 which resulted in 8 as the result. But in the next function, reg(), we assigned them some other value, that are g as 50 and j as 10 so we get the result as 5.0.

A thing worth noting is that that if you will try to print the value of any of the variables that is, g and j after the function call, then you will encounter an error saying “NameError: g is not defined”.

What if you want to a function to take multiple input but you are not sure about the number of inputs and you want to create a dynamic function that can work on variable inputs?

In Python we can do this using “*” (asterisk) by using it as *var_args_tuple”.

Let us look ant an example to understand it.

def type(par,*partuple):
print('value is:',par)
for par in partuple:
print('value is:',par)
type(30)
type(70,80,'Rahul',100.54)

Output: value is: 30

value is: 70

value is: 80

value is: Rahul

value is: 100.54

In the above program, we are printing the values of the parameters that we are sending to our function type(). The “*partuple” is used for the variable inputs that are dynamic. The use of tuple gives us the liberty to enter different types of input variables like character type, float type etc. that we used in the above function. It is a very useful function for dynamic programming. This function provides us flexibility to do whatever we want to do.

We can also create anonymous functions in Python by using the keyword lambda which used for creating small functions.

The lambda functions can take n number of arguments but are restricted to generate a single output.

We can not call a lambda function (anonymous functions) the way we call a regular function. These functions require an expression for their execution.

The lambda functions have their own namespace and they cannot access the variables other than the ones which are in in its parameters list or the ones which are in the global namespace.

Let us look at an example to get a better understanding.

o = lambda s1,s2: s1**s2;
print('The value of s1 to the power of s2 is ', o(5,4))
print('The value of s1 to the power of s2 is ', o(2,6))

Output: The value of s1 to the power of s2 is 625

The value of s1 to the power of s2 is 64

In the above function, we are using two input variables that is, s1 and s2. And performing the function s1 to the power of s2. Then we call the function as it is shown in the code.

This is it for this tutorial, I hope you guys learned something from it. Keep practicing them and try to experiment with the functions as they are very volatile, and practicing will make your concepts clearer.

See you in the next tutorial!