CS 210, Intro to AI Programming
Topics | |
---|---|
1. What is AI, Python | 6. ANN: Image recognition |
2. Symbolic AI | 7. Generative AI |
3. Classical Machine Learning: Training | 8. Custom chatbot |
3. Classical Machine Learning: Inference | 9. LLM fine-tuning |
5. Midterm | 10. Ethics |
11. Final |
Learning a New Programming LanguageInstalling Python and Running a ProgramWindowsCheck for Python and the Python LauncherInstall One or Both if MissingRun Hello WorldMacOSCheck the version of PythonInstall or Update PythonInstall Homebrew (If You Don't Have It)Install PythonRun Hello WorldLearning PythonDistictives of PythonFizzBuzz Example ProgramProgramming Paradigms of PythonExercise: Translate a Dice Game into PythonMore ResourcesMore Lecture Notes on PythonOnline TutorialsOnline Videos
Most programming languages have many things in common:
They implement algorithms—think of an algorithm as a recipe or set of instructions for doing something.
They have a way to implement the three common control structures:
Sequence
Selection
Repetition
There are ways to define and call functions (aka procedures, methods, subroutines)
There are ways to represent structured data:
Strings
Arrays Python has lists, which are similar. The NumPy library provides actual arrays.
Dictionaries
Once you've learned how to work with these concepts in one language, it's mostly just a mater of learning the new syntax for the next language you learn.
Open a Power Shell Terminal on your computer and check to see if Python is installed on your system using this command:
xxxxxxxxxx
python --version
If it is installed, the version number will be reported.
Check to see if the Python launcher is installed:
xxxxxxxxxx
py --version
If it is installed, the version number will be reported.
Download the latest Python installer and run it.
If Python isn't installed, then install it using the custom setup. Check the "Python Launcher" box in the "Optional Features" dialog.
If Python is installed, but not py, then choose the "Modify" option. In the "Optional Features" dialog, check the box for "Python Launcher".
Create the "Hello World" Script
xxxxxxxxxx
# A simple program to print a greeting
print("Hello, World! Python is running.")
Save the file as hello.py
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
Execute the Script:
Use the python
command followed by the file name:
xxxxxxxxxx
python hello.py
(If python
doesn't work, try using py hello.py
instead.)
Output: The Terminal will display the result:
xxxxxxxxxx
Hello, World! Python is running on Windows.
While macOS comes with a system version of Python pre-installed, it's outdated and should not be used for development. We will install a modern version using Homebrew, the popular package manager for macOS.
In the Terminal, run:
xxxxxxxxxx
python3 --version
If it's older than 3.12 (or whtever the latest version is) then upate it.
Homebrew is a tool that makes installing developer software on Mac incredibly easy.
Open the Terminal:
Press Cmd + Space
and type "Terminal," then press Enter.
Run the Homebrew installation command:
Copy and paste the following line into your Terminal and press Enter
xxxxxxxxxx
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
The script will prompt you to enter your administrator password (it won't display as you type) and press Enter.
Follow any final on-screen instructions, such as adding Homebrew to your system's PATH.
Once Homebrew is installed, installing Python is a single command.
In the Terminal, run:
xxxxxxxxxx
brew install python
Homebrew will download and install the latest stable version of Python (typically Python 3.x).
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:
xxxxxxxxxx
python3 --version
You should see output confirming the version you just installed (e.g., Python 3.12.2
).
Create your Python file: Use a text editor to create a file named hello.py
and add the following content:
xxxxxxxxxx
# A simple program to print a greeting
print("Hello, World! Python is running.")
Open Terminal and Navigate: Open your Terminal and navigate to the directory where you saved hello.py
(using the cd
command).
Run the script: Execute the file using the Python 3 interpreter:
xxxxxxxxxx
python3 hello.py
Output: The Terminal will display the output:
xxxxxxxxxx
Hello, World! Python is running.
No curly braces! Indentation is used to delineate blocks of code.
Dynamic typing: You don't need to declare the data types of variables.
Interpreted, not compiled.
No ;
to terminate lines. The end of the line is the end of the line.
Python has a distinctive pythonic style which is described in the PEP-8 style guide.
Here is an implementation of the classic "FizzBuzz" program1 in Python:
xxxxxxxxxx
# 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)
Multi‑paradigm, but not in equal balance
While Python supports OOP, functional programming, and procedural code, its sweet spot blends object orientation with functional elements — without the ceremony of C++ or Java’s class scaffolding.
Dynamic typing and duck typing: Python favors type flexibility at runtime over compile‑time enforcement. Class types are implicit — if it quacks and walks like a duck, it’s treated like a duck.
First‑class functions: You can pass, return, and store functions just like data, which supports a more functional style than many C‑family languages.
Here is an example of Object Oriented Programming in Python. This car simulator illustrates these of OOP concepts:
Composition: The Car
class has-a an Engine
instance.
Inheritance: Driver
and Passenger
are-a special types of Occupant
.
Encapsulation: The car's state (like fuel level and running status) is managed internally by the Engine
class.
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:
Die
: Represents the physical object that can be rolled.
Player
: Represents a participant who interacts with the die.
DiceBattle
: Orchestrates the game flow and holds the game state (the players and the die).
Python Collections Data Types—Brian Bird and Google Gemini
Python Loops—Brian Bird and Google Gemini
Python Tutorial—Official Python Web Site
Python Tutorial—W3Schools
The Python Handbook—Falvio Copes, FreeCodeCamp
PEP 8 – Style Guide for Python Code—Official Python Web Site
Summary of the Style Guide—Brian Bird and Google Gemini
Note: Some parts of this document were initially drafted with assistance from Gemini 2.5 Flash 9/30/25
Intro to AI Programming Course Materials by Brian Bird, written in , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.