Definition
Ready to show what you can do in Python?
This review brings together Python Programming for AI: algorithms and tracing, variables and types, text, decisions and loops, collections, functions, classic searching and sorting algorithms with Big-O, files, exceptions and classes, with a coding challenge and a scored quiz.
In simple words
First try the challenge: the program below has a function to complete. Then take the quiz. Every answer is explained, so use wrong answers as a list of topics to revisit.
Why it matters
Every later module, from NumPy and pandas to machine learning and LLM apps, assumes these skills. A quick check now saves confusion later.
How it works
| Skill | Check yourself |
|---|---|
| Algorithms | Can you write and trace the steps before coding? |
| Types and conversion | Do you convert input text before maths? |
| Decisions and loops | Can you choose between for and while? |
| Collections | List, tuple, set or dictionary: which fits? |
| Functions | Do your functions return results instead of printing? |
| Search, sort and Big-O | Can you say why binary search is O(log n)? |
| Files, errors and classes | Can you read a CSV safely and wrap data in a class? |
In code
Challenge: complete count_vowels so the checks print True. A working version is filled in; try deleting it and writing your own.
def count_vowels(text):
# Return how many of a, e, i, o, u appear in text, ignoring case.
return sum(1 for ch in text.lower() if ch in "aeiou")
checks = [
count_vowels("Python") == 1,
count_vowels("ARTIFICIAL") == 5,
count_vowels("") == 0,
count_vowels("rhythm") == 0,
]
print(checks)
print("All passed!" if all(checks) else "Keep going")Output
[True, True, True, True] All passed!
Common mistakes
- Skipping the tracing step and guessing what code does.
- Mixing up = and ==, or / and //.
- Running binary search on unsorted data.
Check yourself
Summary
- You can plan an algorithm, trace it, and write it in Python.
- You can use collections, functions, classic algorithms, files, errors and classes.
- Next: working with real data in NumPy, pandas and SQL.
Finished reading?