Python Programming Basics: Your Gateway to Coding

From Basics to Pro (Part 1)

Python, often referred to as a “batteries-included” language, is known for its simplicity and versatility. Whether you’re a seasoned developer or someone just starting on their coding journey, Python provides an excellent platform to grasp programming concepts. In this blog post, we’ll explore the fundamentals of Python programming. By the end, you’ll have a solid foundation to tackle more complex coding challenges and automation tasks.

Why Python?

Python is a high-level, interpreted language that is both easy to read and write. It’s not only a popular choice for web development but also a go-to language for system administrators, data scientists, and AI enthusiasts. Its ease of use and extensive libraries make it an excellent choice for various applications.

Installation and Setup

Before diving into Python programming, you’ll need to set up your development environment. Python is available for Windows, macOS, and Linux. Here are the steps to get started:

  1. Download Python: Visit the official Python website (https://www.python.org/downloads/) and download the latest version of Python for your operating system. Follow the installation instructions.
  2. Code Editor: You can use any code editor to write Python code. Popular choices include Visual Studio Code, PyCharm, and Jupyter Notebook.

An example of what you will learn to understand at the end of this basic course.

The last thing we do is step through this script.

Python
import psutil

def check_cpu_usage(threshold):
    cpu_percent = psutil.cpu_percent(interval=1)  # Check CPU usage over 1 second.
    
    if cpu_percent > threshold:
        print(f"CPU Usage is {cpu_percent}% and is higher than the threshold of {threshold}%.")
    else:
        print(f"CPU Usage is {cpu_percent}% and is within the threshold of {threshold}%.")

if __name__ == "__main__":
    threshold = 80  # Define your CPU usage threshold here (e.g., 80%).
    check_cpu_usage(threshold)

Your First Python Program

Let’s start with a simple Python program. Open your code editor, create a new file, and type the following:

Python
print("Hello, World!")

This is a traditional first program in any language. Save the file with a .py extension, for example, hello.py. To run the program, open your terminal or command prompt, navigate to the file’s directory, and execute:

Python
python3 hello.py

You should see “Hello, World!” printed to the console.

Variables and Data Types

Python supports various data types, including integers, floats, strings, lists, and dictionaries. You can declare variables like this:

Python
age = 30
name = "John"
height = 6.2

Python is dynamically typed, meaning you don’t have to specify a variable’s type explicitly; Python infers it for you.

Input and Output

To get user input, use the input() function:

Python
name = input("Enter your name: ")

For output, we’ve already seen the print() function in action.

Conditional Statements

Conditional statements are essential for decision-making in your programs. Here’s an example:

Python
age = 25
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

In this code, we use an if-else statement to determine if someone is an adult or a minor based on their age.

Loops

Python supports for and while loops for repetitive tasks. A for loop is used to iterate over a sequence, while a while loop continues as long as a condition is true. Here’s an example:

Python
for i in range(5):
    print(i)

This code will print numbers from 0 to 4.

Functions

Functions are reusable blocks of code. You can define your own functions or use built-in ones. Here’s a simple function:

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

You can call this function with a name as an argument, like greet("Alice").

These are the core concepts of Python programming. With these basics, you’re well on your way to becoming a proficient Python coder. Python’s simplicity and power make it an excellent choice for various applications, from web development to system administration. Stay curious and keep coding!

Let’s break down the example we started with

Python
import psutil

def check_cpu_usage(threshold):
    cpu_percent = psutil.cpu_percent(interval=1)  # Check CPU usage over 1 second.
    
    if cpu_percent > threshold:
        print(f"CPU Usage is {cpu_percent}% and is higher than the threshold of {threshold}%.")
    else:
        print(f"CPU Usage is {cpu_percent}% and is within the threshold of {threshold}%.")

if __name__ == "__main__":
    threshold = 80  # Define your CPU usage threshold here (e.g., 80%).
    check_cpu_usage(threshold)

First, make sure you have the psutil library installed. You can install it using pip if you haven’t already

Python
pip install psutil
  • We import the psutil library, which allows us to monitor system resources like CPU usage.
  • The check_cpu_usage function checks the CPU usage and compares it to the specified threshold. It uses the psutil.cpu_percent() function to get the CPU usage percentage over a 1-second interval.
  • The main part of the script calls the check_cpu_usage function with your defined threshold (e.g., 80%). You can change the threshold value to your desired level.

When you run this script, it will tell you if the CPU usage is higher or within the specified range. You can customize the threshold to fit your specific monitoring needs. This script can be a useful part of your sysadmin toolkit for keeping an eye on system performance.

For more in-depth Python tutorials, sysadmin tips, and practical examples, don’t forget to check out our YouTube channel. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *