Python

The programming foundation for artificial intelligence: data structures, control flow, functions, files, modules, and notebook practice.

Material

Syntax and values

Numbers, strings, booleans, variables, expressions, comments, and readable naming.

Collections

Lists, tuples, dictionaries, sets, indexing, slicing, iteration, and nested data.

Control flow

Conditionals, loops, comparisons, truth values, and simple decision rules.

Functions

Parameters, return values, scope, reusable transformations, and small testable blocks.

Files and modules

Reading files, writing outputs, importing libraries, and organizing code into modules.

AI notebook habits

Run cells deliberately, inspect intermediate values, keep experiments reproducible, and explain each step.

Exercise: values, loops, and conditions

Use a list of model-like scores, compare each value to a threshold, and create a label for each result.

  • Change the threshold to 0.85 and compare the labels.
  • Add a third label named excellent for scores above 0.90.
  • Print each score and label together.
scores = [0.72, 0.81, 0.66, 0.90]
threshold = 0.75

labels = []

for score in scores:
    if score >= threshold:
        labels.append("ready")
    else:
        labels.append("review")

print(labels)

Exercise: turn logic into a function

AI code becomes easier to read when repeated decisions are moved into small functions with clear inputs and outputs.

  • Add a docstring that explains what label_score returns.
  • Call the function with a custom threshold.
  • Write a second function that returns the average score.
def label_score(score, threshold=0.75):
    if score >= threshold:
        return "ready"

    return "review"


scores = [0.72, 0.81, 0.66, 0.90]
labels = [label_score(score) for score in scores]

print(labels)