Blog

  • Setting up the development environment

    This entry is part 2 of 2 in the series Commodities

    In this post I will describe setting up a development environment on a Mac machine. If you are following along and are using Windows I will indicate any differences to the procedure.

    1. Creating a Directory
    2. Creating a Virtual Environment
    3. Add some Python Packages to the Python Virtual Environment
    4. Open Project in VS Code
    5. Initialise Git

    Creating a Directory

    I suggest you allocate yourself a directory somewhere on your computer (Mac, Windows, Linux) and then create a sub-directory within for each project your embark on.

    To prevent problems with some packages, I suggest you avoid directory names containing spaces.

    On my Mac I have a directory on my desktop called Python and within that I have created a sub-directory called commodities.

    (base) peter@Peters-MacBook-Pro-3 commodities % pwd
    /Users/peter/Desktop/Python/commodities
    (base) peter@Peters-MacBook-Pro-3 commodities % 

    Creating a Virtual Environment

    I will be using the ta-lib package which sometimes won’t install in pip created virtual environments so I am going to use conda to create my environment instead.

    Add some Python Packages

    Ta-Lib

    (commodities) peter@Peters-MacBook-Pro-3 commodities % conda install conda-forge::ta-lib
  • Introduction to the Commodity Futures Project

    This entry is part 1 of 2 in the series Commodities

    In this series of posts, I am going to describe the full setup, design and operation of a Python Django application to acquire and analyse commodity futures with the aim of finding a winning strategy for predicting a [pretend] winning strategy.

    Note that this is just for a bit of fun and to demonstrate the power of Python.

  • Exploring the Python os Module: A gateway to the Operating System

    Python is widely celebrated for its simplicity and power, and one of the key reasons behind its versatility is the vast collection of built-in modules. Among these, the os module stands out as a foundational tool for interacting with the operating system. Whether you’re managing files, navigating directories, or accessing system variables, the os module makes it easy to perform OS-level tasks directly from Python.

    What is the os module?

    The os module provides a way of using operating system-dependent functionality like reading or writing to the file system, modifying environment variables, or working with processes. It is a part of Python’s standard library, meaning you don’t need to install anything extra—just import it and you’re ready to go:

    Python
    import os

    Common use cases

    • Navigating the file system
    • Listing files and directories
    • Creating and removing directories
    • File manipulation
    • Working with paths
    • Environment variables

    Why use the os module?

    The os module abstracts away platform-specific details, allowing your scripts to run across Linux, Windows, and macOS without change. It helps automate workflows, manage resources, and build more powerful, adaptable applications.

    Files and Directory Operations

    Python
    os.getcwd()                  # Get current working directory
    os.chdir(path)               # Change current working directory
    os.listdir(path='.')         # List files and dirs
    os.mkdir(path, mode=0o777)   # Create directory
    os.makedirs(path, exist_ok=False)  # Create nested directories
    os.rmdir(path)               # Remove empty directory
    os.removedirs(path)          # Remove nested directories
    os.remove(path)              # Delete a file
    os.rename(src, dst)          # Rename/move file or dir
    os.replace(src, dst)         # Rename/move, overwrite if needed
    os.stat(path)                # Get file metadata
    os.scandir(path='.')         # Iterator of DirEntry objects
    os.walk(top)                 # Recursively list files/dirs

    Path Manipulation (os.path)

    Python
    os.path.join(a, b, ...)      # Join paths safely
    os.path.abspath(path)        # Absolute path
    os.path.basename(path)       # File name from path
    os.path.dirname(path)        # Directory from path
    os.path.exists(path)         # Path exists?
    os.path.isfile(path)         # Is file?
    os.path.isdir(path)          # Is directory?
    os.path.getsize(path)        # File size in bytes
    os.path.split(path)          # (dir, file)
    os.path.splitext(path)       # (filename, extension)

    Environment Variables

    Python
    os.environ                   # Dict-like env vars
    os.environ.get(key, default) # Get env var
    os.environ['VAR'] = value    # Set env var
    os.putenv(key, value)        # Set env var (less common)
    os.unsetenv(key)             # Remove env var

    Process Management

    Python
    os.system(command)           # Run shell command
    os.startfile(path)           # Open file/app (Windows)
    os.execv(path, args)         # Replace current process
    os.fork()                    # Create process (Unix)
    os.spawn*()                  # Start process (multiple variants)
    os.kill(pid, sig)            # Send signal to process
    os.getpid()                  # Current process ID
    os.getppid()                 # Parent process ID

    File Descriptor Ops (low-level)

    Python
    os.open(path, flags, mode=0o777) # Open file descriptor
    os.read(fd, n)                   # Read n bytes
    os.write(fd, b'some bytes')      # Write bytes
    os.close(fd)                     # Close file descriptor

    OS Info & Constants

    Python
    os.name               # 'posix', 'nt', 'java'
    os.sep                # Path separator ('/' or '\\')
    os.altsep             # Alt path sep (Windows)
    os.extsep             # Extension separator '.'
    os.pathsep            # PATH separator
    os.linesep            # Newline character(s)
    os.devnull            # Null device path
    os.cpu_count()        # Number of CPUs

    Miscellaneous

    Python
    os.urandom(n)         # n random bytes
    os.getlogin()         # Logged-in username
    os.getuid(), os.getgid()   # User/group ID (Unix)
    os.chmod(path, mode)  # Change permissions
    os.access(path, mode) # Check permissions
  • Adding Emojis to a Python print statement

    The icons in the print() statements — like 🎉, ✅, 🚫 — are Unicode emojis. Python (especially Python 3) supports printing Unicode characters, including emojis, directly to the console — as long as the terminal or console supports Unicode (which most modern terminals do).

    🔍 Here’s how it works:

    Python
    print("🎉 Welcome to the Number Guessing Game!")

    You’re actually inserting a Unicode emoji character (🎉 = U+1F389) as part of the string. Python sees it just like any other string character and prints it.

    ✅ No special library needed

    You don’t need to import any emoji or Unicode library — these are just standard Unicode characters inside your strings.

    ⚠️ Terminal Support

    While this works out-of-the-box on:

    • macOS Terminal
    • Linux terminal
    • Windows Terminal / PowerShell (modern versions)
    • VS Code terminal
    • PyCharm terminal

    …some older terminals might not display the emoji correctly and may show boxes or question marks instead.

  • How to set up a Python Virtual Environment on Mac, Windows or Linux

    Setting up a Python virtual environment is one of the most important skills you can learn as a Python developer. It helps you manage dependencies, avoid version conflicts, and keep your global environment clean — especially when working on multiple projects.

    In this guide, I’ll walk you through setting up a virtual environment on macOS, Windows, and Linux with clear, step-by-step instructions.

    So what is a Virtual Environment?

    A virtual environment is a self-contained directory where Python packages can be installed independently of your system’s global Python setup. It’s perfect for:

    • Keeping dependencies isolated per project
    • Preventing version conflicts
    • Making your projects easier to share and deploy

    Prerequisites

    Before you begin, make sure you have:

    • Python 3.3 or later installed
    • pip (Python’s package manager)

    To check the system version of Python you have installed on a Mac open a terminal prompt and run:

    (base) peter@MacBookPro Python % python -V
    Python 3.9.12

    Similarly, to check your pip installation:

    (base) peter@MacBookPro Python % pip -V
    pip 24.0 from /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)
    (base) peter@MacBookPro Python % 

    Create a Project Directory

    Create a Directory where you are going to be developing your project and the navigate to it in a terminal or bash window:

    (base) peter@MacBookPro Python % mkdir newdirectory
    (base) peter@MacBookPro Python % cd newdirectory

    Create the Virtual Environment

    Enter the following command to create the virtual environment;

    Bash
    python3 -m venv virtualenvname

    This will create.a subdirectory containing your virtual environment. I tend to call my virtual environments (or ‘virtual envs’) .venv but the choice is yours,

    Activating the Virtual Environment

    Mac OS / Linux

    Before activation

    Bash
    (base) peter@MacBookPro sbs % 

    Activated

    Bash
    (base) peter@MacBookPro sbs % source virtenv/bin/activate<br>(virtenv) (base) peter@MacBookPro sbs % 

    Windows

    Command Prompt

    myenv\Scripts\activate.bat

    Powershell

    PowerShell
    myenv\Scripts\Activate.ps1
    

    Installing Packages

    Now that your environment is active, you can install packages without affecting your global Python installation.

    An example of how to install a package. With the virtual environment (virtenv) activated:

    (virtenv) (base) peter@MacBookPro sbs % pip install requests

    The response I get:

    (virtenv) (base) peter@MacBookPro sbs % pip install requests
    Collecting requests
      Using cached requests-2.32.4-py3-none-any.whl.metadata (4.9 kB)
    Requirement already satisfied: charset_normalizer<4,>=2 in ./virtenv/lib/python3.9/site-packages (from requests) (3.4.2)
    Requirement already satisfied: idna<4,>=2.5 in ./virtenv/lib/python3.9/site-packages (from requests) (3.10)
    Requirement already satisfied: urllib3<3,>=1.21.1 in ./virtenv/lib/python3.9/site-packages (from requests) (2.4.0)
    Requirement already satisfied: certifi>=2017.4.17 in ./virtenv/lib/python3.9/site-packages (from requests) (2025.4.26)
    Using cached requests-2.32.4-py3-none-any.whl (64 kB)
    Installing collected packages: requests
    Successfully installed requests-2.32.4
    
    [notice] A new release of pip is available: 25.1.1 -> 25.2
    [notice] To update, run: pip install --upgrade pip
    (virtenv) (base) peter@MacBookPro sbs % 

    The pip process will install any dependencies which may or may not already be present on your local system so the response you get may vary from mine.

    The key point is you should receive a success message similar to line 10 in the example above.

    Note: whenever you use pip for the first time in a virtual environment, you will receive a notice that your pip version is out of date (lines 12 & 13 in the example). Running an out of date version doesn’t appear to make any difference but you may want to consider upgrading (line 13) if only to remove this annoying message.

  • Number guessing 2

    This entry is part 2 of 2 in the series Number Guessing
    1
    Python
    import random
    
    def number_guessing_game():
        print("🎉 Welcome to the Number Guessing Game!")
        print("I'm thinking of a number between 1 and 100...")
    
        # Generate a random number between 1 and 100
        secret_number = random.randint(1, 100)
        attempts = 0
    
        while True:
            try:
                guess = int(input("Enter your guess: "))
                attempts += 1
    
                if guess < 1 or guess > 100:
                    print("🚫 Please enter a number between 1 and 100.")
                    continue
    
                if guess < secret_number:
                    print("📉 Too low! Try again.")
                elif guess > secret_number:
                    print("📈 Too high! Try again.")
                else:
                    print(f"✅ Congratulations! You guessed it in {attempts} attempts.")
                    break
    
            except ValueError:
                print("⚠️ Please enter a valid number.")
  • Number guessing 1

    This entry is part 1 of 2 in the series Number Guessing
  • Beginners Guide to GIT

    Here’s a concise guide to the main Git commands and what they do. These are essential for version control and collaboration in software development:

    Setup & Configuration

    • git config –global user.name “Your Name”
      Set your Git username.
    • git config –global user.email “you@example.com
      Set your Git email.

    Repository Management

    • git init
      Initialize a new Git repository.
    • git clone <url>
      Copy a remote repository to your local machine.

    Tracking Changes

    • git status
      Show the current state of the working directory and staging area.
    • git add <file>
      Stage changes for commit.
    • git add .
      Stage all changes in the current directory.
    • git commit -m “message”
      Save staged changes with a descriptive message.

    Branching & Merging

    • git branch
      List all branches.
    • git branch <name>
      Create a new branch.
    • git checkout <branch>
      Switch to another branch.
    • git merge <branch>
      Merge changes from another branch into the current one.

    Working with Remotes

    • git remote -v
      View remote repository URLs.
    • git push
      Upload local commits to a remote repository.
    • git pull
      Fetch and merge changes from a remote repository.
    • git fetch
      Download changes from a remote repository (without merging).

    Viewing History

    • git log
      View commit history.
    • git diff
      Show changes between commits, branches, or files.
    • git show <commit>
      Show details of a specific commit.

    Undoing Changes

    • git reset <file>
      Unstage a file.
    • git checkout — <file>
      Discard changes in a file.
    • git revert <commit>
      Create a new commit that undoes a previous one.

    Would you like a printable cheat sheet or a visual diagram to go with this?

    In Git, “staged” refers to files that have been marked to be included in the next commit. It’s part of Git’s three-stage workflow:

    Git Workflow States

    1. Working Directory\ This is where you make changes to your files. These changes are not yet tracked by Git for committing.
    2. Staging Area (Index)\ When you run git add <file>, Git moves the file to the staging area. This means you’re telling Git:\ “I want to include this change in the next snapshot (commit).”
    3. Repository (Commit History)\ When you run git commit, Git takes everything in the staging area and saves it permanently in the repository as a new commit.

    Example Workflow

    # You modify a file

    nano hello.py

    # You stage the file

    git add hello.py

    # You commit the staged file

    git commit -m “Add greeting script”

    Only files that are staged will be included in the commit. If you modify other files but don’t stage them, they won’t be part of that commit.

  • Importing packages into Python

    In Python, the import statement is used to bring in modules and their functionality so you can use them in your code. There are several variations of the import statement, each serving different purposes depending on what and how you want to import.

    1. Basic Import

    import math

    This imports the entire math module. You access its functions with the module name:

    math.sqrt(16)

    2. Import with Alias

    import numpy as np

    This imports the module and gives it a shorter alias (np), which is useful for long module names or common conventions.

    3. Import Specific Functions or Classes

    from math import sqrt, pi

    This imports only the specified items from the module. You can use them directly:

    sqrt(25)

    4. Import All (Not Recommended)

    from math import *

    This imports everything from the module into the current namespace. It’s discouraged because it can lead to name conflicts and makes code harder to read.

    5. Dynamic Import (Using __import__)

    module_name = “math”

    math_module = __import__(module_name)

    This is useful when the module name is determined at runtime.

    6. Import from a Package

    If you have a package with submodules:

    from mypackage import submodule

    Or even deeper:

    from mypackage.submodule import myfunction

    7. Relative Imports (Used in Packages)

    from . import sibling_module

    from ..parent_package import parent_module

    These are used within packages to refer to other modules relative to the current module’s location.

  • Python: not necessarily as easy as they make out


    1. Misunderstanding What “Easy” Means

    Python has a simple syntax, but that doesn’t make programming itself easy. Logic, problem-solving, debugging, and understanding how computers “think” are still challenging — especially for those with no prior coding experience

    2. Too Much Theory, Not Enough Practice

    Many learners get stuck in “tutorial hell” — watching endless videos or reading about Python syntax without actually writing code. Without consistent practice, the concepts don’t stick.

    ✅ Tip: Build small projects early, even if they’re messy.


    3. Confusion About Python Versions & Setup

    Installing Python and dealing with versions (e.g., Python 2 vs Python 3), virtual environments, or package managers like pip can be confusing at first — especially on Windows or Linux.

    ✅ Tip: Beginners should follow a guided setup tutorial tailored to their OS.


    4. Abstract Concepts Like Loops, Functions, and OOP

    Things like loops, functions, and especially object-oriented programming can be hard to grasp without visual or practical examples. These are core concepts that take time to internalize.

    ✅ Tip: Use analogies and diagrams (like comparing functions to “kitchen recipes”) to make ideas more tangible.


    5. Lack of a Clear Learning Path

    New learners often jump between random tutorials, not knowing whether to focus on web development, data science, or automation. Without a roadmap, motivation fades.

    ✅ Tip: Choose one path (e.g., web apps with Flask, or data analysis with pandas) and go deep.


    6. Debugging Frustration

    Even a missing colon can crash your code. For beginners, error messages are often cryptic, and knowing how to fix bugs takes time and experience.

    ✅ Tip: Use an IDE like VS Code with Python extensions — it’ll catch many errors early.


    7. Overwhelming Amount of Libraries and Tools

    Python has libraries for everything — from web scraping to AI — but this abundance can overwhelm beginners. It’s hard to know what’s essential vs. what can wait.

    ✅ Tip: Start with the standard library and learn just enough to solve your current problem.


    8. Fear of “Not Being Good Enough”

    A lot of people compare themselves to others online. When they struggle with things that “should be easy,” they assume they’re not smart enough — which isn’t true.

    ✅ Tip: Programming is a skill, not a talent. You get better by doing, not by knowing everything upfront.


    Summary

    Python is a great first language, but learning to program involves way more than just syntax. With patience, the right guidance, and consistent practice, anyone can become proficient — but it takes time. If you’re struggling, that’s normal.

    Want to make it easier? Start building something small — even a basic calculator — and learn as you go. That’s how real progress happens.


    Would you like me to turn this into a blog post, infographic, or short video script for your site?