Introduction to Python for AI (Basics)
Introduces the core Python building blocks every AI practitioner uses daily: variables, data types, lists, dictionaries, loops, functions, and libraries like NumPy and pandas, all runnable in free browser tools.
{"contentFormat":"slides.v1","completion":{"requireAllSlides":true,"requireQuiz":true},"slides":[{"kind":"title","eyebrow":"Module 6: Coding for AI + Vibe Coding","title":"Introduction to Python for AI (Basics)","body":"Python is the language of modern artificial intelligence. In this lesson you will meet the core building blocks that every AI practitioner uses daily: variables, data types, lists, dictionaries, loops, functions, and essential libraries. You will understand enough to read AI code without fear and write small programs that actually run — all in a free browser tool, no installation required.","outcomes":["Write and run Python code using variables, lists, and dictionaries","Use loops and conditionals to process data","Define and call functions with docstrings","Import and use external libraries like NumPy"],"narration":"Welcome to Introduction to Python for AI. By the end of this lesson, you'll be able to write and understand basic Python code used in AI workflows. Let's get started."},{"kind":"content","heading":"Why Python for AI?","body":"Python dominates AI because of its clear syntax and vast ecosystem. The most important tools — NumPy (fast maths on arrays), pandas (data tables), and AI SDKs like openai and anthropic — are all Python-first.\n\nAnalogy: Think of Python as a well-stocked kitchen. The language itself is the stove and worktop; the libraries are pre-made sauces and spice mixes. You rarely cook from scratch — you combine trusted ingredients.\n\nTo use external libraries, you first need to install them. Python comes with a package manager called pip. For example, to install NumPy, run:\n
bash\npip install numpy\n\nIt's also good practice to use virtual environments to keep project dependencies separate. Create one with:\nbash\npython -m venv myenv\nsource myenv/bin/activate # On Windows: myenv\\Scripts\\activate\n\nThen install packages inside it. This avoids conflicts between projects.","callout":{"variant":"note","title":"Installing Packages","text":"NumPy is not built into Python. You must install it with pip install numpy. Always use a virtual environment to manage dependencies."},"narration":"Python is the go-to language for AI because of its readability and powerful libraries. To use libraries like NumPy, you need to install them with pip, and it's best to work inside a virtual environment."},{"kind":"content","heading":"Variables and Data Types","body":"A variable is a labelled box that stores a value. Python figures out the type automatically.\n\npython\nname = \"Amara\" # str (text)\nage = 29 # int (whole number)\nhourly_rate = 12.5 # float (decimal)\nis_student = True # bool (True/False)\n\nprint(f\"{name} is {age} years old.\")\n\n\nThe f before the string makes it an f-string, letting you embed variables inside {}. This is the standard way to build text in modern Python.","narration":"Variables store data. Python automatically determines the type. F-strings make it easy to combine text and variables."},{"kind":"content","heading":"Collections: Lists and Dictionaries","body":"A list is an ordered, changeable collection. A dictionary stores key-value pairs — like a labelled record.\n\npython\nskills = [\"Python\", \"data\", \"AI\"]\nskills.append(\"prompting\")\nprint(skills[0]) # \"Python\"\n\nlearner = {\n \"name\": \"Wei\",\n \"country\": \"Singapore\",\n \"level\": \"beginner\",\n}\nprint(learner[\"country\"]) # \"Singapore\"\n\n\nDictionaries are crucial in AI because API responses and configuration data often come as JSON, which Python represents as dictionaries.","narration":"Lists hold ordered items; dictionaries store labelled data. Both are essential for handling AI data."},{"kind":"content","heading":"Loops, Conditionals, and Functions","body":"Loops repeat actions; if-statements make decisions. Functions are reusable blocks of code.\n\npython\nscores = [85, 42, 91, 60]\nfor score in scores:\n if score >= 60:\n print(f\"{score}: pass\")\n else:\n print(f\"{score}: revise\")\n\ndef average(numbers):\n \"\"\"Return the mean of a list of numbers.\"\"\"\n return sum(numbers) / len(numbers)\n\nprint(average(scores)) # 69.5\n\n\nIndentation (4 spaces) defines code blocks in Python. Docstrings (triple quotes) describe functions — writing clear docstrings helps AI assistants generate better code.","callout":{"variant":"tip","title":"Indentation Matters","text":"Python uses indentation to group statements. Always use 4 spaces consistently. Mixing tabs and spaces causes errors."},"narration":"Loops and conditionals control the flow of your program. Functions let you reuse code. Remember to indent properly."},{"kind":"content","heading":"Real-World Examples","body":"Here are three concrete examples of Python basics in action:\n\n- Market trader in Lagos, Nigeria: Uses a few lines of pandas to read a CSV of daily sales, group by product, and find top earners — replacing an hour of manual work.\n- Nursing student in the Philippines: Writes a Python function to convert temperatures between Celsius and Fahrenheit, then loops over a list to flag unsafe readings.\n- Startup in Berlin, Germany: Uses Python with the anthropic SDK to send customer emails to Claude for summarisation, storing results in dictionaries before saving to a database.\n\nThese examples show that the basics you're learning are exactly what professionals use daily.","narration":"Python basics are used everywhere — from market analysis in Lagos to patient monitoring in the Philippines to AI integration in Berlin."},{"kind":"quiz","heading":"Check Your Understanding","questions":[{"question":"Which of the following is the correct way to install the NumPy library?","options":["import numpy","pip install numpy","numpy install","install numpy"],"questionId":"cmrf73k9d0021pd276fenw6uc"},{"question":"What will the following code print?\n\npython\nskills = [\"Python\", \"data\", \"AI\"]\nprint(skills[1])\n","options":["Python","data","AI","Error"],"questionId":"cmrf73k9d0022pd27gzwary76"},{"question":"Which of the following is NOT a valid Python data type?","options":["str","int","float","char"],"questionId":"cmrf73k9d0023pd27od3tnjvr"}],"narration":"Let's test your understanding with a quick quiz.","quizId":"qz_cmk7lkesk002bg4p8qvex5fqu"},{"kind":"summary","heading":"Key Takeaways","takeaways":["Python is the leading language for AI due to its readability and ecosystem.","Variables store data; Python infers types (str, int, float, bool).","Lists hold ordered items; dictionaries store key-value pairs.","Loops and conditionals control program flow; indentation defines blocks.","Functions encapsulate reusable logic; docstrings describe them.","Install external libraries with pip and use virtual environments to manage dependencies.","NumPy provides fast array operations and is the foundation for AI math."],"narration":"You've learned the Python basics essential for AI. Practice by writing small programs and exploring libraries. Keep coding!"}]}