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:

ExampleTypeOrdinary role
4inta whole number
20.5floata number with a fractional part
"4"strtext

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?

Choose one

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".

Not attempted
Review

Not marked done.

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.

Command/Ctrl + Enter. Python runs in your browser.

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?

Choose one

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.

Not attempted
Review

Not marked done.

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

Command/Ctrl + Enter. Python runs in your browser.

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)
Not attempted
Review

Not marked done.

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.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.