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.