Python is a popular programming language that is widely used for scripting, automation, data analysis, and web development. Python scripting is a skill that every DevOps Engineer and Site Reliability Engineer (SRE) should master, as it can help you automate routine tasks, simplify complex operations, and speed up your workflow. This article provides an overview of Python scripting, and covering the basics. This article will help you understand the fundamental concepts necessary for using Python as a scripting language to assist in writing automation scripts.

What is the Python programming language?

No getting started introduction to Python is complete without a description of what Python is, and who created it!

Python is a high-level, interpreted programming language that was first released in 1991 by Guido van Rossum. It is known for its simplicity, readability, and ease of use, making it a popular language for beginners and experts alike. Python is open-source and freely available, which means anyone can use, modify, and distribute the language without paying any licensing fees.

Python is a versatile language that can be used for a wide range of applications, including web development, scientific computing, data analysis, artificial intelligence, machine learning, automation, and more. It has a large standard library and a vast ecosystem of third-party modules and packages, making it a powerful language for building complex applications.

Some of the key features of Python include:

  • Easy-to-learn syntax
    Python’s syntax is designed to be simple and intuitive, making it easy for beginners to learn and write code.
  • Interpreted
    Python code is executed by an interpreter, which means there is no need for compilation before running the code.
  • Dynamically typed
    Python is a dynamically typed language, which means that variables do not need to be declared with a specific data type before use.
  • Object-oriented
    Python supports object-oriented programming, which allows developers to organize code into reusable and modular units.
  • Portable
    Python can run on a wide variety of platforms, including Windows, macOS, Linux, and more.

Overall, Python is a powerful and flexible programming language that has gained widespread popularity in recent years, making it an excellent choice for a wide range of applications.

Installing Python

Installing Python on Windows, macOS, and Linux is a straightforward process. Here are the steps for each operating system:

Install Python on Windows

  1. Go to the official Python website at https://www.python.org/downloads/.
  2. Click on the “Download Python” button for the latest stable version of Python.
  3. Scroll down to the “Files” section and select the appropriate version of Python for your Windows system.
    If you are unsure, select the “Windows x86-64 executable installer” for a 64-bit Windows system or the “Windows x86 executable installer” for a 32-bit Windows system.
  4. Run the installer and follow the instructions to complete the installation.
    By default, Python will be installed in the C:\Python[version] directory, where [version] is the version number of Python you installed.

Install Python on macOS

  1. Go to the official Python website at https://www.python.org/downloads/.
  2. Click on the “Download Python” button for the latest stable version of Python.
  3. Scroll down to the “Files” section and select the appropriate version of Python for your macOS system.
    If you are unsure, select the “macOS 64-bit installer” for the latest version of macOS.
  4. Run the installer and follow the instructions to complete the installation.
    By default, Python will be installed in the /Library/Frameworks/Python.framework/Versions/[version]/ directory, where [version] is the version number of Python you installed.

Install Python on Linux

  1. Open the terminal and run the following command to update the package list: sudo apt-get update (for Ubuntu or Debian) or sudo yum update (for CentOS or Fedora).
  2. Run the following command to install Python: sudo apt-get install python3 (for Ubuntu or Debian) or sudo yum install python3 (for CentOS or Fedora).
  3. Verify that Python is installed by running the following command: python3 --version
    The terminal should output the version of Python you installed.

Note: If you are using a different Linux distribution or version, the installation steps may vary. Check your distribution’s documentation for specific instructions.

Python Language Syntax Basics

Python is a high-level programming language with a simple and intuitive syntax that makes it easy to learn for beginners.

The following are some of the basic syntax elements of the Python programming language:

Code Comments

Comments are used to add notes to your code that explain what it does or how it works. They are not executed as part of the program. In Python, comments start with the hash symbol (#).

# This is a comment in Python

Variables

Variables are used to store values that can be used later in the program. In Python, you can assign a value to a variable using the equal sign (=).

x = 10      # Assign the value 10 to the variable x
y = "hello" # Assign the string "hello" to the variable y

Data Types

Python has several built-in data types that you can use to store and manipulate values. Some of the basic data types include:

  • Integers: whole numbers (e.g. 1, 2, 3)
  • Floats: numbers with a decimal point (e.g. 3.14, 2.0)
  • Strings: sequences of characters (e.g. “hello”, ‘world’)
  • Booleans: True or False values
x = 10        # integer
y = 3.14      # float
z = "hello"   # string
a = True      # boolean

Operators

Operators are used to perform operations on values and variables. Some of the basic operators in Python include:

  • Arithmetic operators: +, -, *, /, %, ** (addition, subtraction, multiplication, division, modulo, exponentiation)
  • Comparison operators: ==, !=, <, >, <=, >= (equal to, not equal to, less than, greater than, less than or equal to, greater than or equal to)
  • Logical operators: and, or, not (logical and, logical or, logical not)
x = 10
y = 3
z = x + y    # addition
a = x > y    # comparison
b = (x > y) and (y < 5)   # logical

Control Structures

Control structures are used to control the flow of a program. Some of the basic control structures in Python include:

  • Conditional statements: if, elif, else (execute code based on a condition)
  • Loops: for, while (repeat code multiple times)
# Example of a conditional statement
x = 10
if x > 5:
    print("x is greater than 5")
elif x < 5:
    print("x is less than 5")
else:
    print("x is equal to 5")

# Example of a loop
for i in range(5):
    print(i)

Write your first Python Script

Python scripts can be written with any editor. Visual Studio Code is a particularly popular code editor used, but any text editor can be used. When writing Python code, the Python code files will be saved with a .py file extension.

Let’s write your very first Python script!

Copy the following Python code for a “Hello world!” script and save it with the file name of hello.py:

print("Hello world!")

With the hello.py script saved to your machine, navigate your terminal to the directory where the file is saved, then use the following command to execute the script:

python hello.py

When executed, the script will output a Hello world! message to the console.

You have successfully written and executed your very first Python script!

Reading and Writing to Files

Reading and writing to files is a common task in Python programming. You may find the need to read and/or write to files in the local file system.

Here’s a brief explanation of the basics of file I/O in Python, along with some code examples:

Opening a file

To read or write to a file, you first need to open it using the built-in open() function. The open() function takes two arguments: the name of the file you want to open, and a string that specifies the mode in which you want to open the file (read, write, append, etc.).

Here’s an example:

# Open a file in read mode
file = open('example.txt', 'r')

Reading from a file

Once you’ve opened a file, you can read its contents using the read() method. This method reads the entire contents of the file and returns them as a string.

Here’s an example:

# Read the contents of the file
content = file.read()
print(content)

Writing to a file

To write to a file, you need to open it in write mode using the open() function. You can then write to the file using the write() method. This method takes a string as an argument and writes it to the file.

Here’s an example:

# Open a file in write mode
file = open('example.txt', 'w')

# Write to the file
file.write('This is a test.\n')
file.write('This is another test.\n')

# Close the file
file.close()

Note that when you open a file in write mode, it will overwrite the contents of the file. If you want to add to the contents of the file, you can open it in append mode using the 'a' mode:

# Open a file in append mode
file = open('example.txt', 'a')

# Add to the file
file.write('This is yet another test.\n')

# Close the file
file.close()

Closing a file

After you’re finished reading from or writing to a file, it’s important to close the file using the close() method. This ensures that any data you’ve written to the file is saved and that the file is available for other processes to access.

Here’s an example:

# Open a file in read mode
file = open('example.txt', 'r')

# Read the contents of the file
content = file.read()
print(content)

# Close the file
file.close()

This covers the basics of working with files using Python. Always remember to close files when finished working with them, and be sure to handle any exceptions that might occur when working with files.

What are Python Libraries?

In Python, a library is a collection of pre-written code that can be used to perform common tasks. Python libraries contain functions, classes, and other objects that can be used to add new functionality to your Python programs without having to write the code from scratch.

Python libraries are widely available and cover a broad range of domains, including scientific computing, data analysis, web development, machine learning, and more. Some popular Python libraries include NumPy, pandas, Matplotlib, Flask, Django, TensorFlow, and PyTorch.

Python libraries can be installed using a package manager like pip. Once installed, libraries can be imported into your Python code using the import statement. Once imported, you can use the functions and other objects provided by the library in your code.

What is Pip?

PIP is the default package manager for Python. It is used to install and manage packages, which are collections of Python modules that provide additional functionality. PIP allows users to easily install, uninstall, and upgrade Python packages from the Python Package Index (PyPI), a repository of over 200,000 packages.

To install the azure-storage-blob library using pip, you can follow these steps:

  1. Open a command prompt or terminal window.
  2. Use the following command to install the library:
pip install azure-storage-blob

This command will download and install the latest version of the azure-storage-blob library and any dependencies that it requires. Once the library is installed, you can import it in your Python code using the import statement.

Pip is also used for managing Python packages, and it makes it easy to install and remove packages.

Here are the most common pip commands:

  • pip install [package]: Installs the specified package
  • pip uninstall [package]: Uninstalls the specified package
  • pip list: Lists all installed packages
  • pip freeze: Lists all installed packages with their versions

Use a Python library in your script

Here is an example of how to import the azure-storage-blob library and use it to upload a file to a Microsoft Azure Blob Storage container:

# Import the necessary library
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

# Define the connection string and container name
connect_str = "<your_connection_string>"
container_name = "<your_container_name>"

# Create a BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

# Create a ContainerClient object
container_client = blob_service_client.get_container_client(container_name)

# Upload a file to the container
file_path = "<your_file_path>"
blob_name = "<your_blob_name>"
with open(file_path, "rb") as data:
    container_client.upload_blob(name=blob_name, data=data)

In this example, we first import the necessary classes from the azure.storage.blob library. Then, we define the connection string and container name for our Azure Blob Storage account. We create a BlobServiceClient object using the connection string, and a ContainerClient object using the container name.

Finally, we upload a file to the container by opening the file using the with statement and calling the upload_blob method of the ContainerClient object. The name parameter specifies the name of the blob, and the data parameter specifies the contents of the blob.

Define and Use Python Functions

In Python, functions are defined using the def keyword, followed by the function name, and the parameters in parentheses. The function body is indented below the function definition. Here’s a basic example of a Python function:

def greet(name):
    print("Hello, " + name + "!")

greet("John")

In this example, the greet() function is defined with a single parameter name. The function body simply prints a greeting message with the provided name.

To use the function, we simply call it with an argument for the name parameter. In this case, we call greet() with the argument "John", which will print the message "Hello, John!".

Functions can also return values using the return statement.

Here’s an example:

def add_numbers(a, b):
    return a + b

result = add_numbers(3, 4)
print(result)

In this example, the add_numbers() function takes two parameters a and b, adds them together, and returns the result using the return statement. The returned value is then assigned to the variable result, which is printed to the console.

Functions can be very useful in Python for breaking up large programs into smaller, more manageable pieces of code. They can also be reused in different parts of a program or even in other programs altogether.

Error Handling in Python

In Python, errors and exceptions can be handled using try/except blocks. When an exception is thrown, the program can catch it using a try/except block, which allows the program to handle the exception gracefully and continue running.

Exceptions can be thrown in several ways, such as by raising an exception using the raise statement, by encountering an error in the execution of a statement, or by a module or function raising an exception.

Here’s an example of how to catch an exception in Python using a try/except block:

try:
    # some code that may raise an exception
    x = 1 / 0
except ZeroDivisionError:
    # handle the exception
    print("Cannot divide by zero")

In this example, the code in the try block attempts to divide the number 1 by 0, which will raise a ZeroDivisionError. The program catches the exception using an except block, which prints an error message to the console.

Here’s another example of how to catch multiple types of exceptions using a try/except block:

try:
    # some code that may raise an exception
    my_list = [1, 2, 3]
    print(my_list[3])
except IndexError:
    # handle the IndexError
    print("Index out of range")
except Exception as e:
    # handle any other exceptions
    print("An error occurred:", e)

In this example, the program attempts to access an element of the my_list list that is out of range, which will raise an IndexError. The program catches the IndexError using the first except block, and catches any other exceptions using the second except block.

When an exception is raised, Python creates an exception object that contains information about the exception, such as its type and message. The program can access this information using the as keyword, as shown in the second except block in the previous example.

In addition to handling exceptions that are raised by call to Python libraries, the raise statement can also be used to throw exceptions too. This can be useful to better handle exceptions and other errors that occur during the execution of the Python code.

Here’s an example of explicitly raising an exception within a function and handling it too:

def divide(x, y):
    if y == 0:
        raise ZeroDivisionError("Cannot divide by zero")
    return x / y

try:
    result = divide(10, 0)
except ZeroDivisionError as e:
    print(e)
else:
    print(result)

In this example, the divide() function is defined to perform division between two numbers, but it raises a ZeroDivisionError if the second number is 0.

In the try block, the divide() function is called with arguments 10 and 0. Since the second argument is 0, the function raises an exception. The except block catches the ZeroDivisionError, prints the error message, and the program continues to execute.

The else block is executed if there is no exception, which prints the result of the division. Since an exception was raised in the try block, the else block is not executed in this case.

The output of this block will be:

Cannot divide by zero

In general, it’s a good practice to be as specific as possible when catching exceptions, and to only catch the exceptions that you know how to handle. This helps to avoid masking bugs or errors in the program.

Conclusion

Python scripting is a versatile and powerful skill that can help you achieve various goals as a DevOps Engineer or Site Reliability Engineer (SRE). This article covers several of the basic concepts necessary for using Python as a scripting language. There are more aspects to the Python language to learn, but the basics in this article should help you get started using Python for writing automation scripts.

Happy scripting!

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
Microsoft MVP HashiCorp Ambassador

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading