All courses

Intermediate · module I1

Python Programming for AI

By the end: Plan a solution as an algorithm, then write, run and debug Python programs that use variables, decisions, loops, collections, functions, searching and sorting algorithms and files, and build a simple chatbot

Covers
From planning a solution as an algorithm to writing small AI-style programs in Python
Contents
10 chapters, 34 topics
Time
About 6 hours
Level
Intermediate
Your progress
0 / 34

Chapter 1 Algorithms and Problem Solving

  1. 1.1

    From Problem to Algorithm

    Inputs, precise steps and outputs, and what makes an algorithm complete and correct

    Activity: write the steps to find the largest of three numbers

    8 min Read Activity
  2. 1.2

    Pseudocode and Flowcharts

    Writing one algorithm as structured plain language and as a flowchart, using sequence, decision and repetition

    Interactive: turn a flowchart into pseudocode

    10 min Read Activity
  3. 1.3

    Tracing an Algorithm by Hand

    Dry-running the steps with a trace table to predict the result before any program runs

    Activity: trace a loop that totals a list of marks

    8 min Read Activity

Chapter 2 Getting Started with Python

  1. 2.1

    Introduction to Python

    What Python is, why AI uses it, and how a program runs

    Run a first program in the browser

    8 min Read Activity
  2. 2.2

    Setting Up a Python Environment

    Browser playground, Jupyter notebooks, VS Code and Google Colab

    Lab: open and run a notebook

    10 min Read Activity
  3. 2.3

    Output, Comments and Program Structure

    print(), comments, statements and indentation

    Code: print a formatted profile card

    8 min Read Activity
  4. 2.4

    Reading Errors and Debugging

    Syntax errors and exceptions, and reading a traceback

    Code: fix three broken programs

    10 min Read Activity

Chapter 3 Variables and Data Types

  1. 3.1

    Variables

    Creating, naming and updating variables

    Code: store and update a score

    8 min Read Activity
  2. 3.2

    Data Types

    Integers, floats, strings, booleans and None, and type()

    Code: identify the type of each value

    8 min Read Activity
  3. 3.3

    Input and Type Conversion

    input(), int(), float() and str()

    Code: an age calculator

    8 min Read Activity
  4. 3.4

    Operators and Expressions

    Arithmetic, assignment, comparison and logical operators, and their order

    Code: a shopping bill calculator

    10 min Read Activity

Chapter 4 Working with Text

  1. 4.1

    Strings, Indexing and Slicing

    Accessing characters and parts of text

    Code: extract initials and reverse a word

    10 min Read Activity
  2. 4.2

    String Methods and Formatting

    upper, lower, strip, split, replace, join and f-strings

    Code: clean and format user input

    10 min Read Activity

Chapter 5 Control Flow

  1. 5.1

    Conditional Statements

    if, elif and else, with nested decisions

    Code: a grade calculator

    10 min Read Activity
  2. 5.2

    for Loops and range()

    Repeating work for each item and for a count

    Code: a multiplication table

    10 min Read Activity
  3. 5.3

    while Loops, break and continue

    Repeating until a condition changes

    Code: a number-guessing game

    10 min Read Activity

Chapter 6 Data Structures

  1. 6.1

    Lists

    Creating, indexing, changing, sorting and looping over lists

    Code: a marks list with average and topper

    12 min Read Activity
  2. 6.2

    Tuples and Sets

    Fixed sequences and unique collections

    Code: find unique words in a sentence

    8 min Read Activity
  3. 6.3

    Dictionaries

    Key-value pairs, lookups and nested dictionaries

    Code: a word counter

    12 min Read Activity
  4. 6.4

    List and Dictionary Comprehensions

    Building collections in one clear line

    Code: squares and filters

    8 min Read Activity

Chapter 7 Functions and Modules

  1. 7.1

    Defining Functions

    Parameters, arguments, return values and default values

    Code: a reusable temperature converter

    12 min Read Activity
  2. 7.2

    Scope and Lambda Functions

    Where variables live, and short anonymous functions

    Code: sort records with a lambda

    8 min Read Activity
  3. 7.3

    Modules and Libraries

    import, the standard library, and installing packages with pip

    Code: use math, random and datetime

    10 min Read Activity

Chapter 8 Algorithms in Python

  1. 8.1

    Stacks and Queues

    Last-in-first-out and first-in-first-out, with a list and collections.deque

    Code: an undo button and a print queue

    8 min Read Activity
  2. 8.2

    Linear and Binary Search

    Checking items one by one, and halving a sorted list at every step

    Code: find a name in a sorted list both ways

    12 min Read Activity
  3. 8.3

    Sorting Algorithms

    Selection, bubble and insertion sort written by hand, compared with sorted()

    Code: sort scores by hand and check against sorted()

    12 min Read Activity
  4. 8.4

    Recursion

    A function that calls itself, the base case and the call stack

    Code: factorial and a countdown

    10 min Read Activity
  5. 8.5

    Algorithm Efficiency and Big-O Notation

    Counting the steps an algorithm takes as the input grows

    Code: count comparisons in linear and binary search

    Linear search O(n), binary search O(log n), bubble sort O(n²)

    10 min Read Activity

Chapter 9 Files and Exceptions

  1. 9.1

    Reading and Writing Files

    Text files and CSV files

    Code: save and load an attendance register

    10 min Read Activity
  2. 9.2

    Handling Exceptions

    try, except, else and finally

    Code: a calculator that never crashes

    8 min Read Activity

Chapter 10 Python for AI Projects

  1. 10.1

    Introduction to Classes and Objects

    Bundling data and behaviour, __init__ and self

    Code: a Student class

    12 min Read Activity
  2. 10.2

    Project: A Rule-Based Chatbot

    Keyword matching, responses and a conversation loop

    Project: build a help-desk chatbot

    20 min Read Activity
  3. 10.3

    Project: A Marks Analyser

    Reading data, computing statistics and reporting results

    Project: analyse a batch of marks from a CSV file

    20 min Read Activity
  4. 10.4

    Module Review and Assessment

    Coding exercises and a scored quiz

    Graded coding exercises

    15 min Read