Each run starts with fresh data. Download any exported files before another run. Your written answers and code drafts are saved on this device; export your work to keep a backup.
M00C01 · Explore · predict · practice
Names hold values
A variable is a name associated with a value. The equals sign assigns a value; two equals signs compare values. Quotation marks make text, while an unquoted number can take part in arithmetic. print() displays a value so you can inspect it.
Read the example
name = "Amira"
age = 20
next_age = age + 1
is_twenty = age == 20
print(name)
print(next_age)
print(is_twenty)
print(type(age).__name__)
Verified example output · pandas 2.2.3
Amira
21
True
int
Watch for this. Python names are case-sensitive: age and Age are different names.
Your turn
Describe Bilal, age 21, using name and age. Calculate his age in three years in next_age. Store the comparison of his current age with 21 in is_twenty_one, then print the results.
For the code check, keep these result variable names: age, is_twenty_one, name, next_age. Different valid approaches are welcome.
Show a hint
Change the name and starting age, then use age + 3. Keep = for assignment and == for comparison.
Show one worked solution
name = "Bilal"
age = 21
next_age = age + 3
is_twenty_one = age == 21
print(name)
print(next_age)
print(is_twenty_one)
The results are Bilal, 24, and True. The original age remains 21.
Compare your explanations
Read. age = 20 assigns 20 to age. The expression age == 20 compares age with 20 and produces True.
Predict. next_age prints 21. age stays 20 because the calculated value was assigned to a different name.
Explain. "20" is text; 20 is a whole number. Their types determine which operations make sense.
One quick check
What does age == 20 do?
What you should be able to explain. I can distinguish a value, a variable, an assignment, and a comparison.
M00C02 · Explore · predict · practice
A list keeps values in order
Square brackets create a list. Each value has a position, beginning at 0. len() counts the values, and a slice selects a range of positions. A slice includes its start position and excludes its stop position.
result contains 72 and 91. The original list now has four values, ending in 65.
Compare your explanations
Read. scores is the whole list. scores[0] retrieves the first value, 88.
Predict. Two values: 88 and 72. Position 2 is excluded from the slice.
Explain. The list has three values at positions 0, 1, and 2. Position 3 does not exist, so Python raises IndexError.
One quick check
Which position refers to the first list value?
What you should be able to explain. I can count, select, and append list values.
M00C03 · Explore · predict · practice
Give a calculation a name
A function is a named set of instructions you can reuse. def starts a function definition, and the indented body states what it does. A parameter receives an input value when the function is called. return sends the result back so it can be saved or used in another calculation.
Read the example
def add_bonus(score, bonus):
return score + bonus
first = add_bonus(72, 3)
second = add_bonus(88, 2)
print(first)
print(second)
Verified example output · pandas 2.2.3
75
90
Watch for this. Indentation is part of Python syntax. Division also requires a nonzero denominator.
Your turn
Create a function called to_percentage(points, maximum) that returns points divided by maximum, multiplied by 100. Calculate 16 out of 20 into result and print it.
For the code check, keep these result variable names: result. Different valid approaches are welcome.
Show a hint
Keep the return line indented. For this exercise, maximum is positive.
The returned percentage is 80.0. The same function can be called with a different number of points and a different positive maximum.
Compare your explanations
Read. score receives 72 and bonus receives 3 for that call.
Predict. first contains 75; second contains 90.
Explain. A returned answer can be saved in a variable and used later. Printing alone displays it but does not return that answer to the caller.
One quick check
Which keyword sends the calculated answer back to the caller?
What you should be able to explain. I can read a small function, call it, and use its returned value.
M00C04 · Explore · predict · practice
Import a tool and read an error
import makes a module's tools available; as gives the module a shorter name. This example uses Python's built-in math module. The try/except block catches an expected conversion error so we can read its type and message, then continue. Read the error name and the failing value before changing code.
Read the example
import math as m
try:
number = float("nine")
except ValueError as error:
print(type(error).__name__ + ": " + str(error))
number = float("9")
result = m.sqrt(number)
print("Corrected result:", result)
Verified example output · pandas 2.2.3
ValueError: could not convert string to float: 'nine'
Corrected result: 3.0
Watch for this. Catching an error does not repair its cause. This example prints the expected error and then supplies a valid value explicitly.
Your turn
Import math as m, set text to "25", convert text to a number, and calculate its square root in result. Print the result.
For the code check, keep these result variable names: number, result, text. Different valid approaches are welcome.
Show a hint
Use float(text) for conversion and m.sqrt(number) for the calculation.
Show one worked solution
import math as m
text = "25"
number = float(text)
result = m.sqrt(number)
print(result)
The result is 5.0. Later, import pandas as pd follows the same alias pattern; pandas supplies the data tools used in the course.
Compare your explanations
Read. m is a short name for the math module. m.sqrt accesses its square-root function.
Predict. The word nine cannot be parsed as a numeric value by float. The digit string 9 can, producing 9.0.
Explain. Read the error type, its message, and the line that failed. Check the actual input and variable names rather than changing unrelated lines.
One quick check
What does as pd mean in import pandas as pd?
What you should be able to explain. I can recognize an import alias and use an error message to guide a correction.