Learning goals
Create meaningful names, choose basic types, convert numeric text, and understand mutable values.
Explanation
A variable is a name bound to an object. Assignment uses =. Python determines an object's type at runtime; you do not need to declare a type before assigning a value. Names are case sensitive. Use letters, digits, and underscores, without starting with a digit. Do not use keywords such as class.
Integers represent whole numbers; floats represent approximate real numbers; strings represent text; booleans represent True or False. Lists, tuples, sets, and dictionaries hold collections. None represents the absence of a value. A list can change in place, but a string cannot.
Example 1 A student record
student_name = "Ayesha"
age = 18
average_score = 87.5
is_enrolled = True
next_exam = None
print(student_name, age, average_score, is_enrolled)
print(type(age).__name__)
print(type(average_score).__name__)
print(next_exam is None)Expected output:
Ayesha 18 87.5 True
int
float
Truetype identifies an object's type; __name__ supplies its readable name. Use isinstance(age, int) when checking whether a value belongs to a type or its subclasses.
Example 2 Conversion and mutability
quantity_text = "3"
unit_price = 12.5
quantity = int(quantity_text)
print(quantity * unit_price)
print(bool("False"), bool(""))
scores = [70, 80]
other_name = scores
other_name.append(90)
print(scores)Expected output:
37.5
True False
[70, 80, 90]bool("False") is True because any nonempty string is truthy. It does not parse an English boolean. Both scores and other_name refer to the same list, so mutation through either name is visible through both. A separate shallow list copy can be made with scores.copy().
Common mistakes
int("3.5") fails; use float for decimal text. int(3.9) truncates toward zero rather than rounding. Floating point values are approximate, so use decimal arithmetic or integer minor units when exact monetary calculations are required.
Student tasks
1. Create a product with a name, integer quantity, decimal price, and boolean availability. Print each value and its type.
2. Convert "12" and "8" to integers and add them. The result must be 20 rather than "128".
3. Create a list, copy it, modify the copy, and show that the original stays unchanged. Use a flat list for this exercise.
4. Explain why bool("0") is True and bool(0) is False.
Completion check: meaningful snake_case names, correct types, and outputs for all four tasks.