I1 · Python Programming for AI

Module Review and Assessment

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.

15 min Intermediate

What you need: The earlier topics of this module.

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

SkillCheck yourself
AlgorithmsCan you write and trace the steps before coding?
Types and conversionDo you convert input text before maths?
Decisions and loopsCan you choose between for and while?
CollectionsList, tuple, set or dictionary: which fits?
FunctionsDo your functions return results instead of printing?
Search, sort and Big-OCan you say why binary search is O(log n)?
Files, errors and classesCan 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.

challenge.py
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

Question 1

1. Which is NOT needed in a good algorithm?

Question 2

2. What does print(type(3.0).__name__) show?

Question 3

3. int("7") + 3 is…

Question 4

4. "AI LAB".lower()[0:2] is…

Question 5

5. How many times does for i in range(2, 8, 2): run?

Question 6

6. Which loop suits 'repeat until the password is right'?

Question 7

7. Which collection removes duplicates?

Question 8

8. d = {"a": 1}. What is d.get("b", 5)?

Question 9

9. [x * x for x in range(4)] is…

Question 10

10. A queue removes items in which order?

Question 11

11. Binary search on 1,000,000 sorted items needs about…

Question 12

12. Bubble sort is…

Question 13

13. Which file mode keeps old content and adds to the end?

Question 14

14. Which block always runs in try/except/finally?

Question 15

15. In a class, what does self.name = name inside __init__ do?

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?