Python functions are the blocks of code that perform a specific task. Functions provide the facility of modular programming. You can code the function once and use that function many times. You can use the Python-predefined functions as well as user-defined functions. In Ubuntu 22.04, you can execute these functions using the terminal.
In this particular write-up, we will explain how you can a Python function in the terminal.
How do I Run a Python function in the Terminal?
There are two types of functions in any programming language. The functions can be predefined which are part of the programming language, and the user-defined functions which are programmed by the user. In this section, I’ll explain how you can execute a function in the terminal of the Ubuntu 22.04. Use the following steps to execute the Python function in the Terminal.
Step 1: Create a Python Script
If you want to execute a function, it must be created first. We will create a Python script and then write the Python code. Python scripts are the files that contain the Python code. Use the following command to create a Python script:
$ sudo nano "<scritptName.py>"
The command will create a Python script, and open it in the nano text editor:

Step 2: Write the Code for the Function
Write the code for the function and save the file. You can create the function by using the following syntax:
def name_of_function():
Statements of function
--------------------------
---------------------------
#call the function
name_of_function()
- Create the function by using the keyword “def”
- Then write the name of the function.
- You can write the statements of your program.
- Call the function specifying its name.
- Save the file by pressing “Ctrl + s”.
- Press “Ctrl + x” to exit from the editor.
A sample code file is shown below in the diagram:

Step 3: Execute the Function in the Terminal
Now execute the following command to execute the function in the terminal:
$ python3 "<name of your python_script>"
In my case, the output is as follows:

Step 4: Using the Python Shell
You can also use the Python shell to execute the Python functions. Launch the Python shell by typing the “python3” in the terminal and pressing the “Enter” key. Use the following syntax to execute the function in the Python shell:
$ python 3
>>> def function_name():
... statements of the function
...
>>> function_name()
The sample function execution is shown in the diagram:


Step 5: Executing the Pre-Defined Function
The pre-defined functions are part of the language. You can use them by specifying the name of the library in which the required function is defined. You can use math and stat libraries to execute the math or stat functions.
Use the following syntax to execute the pre-defined function using the Python shell:
$ python3
>>> import math
>>> math.name_of_function()
>>> print the results
- Start the shell by typing python3
- Import the library
- Write the library name along with the name of the function
- Then print the results
The sample code and output are shown below in the diagram:

This is how you can run a Python function in the terminal of Ubuntu 22.04.
Conclusion
To run a Python function in the terminal, you can use the terminal to execute the Python script with the python3 preprocessor and you can also use the Python shell. In this article, I have explained how you can run a Python function in the terminal of Ubuntu 22.04.
Frequently Asked Questions
How can I create a Python script in Ubuntu 22.04 terminal?
You can create a Python script in Ubuntu 22.04 terminal by using the command: $ sudo nano '<scriptName.py>'. This command will create a Python script and open it in the nano text editor.
What is the syntax for defining a Python function?
In Python, you define a function using the 'def' keyword followed by the name of the function. For example: def name_of_function(): Statements of the function.
How do I call a Python function in a script?
To call a Python function in a script, you simply use the name of the function followed by parentheses. For example: name_of_function().
What are the benefits of using user-defined functions in Python?
User-defined functions in Python allow for modular programming, enabling you to write code once and reuse it multiple times. They enhance code readability and reusability.
Can I use predefined functions in Python scripts?
Yes, you can use predefined functions in Python scripts. Predefined functions are built-in functions provided by Python that perform specific tasks.
How do I save a Python script after writing the function code?
To save a Python script after writing the function code, press 'Ctrl + s' in the text editor. This will save the changes you made to the file.
What is the purpose of the 'def' keyword in Python function creation?
The 'def' keyword is used to define a function in Python. It signifies the beginning of a function block and is followed by the name of the function being defined.
How do I exit the text editor after saving the Python script?
To exit the text editor after saving the Python script, press 'Ctrl + x'. This will close the editor and return you to the terminal.