Python

Updated March 23, 2026c

Made with the most excellent and free Pandoc document converter, by converting Markdown to HTML. And Pandoc puts in the Table of Contents too!

1 What is a class?

Think of variables as objects that can hold one value, like an integer, or hold many values, like a list. A class defines another custom object which is more complex. A class is a blueprint for a variable/object with integers, strings, lists, dictionaries, or more and it can have its own functions called methods. The variable portion of a class object are called properties or attributes, it depends which language you talk about.

A "class object" is a variable of that class. So in a video game we can make a class with these properties: Charactername, strength, intelligence, dexterity, speed, and an image for what they look like. And every character uses the same class but with different values for the variables/properties inside that class.

1.1 Example class

I use this class to pass options to every function I use. It can hold a lot of data, including large lists and large dictionaries.

import os   
import time
#####################################################
# Classes
# Option variables go here.
# Ways to access class members/properties: print(options.debug)
#    print(vars(options)['debug'])
#    print(getattr(options,'debug'))
class clsOptions: # This is the class blueprint.
    r'''
    This holds all command line options.
    '''
    def __init__(self):
        r'''These are class variables we need to use with initial values.'''
        # Program options
        self.debug = False # From --debug
        self.debuglist = [] # List of models to debug/break on.
        self.optlimitread = 0 # 0 = no limit on reading records.
        self.optoverwrite = False # True to overwrite output files, for testing.
        self.optcolorchg = 0 # From --colorchg:N
        self.starttime = 0 # Used to time how long the program takes to run.

    def __repr__(self):
        r'''Usage: in debugger show the value of all class members like this: p options
        This reduces typing quite a bit.
        '''
        procname = str(inspect.stack()[0][3]) + ":"
        s = f"{procname} This holds command line options.\n"
        s = s + f"debug={self.debug}\n"
        s = s + f"debuglist={self.debuglist}\n"
        s = s + f"optlimitread={self.optlimitread}\n"
        s = s + f"optcolorchg={self.optcolorchg}\n"
        s = s + f"optoverwrite={self.optoverwrite}\n"
        s = s + f"starttime={self.starttime}\n"
        
        return s
        
# Here we make the instance of a class, the actual variable we use.    
options = clsOptions() # Make the class variable/instance.
options.starttime = time.time() # Set a class property

2 Programming and AI

Why is AI often bad to use with programming?

I found this out the hard way. I was asking AI how to do stuff in Python, and didn't mention the Python version. AI was giving me code from 13 years ago that didn't work in modern python. You have to be really specific with your questions. Once I included the Python version in my questions all the code worked.

And I asked it to write a program (a screen saver) using pygame but it never knew pygame was deprecated and replaced with pygame-ce. So my program didn't work in Python 3.14. Pygame wasn't updated for the newer version of Python.

AI seems fine to suggest snippets of code for a task I want to do but it is not good at designing the overall program, especially using proper security for an online web program.

Just find Python tutorials on Youtube. I like the free Indently tutorials. Or use https://freecodecamp.org/. They have a Python tutorial on YT as well.

3 Python forums

  1. Main forum. https://discuss.python.org

4 Python tools

4.1 Python tutorials I like

  1. Freecodecamp. https://freecodecamp.org
  2. Indently tutorials on Youtube. Site: https://www.youtube.com/@Indently Python Tutorial from 2023: https://www.youtube.com/watch?v=bnSYeYFRCaA&list=PL4KX3oEgJcffJTxggH5LviQeMHiNagq3y The only issue here is you should use f-strings now, not %s for variables inside strings.
  3. Udemy also has some good tutorials for about $12-15usd on sale. https://udemy.com

4.2 Test Regex

  1. Debuggex. Test different types of regex like Javascript, Python, PCRE (Perl). https://www.debuggex.com/
  2. Regex Kit. Test various types of regex like PCRE, Javascript, Pythan, etc. https://regexkit.com/regex-tester
  3. Regex 101. This has a regex explainer. Enter your regex and test data to test it. Sign in to save your test cases. Has many flavors of regex to test like: PCRE2 (PHP), Javascript, Python, Golang, Java, .NET 7.0 (C#), Rust, and more. Sign in with Google or Github accounts. https://regex101.com/
  4. Regexr. Enter your regex and multipule lines of test data. This will highlight the data to show matches. Sign in to save your tests. Use your Github or Google account to sign in. https://regexr.com
  5. Regexpal. Has login. https://www.regexpal.com/
  6. Regextester. Test different flavors of regular expressions like PCRE. Has login account. Text won't fit to browser window. https://www.regextester.com/ This might be the same: https://regextester.github.io/
  7. Search for more: https://search.brave.com/search?q=free+account+test+regular+expression&source=web

5 Regex quickref sites

  1. PCRE Regex Assertions docs. https://www.php.net/manual/en/regexp.reference.assertions.php
  2. RexEgg. Learn regular expressions. https://www.rexegg.com/regex-quickstart.html

5.0.1 Commonly used regex

  1. Search for all extended ascii: [\d128-\d255] or [\x80-\xff]
  2. Match all non-printable characters: [^ -~] (space through tilde)
  3. Match all printable characters: [ -~]

5.1 Run test code online

5.1.1 No login needed

These sites may or may not save your code that you type in. Always have a backup of your code. But they can be used to test small snippets of code or concepts.

  1. +++ Attempt this online. This is really cool! Supports many languages like: Python, Ada, AWK, BASH, C, C++, Java, Javascript (node.js), Lua, Perl, PHP, PowerShell, many more!. No account needed. https://ato.pxeger.com/about
  2. Online Python. No account needed for login. https://www.online-python.com/
  3. PyNative Python Compiler. No account needed. https://pynative.com/online-python-code-editor-to-execute-python-code/

5.1.2 Login needed?

  1. Anaconda. https://anaconda.com You can use Anaconda to develop and deploy Python solutions, manage packages, and create environments for data science and machine learning projects. Store and share notebooks. This sounds similar to Jupyter. Write and debug code with their AI.
  2. Google Colab. Login via your free Google account. This works just like Jupyter labs. https://colab.research.google.com/
  3. Jupyter Labs. https://jupyter.org/ You can run this in your browser or install it locally. Data is stored in your browser so if you use another browser or PC your data will not be there.
  4. OneCompiler. https://onecompiler.com/python Uses Google account but login only needed if you want to save your code. This supports many languages like Bash, Basic, Pascal, Python, Java, Javascript, C, C#, C++, NodeJS, Groovy, Haskell, PHP, Typescript, and more.
  5. Pyscript. Run Python in a web browser. You can use this on your site as well. You probably need full shell access to make this work on your site though. https://pyscript.net/
  6. PythonAnywhere ($) by Anaconda. This costs money. https://www.pythonanywhere.com/ This is used to host websites. They have quick installers for Django, web2py, Flask, and Bottle frameworks.
  7. PythonHow Python Shell. https://pythonhow.com/python-shell Write program and run it, or use online Python shell.
  8. Python dot org shell. This is the Python prompt shell to run only single commands. There is no editor here. No login needed. https://www.python.org/shell/
  9. Replit. ($) https://replit.com/ Replit is an AI-powered software development & deployment platform for building, sharing, and shipping software fast.
  10. A search for more. https://www.google.com/search?q=run+Python+online+free

6 Python code

6.1 Get version of python

Command line: python --version

In code:

import sys
print(sys.version)
# Outputs: '3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]'

# More details
speedtestname = "Module name"
print(sys.version_info)
if (sys.version_info > (sys.version_info.major, sys.version_info.minor, 0)):
    sys.exit(f"ERROR: This module {speedtestname} only supports Python 2.4 - 3.7.")

# Find shorter version like: 3.11.9
import platform
print(platform.python_version()) # Prints '3.11.9'
t = platform.python_version_tuple() # If you want a tuple
print(t) # prints ('3', '11, '9')