Understand the idea
A problem you can recognise
Your planner receives the characters "7" and "25". You know they describe seven days and twenty-five minutes, but the program has received text. How does it know that you intend multiplication rather than a label? It does not guess your intention. You must choose the values the calculation needs.
That is why conversion is worth learning before a larger input form. We can keep the input hard-coded and still investigate the exact boundary between text and numbers. By the end, a strange output such as repeated digits should become something you can explain, not a reason to try conversions at random.
Understand the idea
Understand the idea
A type describes the kind of value an operation receives, not how its output happens to look. The integer 12 and the string "12" print similar characters, but adding two integers calculates a sum while adding two strings joins text. Start by identifying literal syntax, then inspect type(value) when you need evidence. Do not fix every error by converting everything to a string: decide whether the next operation needs text or a number.
Conversion is an explicit request to construct a value of another type. int("12") parses suitable integer text; str(12) constructs text; float("2.5") parses a decimal representation. Conversion can fail. int("twelve") raises ValueError because those characters are not integer syntax. int(2.9) truncates towards zero; it does not round to the nearest integer. Keep those operations distinct.
For a study-duration calculation, a numeric total should stay numeric until it is formatted for display. Store converted inputs under clear names rather than repeatedly converting the same text inside a long expression. This makes the point where validation belongs easier to find. Floating-point values also have finite precision, so printed decimal examples are not a promise of exact decimal arithmetic for money.
See how it works
Example 1: Inspect the type
PYTHON exampleRead the code, then try a change.
Try this example
value = "12"
print(type(value).__name__)
print(type(12).__name__)
Expected output:
ReferenceCompare this with the explanation.
str
int
The first literal has quotation marks and produces a string. The second is an integer. name gives the type name without the class wrapper that type itself displays. The printed digits alone would not reveal this distinction.
See how it works
Example 2: Calculate after parsing
PYTHON exampleRead the code, then try a change.
Try this example
days_text = "5"
days = int(days_text)
print(days * 30)
print(days_text * 2)
Expected output:
ReferenceCompare this with the explanation.
150
55
Multiplying the parsed integer performs arithmetic. Multiplying the original string by an integer repeats the text. The original variable is not changed by constructing another value with int.
See how it works
Example 3: Format only at the boundary
PYTHON exampleRead the code, then try a change.
Try this example
minutes = 150
message = "Minutes: " + str(minutes)
print(message)
print(int(2.9))
Expected output:
ReferenceCompare this with the explanation.
Minutes: 150
2
The total becomes text only for concatenation. The last line demonstrates truncation, which differs from rounding. Use the operation whose documented behaviour matches the problem.
Understand the idea
Think through the important decisions
Follow days_text through the second example without changing it. Before int runs, the name refers to a string. The conversion constructs an integer value, which is assigned to another name. Neither the original text nor its name is edited by that conversion. This explains why the program can later demonstrate string repetition using the original input.
Now change the input to "07" and predict the numeric result and the original text separately. A number represents the quantity seven; the source text also contains a leading zero. Converting to an integer keeps the quantity, not that formatting decision. This distinction matters for identifiers such as roll numbers, where the characters may be the information you need to preserve.
Do not turn a roll number into an integer simply because it contains digits. Decide what operations the value should support. A duration will be multiplied; a roll number might instead be displayed or compared as an identifier. The appropriate type follows the job.
Check your understanding
Check the reasoning
Convert intentionally before an operation that requires a different kind of value.
A matching screenshot or output is one check, not proof that a program is correct for all inputs. Change one input, predict the new result and run again. If the result differs, find the first instruction where your prediction stops matching the program state. Keep that small case as a regression check when you fix the cause.
Spot the mistake
The mistake worth understanding
Conversion is not validation of the whole problem. int("-3") successfully produces an integer, but negative study days can still be invalid for your planner. Conversely, int("2.9") fails rather than silently truncating the decimal string. int(2.9) accepts a floating-point value and truncates it. Inspect the input type before assuming those two requests are equivalent.
Your turn
Independent practice
Start with days_text = "7" and minutes_text = "25". Parse both, calculate a numeric total, then print a labelled message. Also print the type name of the total. Explain why concatenating the original strings is not the calculation.
Your turn
Practice hint
Parse the two strings separately before multiplying. Keep the total numeric until you create the message.
Your turn
Try a different case
Explain where your planner stops treating the inputs as text. Then test an ordinary value, a leading zero and text that is not an integer. You have found both a conversion boundary and a reason to learn error handling later.