How to Create a Python Script in Linux

Python is a modern high-level programming language which is open source and is designed for general use, capable of working in many domains. It is simple and easy to use and is highly readable, so it is considered a good language for beginners. Python is packed with useful built-in features, but it can also be extended through the use of modules. Python is dynamically-typed, platform-independent, and supports Object-Oriented Programming (OOP) techniques.

How to Create a Python Script in Linux

How to Create a Python Script in Linux

How to Install Python

First of all, you have to install python language in your system by using below command,
sudo apt update && sudo apt upgrade
sudo apt install python3 

You can also download it from the official Python website and follow the installation instructions.

How to Install Pip

After Python is installed, it is possible to use the Python pip package manager to download and install new modules and packages. Use this command below to install pip.
sudo apt install python3-pip

Now, install markdown library to convert the code in to HTML by this command.
pip install markdown

How to write  Python Script

File: sqrt_multiply.py

import math
import sys

MULTIPLIER = 5

def square_and_multiply(num):
    return math.sqrt(num) * MULTIPLIER

user_val = input("Enter a positive numerical value: ")
try:
    user_val = float(user_val)
except:
    sys.exit("The input is not a number")
if (user_val < 0):
    sys.exit("Negative numbers are not allowed")
res = square_and_multiply(user_val)
print("The result after taking the square root of the number and multiplying it is:", res)


File: program.py

def main():
# Main body of code. Other functions and class methods are called from main.

if __name__ == "__main__":
    main()

File: function_demo.py

def sample_function(arg1, arg2):
    # Body of function
    return result

value = sample_function(param1, param2)

File: math_import.py

import math
result = math.sqrt(16)
print(result)

How to Run a Python Script

python function_demo.py
python math_import.py
Previous Post Next Post