CS 212, AI Programming 1

Intro to Python

Contents

[TOC]

Learning a New Programming Language

Most programming languages have many things in common:

Once you’ve learned how to work with these concepts in one language, it’s mostly just a matter of learning the new syntax for the next language you learn.

Installing Python and Running a Program

Windows

Check for Python and the Python Launcher

Open a Power Shell Terminal on your computer and check to see if Python is installed on your system using this command:

python --version

If it is installed, the version number will be reported.

Check to see if the Python launcher is installed:

py --version

If it is installed, the version number will be reported.

Install One or Both if Missing

Download the latest Python installer and run it.

Run Hello World

  1. Create the “Hello World” Script
# A simple program to print a greeting
print("Hello, World! Python is running.")

Save the file as hello.py

  1. Run the Program in Command Prompt (CMD) or PowerShell

    Open the Command Prompt:

    • Press the Windows Key + R, type cmd, and press Enter. (Or use the Windows search bar to find “Command Prompt” or “PowerShell”).

    • Use the cd (Change Directory) command to move to the folder where you saved your script. If you saved it to C:\PythonProjects, you would type: cd C:\PythonProjects

  2. Execute the Script:

Use the python command followed by the file name:

python hello.py

(If python doesn’t work, try using py hello.py instead.)

Output: The Terminal will display the result:

Hello, World! Python is running on Windows.

MacOS

While macOS comes with a system version of Python pre-installed, it’s outdated and should not be used for development. We will install an up to date version using Homebrew, a package manager for macOS.

Check the version of Python

In the Terminal, run:

python3 --version

If it’s older than 3.14 (the latest version in October 2026) then update it.

Install or Update Python

Install Homebrew (If You Don’t Have It)

Installation instructions are here.

Install Python

In the Terminal, run:

brew install python

Homebrew will download and install the latest stable version of Python.

Verify the Installation

After the installation completes, confirm that the correct version is now recognized by your system.

Check the Python version:

In the Terminal, run:

python3 --version

You should see output confirming the version you just installed (e.g., Python 3.12.2).

Run Hello World

  1. Create your Python file: Use a text editor to create a file named hello.py and add the following content:

    # A simple program to print a greeting
    print("Hello, World! Python is running.")
    
  2. Open Terminal and Navigate: Open your Terminal and navigate to the directory where you saved hello.py (using the cd command).

  3. Run the script: Execute the file using the Python 3 interpreter:

    python3 hello.py
    
  4. Output: The Terminal will display the output:

    Hello, World! Python is running.
    

Using VS Code for Python Programming

Install the Python Extension

Use the Debugger

  1. Open Your Project Folder
    • Open the folder containing your .py files in VS Code.
  2. Select the Python Interpreter
    • Press Ctrl+Shift+P and type Python: Select Interpreter.
    • Choose the interpreter you want to use (e.g., Python 3.x).
  3. Set Breakpoints
    • Open the Python file you want to debug.
    • Click in the gutter to the left of the line numbers to set breakpoints.
  4. Start Debugging
    • Open the file you want to run (for example, main.py).
    • Press F5 to start debugging, or click the green “Run and Debug” button in the Run view (Ctrl+Shift+D).
    • The debugger will stop at your breakpoints, and you can inspect variables, step through code, etc.
  5. Debug Controls
    • Use the debug toolbar at the top to step over (F10), step into (F11), continue (F5), or stop (Shift+F5).
  6. Inspect Variables
    • Use the “Variables” pane in the Run view to see local and global variables.
    • Hover over variables in the editor to see their values.

Tips

Learning Python

Distinctives of Python

Here is an implementation of the classic “FizzBuzz” program1 in Python:

# FizzBuzz program
for i in range(1, 10):
    output = ""

    # Check for divisibility by 3
    if i % 3 == 0:
        output += "Fizz"

    # Check for divisibility by 5
    if i % 5 == 0:
        output += "Buzz"

    # If output is still empty, print the number
    if output == "":
        print(i)
    else:
        print(output)

Programming Paradigms of Python

Here is an example of Object Oriented Programming in Python. This car simulator illustrates these of OOP concepts:

Exercise

Translate a simple game from either JavaScript or C# into Python.

The game is “die battle”: two players each roll a single six-sided die, and the player with the higher number wins. It requires defining three simple classes:

  1. Die: Represents the physical object that can be rolled.
  2. Player: Represents a participant who interacts with the die.
  3. DiceBattle: Orchestrates the game flow and holds the game state (the players and the die).

The solution is here.

Reference

Note: Parts of this document were drafted with assistance from Gemini 2.5 Flash 9/30/25


Creative Commons License AI Programming Course Materials by Brian Bird, written in 2025, revised in are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

  1. The FizzBuzz program iterates from 1 up to some specified limit. It prints “Fizz” for multiples of 3, “Buzz” for multiples of 5, “FizzBuzz” for multiples of both, or just the number. ↩