Inspect and Convert Values
Compare values that display alike but behave differently. Use type() as evidence, trace where a surprising value came from, and convert text only when its meaning supports the conversion.
Two values can look alike when printed and still behave differently:
The output is:
5
41
The first line adds numbers. The second joins strings.
Inspect a Value's Type
Every Python value has a type that describes what kind of value it is and which operations it supports. For the values used in this chapter, the common types are:
| Example | Type | Ordinary role |
|---|---|---|
4 | int | a whole number |
20.5 | float | a number with a fractional part |
"4" | str | text |
The built-in function type() reports a value's type:
<class 'str'>
Use type() when behavior is surprising. It supplies evidence; it does not
change or repair the value.
Q1. Explain the surprising result
Why does "18" + "2" produce "182" rather than 20?
Select one choice, then check.
HintInspect both operands
Both values are enclosed in quotation marks.
SolutionString addition joins text
"18" and "2" are strings. Joining their characters produces "182".
A Name Can Refer to Different Types
Types belong to values, not permanently to names:
After reassignment, count refers to a string. When an operation fails or
produces an unexpected result, trace the assignments that supplied its
operands rather than assuming that a familiar name still refers to the old
kind of value.
Convert Only When the Meaning Supports It
Suppose a count arrives as the text "4", perhaps because it was read from a
text field. If that text genuinely represents a whole-number count, int()
can produce the integer 4:
The functions int(), float(), and str() create integer, floating-point,
and string values when the conversion is valid:
Conversion is not a general cure. int("four") fails because the text does
not represent an integer. Converting every value to text would make arithmetic
impossible. First decide what the value means; then choose the type that fits
that meaning.
Inspect before converting
Run the program once, then change count_text to another string that represents a whole number.
Ready to run.
Q2. Choose a purposeful conversion
reading_text contains "21.5". Which instruction prepares it for numerical
calculation without discarding the fractional part?
Select one choice, then check.
HintThe text contains a decimal point
Choose the numerical type used for values such as 21.5.
SolutionConvert to float
float(reading_text) produces the floating-point value 21.5, which can take
part in arithmetic.
Q3. Repair a text and number mismatch
The program should display next count: 5. Repair the value at the point where
its meaning becomes clear.
Editable Python
Ready to run.
HintConvert the count
Change the second line so that count refers to an integer made from
count_text.
SolutionConvert where the count enters the calculation
count = int(count_text)
A displayed value does not reveal its type. Inspect unexpected operands, trace where they were assigned, and convert only when the value's meaning justifies the new type. The next chapter uses these values to make choices and repeat work.