Values, Objects, and Types
Unit ID: M02-U00 Estimated active time: 18-25 minutes
Why type matters
In Module 1, you used numbers and text. Now we will look more closely at what Python knows about each value.
Run:
print(type(25))
print(type(25.0))
print(type("25"))
print(type(True))
print(type(None))
You should see the types int, float, str, bool, and NoneType.
A value is a piece of information used by a program. In Python, values are objects. Every object has a type. The type helps determine which operations make sense.
Similar appearance, different meaning
These values look related but are different:
25
25.0
"25"
25is an integer: a whole number.25.0is a floating-point number: a number represented with a decimal form."25"is a string: text containing the characters2and5.
Predict the results:
print(25 + 5)
print("25" + "5")
The first result is 30. The second is 255 because + joins the two strings. Python did not make a mathematical mistake. It followed the operation defined for each type.
Inspect rather than guess
Use type() when the type is unclear:
course_hours = "32"
print(type(course_hours))
The name contains the word hours, but the value is still text. A clear name helps people; it does not change the Python type.
Type is not the complete story
Knowing the type does not prove that a value is correct. -10 is an integer, but it may be invalid for a course duration. "unknown" is a string, but it may not be an approved course status.
Later modules will add validation rules. For now, separate two questions:
- What type is this value?
- Is this value suitable for the task?
Guided practice
Predict each type before running:
course_name = "Python Foundations for AI"
module_number = 2
estimated_hours = 3.5
is_available = False
instructor_name = None
print(type(course_name))
print(type(module_number))
print(type(estimated_hours))
print(type(is_available))
print(type(instructor_name))
Explain what each value represents in the fictional course record.
Takeaway
Every Python value has a type, and type affects what operations mean. Inspect with type() instead of trusting appearance or a variable name. Next, we will trace how names refer to values and what reassignment changes.
