Basic Python (AI-powered) · Lesson 2 of 3
Basic Python (AI-powered)
Use Python values, collections, control flow, functions, files, and AI-assisted development as a base for analysis.
Lesson
1. Introduction
Python is a general-purpose language whose data ecosystem makes repeatable analysis possible. Compared with manual spreadsheet steps, a script can explain exactly how values were transformed and can rerun the same work when new records arrive.
2. Core concepts
Variables refer to values; lists preserve ordered items, dictionaries map keys to values, and functions package reusable work. pandas and NumPy build on these ideas with column-oriented tables and arrays. Learn the language foundations before trusting a generated script or a large library call.
3. Topics covered in this module
- Variables, strings, numbers, booleans, and type conversion
- Lists, dictionaries, loops, conditions, functions, and exceptions
- Reading CSV and JSON with explicit encoding and paths
- Notebook cells, packages, virtual environments, and AI-generated code review
4. Syntax and how to read it
def average(values: list[float]) -> float:
if not values:
raise ValueError('values cannot be empty')
return sum(values) / len(values)
print(average([10.0, 20.0]))The function accepts a list and returns a number. The explicit empty-input check prevents division by zero and communicates what the function expects.
5. Practical workflow
Start with a small input, inspect its type and shape, write one transformation as a function, and compare the output with a hand-checked case. Use an isolated environment and record package requirements for repeatability.
6. Common mistakes
- Assuming input text is numeric without converting and validating it.
- Running AI-suggested code on sensitive files without understanding paths, dependencies, and side effects.
7. Best practices
- Keep raw data unchanged and write transformations as small functions.
- Use virtual environments and tests for scripts that will be reused.
8. Practice task
Write a function that accepts a list of numbers and returns only the positive values.
9. One possible solution
def positives(values: list[float]) -> list[float]:
return [value for value in values if value > 0]10. Summary
Use Python values, collections, control flow, functions, files, and AI-assisted development as a base for analysis. Keep raw data unchanged and write transformations as small functions. Use virtual environments and tests for scripts that will be reused.
11. What comes next
The next module, SQL for Data Analytics, builds on this foundation and applies it to a new part of the analytics workflow.
Visual guide
A reliable Basic Python (AI-powered) workflow
-
Step 01
Frame
Define the question, input scope, and intended output.
-
Step 02
Work
Apply the method and validate assumptions as you go.
-
Step 03
Check
Compare results with known cases and communicate limitations.
Code examples
prices = [12.5, 20.0, 7.5]
with_tax = [round(price * 1.08, 2) for price in prices]
print(with_tax)
[13.5, 21.6, 8.1]
import csv
with open('orders.csv', newline='', encoding='utf-8') as file:
rows = list(csv.DictReader(file))
print(len(rows))
Prints the number of data records.
Key points
- Use Python values, collections, control flow, functions, files, and AI-assisted development as a base for analysis.
- Keep raw data unchanged and write transformations as small functions.
- Use virtual environments and tests for scripts that will be reused.
Check your understanding
Practice
Your learning progress