Variables and Data Types Checkpoint
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Before moving to operators, test your understanding of Python variables, dynamic typing, and numeric conversions.
Equality (==) versus identity (is)
Identity is whether two names point to the very same object. The is operator tests identity; == tests whether the values are equal.
== asks if two values are equal. is asks if two names point to the same object. For None, always use is.
amount = 0refund = Noneprint(amount == 0)print(refund is None)print(amount is None)Example 1 · Types after rebinding
value = "100"print(type(value))value = int(value)print(type(value))value = value + 0.5print(value)print(type(value))<class 'str'> <class 'int'> 100.5 <class 'float'>
What this code does
- The same name points to a str, then an int, then a float.
- Each assignment creates a new object; the name has no type of its own.
amount == 0 is True. refund is None is True. amount is None is False, because 0 is a number, not "no value".
Rebinding does not change other names
Assigning to a name binds it to a new object. Other names that pointed to the old object are not touched.
stock = 40backup = stockstock = 35print(stock)print(backup)Example 2 · Four types in one report
user_id = 101account_balance = 4500.75is_verified = Trueprofile_notes = None
print(f"User {user_id}: Balance = {account_balance}")print(f"Verified: {is_verified}, Notes: {profile_notes}")User 101: Balance = 4500.75 Verified: True, Notes: None
What this code does
- Combines
int,float,boolandNoneType. - An f-string puts each value into the text.
Example 3 · Convert, calculate, round
quantity = int("250")unit_price = 14.5total = quantity * unit_priceprint(total)print(type(total))print(round(total / 7, 2))3625.0 <class 'float'> 517.86
What this code does
int("250")turns text into a number.- An int times a float gives a float.
round(..., 2)keeps two decimal places.
stock prints 35 and backup prints 40, because backup still points to the old object.
Finish up
Mark complete & continue
Next: Arithmetic Operators