Python essentials for data work
Python essentials for data work
Let's make a tiny class list before opening a machine learning library. Python can store each student's name and score together, then repeat an operation for every student. These ordinary programming skills let you inspect data and express checks that a model cannot decide for you.
A list holds an ordered collection. A dictionary associates names called keys with values. A function is a named piece of reusable code. We will use those three ideas to apply an explicit pass rule, so you can recognize the difference between a rule written by a person and a relationship learned from data.
Read the containers and the rule
The square brackets around the two dictionaries create a list. Each dictionary uses keys such as name and score. Writing student["score"] retrieves a value by its key; it does not ask Python to infer a meaning from the student's name.
The def statement defines passed. Its comparison score >= 75 returns a Boolean, either True or False. The for loop assigns one dictionary at a time to student and calls the function. Indentation identifies the statements belonging to the function and loop.
What you’ll learn
- Read lists and dictionaries used to describe observations.
- Trace a loop and function call.
- Separate explicit programming rules from fitted models.
Read the containers and the rule
List
Keep the two observations in order.
Diya, then SairaFunction
Compare each score with the stated pass mark.
score >= 75Loop result
The same rule runs for each student.
False, then True
Apply a pass rule to Diya and Saira
Prepare and run
Use Python in the course environment. The input values are included in this program; no separate data file is needed.
Python · complete runnable example
students = [{"name": "Diya", "score": 72}, {"name": "Saira", "score": 80}]
def passed(score):
return score >= 75
for student in students:
print(student["name"], passed(student["score"]))Expected output
Diya False
Saira TrueFollow the result
Diya's score is 72, so the comparison with 75 is false. Saira's score is 80, so it is true. The program prints each name and its Boolean result. The order matches the list order, and no model is trained: you supplied the decision rule directly.
Change the pass mark
Your task: If the pass mark becomes 70, what are the two printed decisions?
Python · code for this task
students = [{"name": "Diya", "score": 72}, {"name": "Saira", "score": 80}]
def passed(score):
return score >= 75
for student in students:
print(student["name"], passed(student["score"]))Worked answer
Both become True because both scores are at least 70. Change the comparison to return score >= 70 inside the function. You do not need to alter the loop or duplicate the rule for each student.
Check types before calculating
The number 72 and the text "72" are different values in Python. Data read from a file may need conversion before numerical operations. Use type(value) while diagnosing a surprising result, and validate a field before converting it.
Missing dictionary keys raise an error when accessed directly. Silently inventing a score would hide a data problem. In later lessons, we will distinguish missing observations from actual zero scores and choose an explicit policy.
Check your understanding
Read the containers and the rule: what to remember
Lists organize records, dictionaries name their fields and functions express reusable operations. The pass rule in this example is explicitly programmed; storing data and looping over it does not train a model.
Read further: official supporting documentation.
Reaching the recap and completing the quizzes are separate milestones.