In this Python tutorial, we will learn to define and execute basic Python functions to define a block of code and only run when it is called within the Python script. In this Python tutorial I am running Python IDLE (Python GUI) version 2.7.

Within the Python function, one will be able to pass data, which are known as parameters and the function will return the result as data.

Define the Python first_function()

As one can see from the below, the below is a function that is called first_function() with a print statement below.

1

2

def first_function():

print("Welcome to R-ALGO Engineering Big Data!")

Run the Python first_function()

I have executed (Run button menu in Python IDLE GUI) the saved script (python_functions.py) and as one can see, there is no output?

Now ask yourself the below questions:

Why did I execute the script and there are no errors?

What did we not do to call the script?

We must call the function in the script to provide the output of the given function.

Call the Python first_function()

Now let’s Run the script again and see the output with the below update to the script.

1

2

3

4

5

def first_function():

print("Welcome to R-ALGO Engineering Big Data!")

#calling first_function()

first_function()

As one can see from the above, the first function() was executed and returned the print statement, “Welcome to R-ALGO Engineering Big Data!”.

Call a Python Function Within Another Python Function

In addition to calling a function, one can call a function from another function in the script as shown below.

1

2

3

4

5

6

7

8

def first_function():

print("Welcome to R-ALGO Engineering Big Data!")

def call_first_function():

print("Calling first_function()")

first_function()

call_first_function()

I hope this Python tutorial helped to provide some basic knowledge on creating and executing Python functions.