Here's a truth that trips up every beginner: data almost never arrives as the type you need. A quantity read from a file is the text '42', not the number 42. Before you can add, average, or compare it, you have to convert it. This short lesson is the bridge between raw input and real analysis.
What you'll learn
What
Type conversion (casting) turns a value of one type into another: int() to whole numbers, float() to decimals, str() to text, and bool() to True/False. Some conversions Python does for you (implicit); most you request explicitly.
Why
CSV files, web forms, APIs, and input() all hand you strings. If you try to sum a column that's secretly text, Python either concatenates it or raises a TypeError. Converting correctly — and safely — is the first real step of every cleaning pipeline.
Where it's used
Parsing numbers out of CSV cells, reading form fields, casting API JSON values, preparing columns for arithmetic, and testing whether a value is 'present' with truthiness.
Where this runs in production
A merchant's product CSV stores stock counts as text. Each cell is passed through int() before totals or reorder alerts can be computed.
JSON responses often deliver numbers as strings. float() converts '17.4' into 17.4 so the app can compare it against a threshold.
A respondent types their age into a text box. The backend attempts int() and treats a failure as invalid input rather than crashing.
When you write 3 + 2.5, Python promotes the int 3 to a float and returns 5.5. It only does this for compatible numeric types, and only to avoid losing information (int → float, never the reverse). It will NEVER guess that '3' + 2 should be 5 — mixing str and int raises a TypeError.
Converting a value is like exchanging currency: str(42) turns 42 into the '42' banknote of a different denomination, but it's the same underlying amount. What you can't do is exchange something that isn't money — int('hello') fails because 'hello' was never a number to begin with.
int(x) → whole number (int('42') is 42; int(3.9) TRUNCATES to 3, it does not round). float(x) → decimal number (float('3.14') is 3.14). str(x) → text ('str(42)' is '42', needed for f-strings and joining). bool(x) → True/False based on truthiness.
Falsy values (bool() → False): 0, 0.0, '' (empty string), [] (empty list), None, and False itself. Everything else is truthy → True. So bool(0) is False but bool(-5) is True; bool('') is False but bool('0') is True (a non-empty string!). This is why 'if value:' quietly means 'if value is present and non-empty'.
Think of bool() as peering into a box. An empty box — 0, '', [], None — reads as False, 'nothing here'. A box with anything in it reads as True. Note the trap: the string '0' isn't an empty box; it contains the character '0', so it's truthy.
int(3.99) is 3, not 4. Python chops off the decimal part rather than rounding to nearest. If you need proper rounding, use round(3.99) which gives 4. Confusing the two silently understates totals — a real bug in financial and reporting code.
Click each stage to follow a quantity that arrives as text and becomes a number ready for arithmetic — with the failure branch shown.
You want to build the label 'Order #7' from an order number.
An integer.
Your task
A checkout system receives all values as strings from a web form. Convert each to the right type and produce the receipt. Use the converters — do not retype the numbers as literals.
Total: $13.50 Member: False
Write your solution in the editor on the right, then hit Run.
What does int(7.8) return?
Which of these values is truthy?
What happens when you run int('3.14')?
What is the correct conversion for each value?
A form sends age as the text '34'. Convert it to a number and print whether the person is an adult (age >= 18) as a boolean. Use age_text = '34'. Expected output: True
A temperature arrives as the string '98.6'. Convert it to a float, then print it truncated to a whole number using int(). Use reading = '98.6'. Expected output: 98
What's the difference between implicit and explicit type conversion in Python?
Implicit conversion is done automatically by Python, mainly to avoid losing information when mixing numeric types — write 3 + 2.5 and Python promotes the int to a float, giving 5.5, without you asking. Explicit conversion is when you call a constructor like int(), float(), str(), or bool() to demand a specific type. Python deliberately keeps implicit conversion narrow: it will widen int to float, but it will never guess that the string '3' should become the number 3, because that could hide bugs. So anything crossing the str/number boundary — which is nearly all real-world input — you convert explicitly.
Why does so much real-world data arrive as strings, and what problems does that cause?
Text is the universal transport format: CSV files are plain text, HTML form fields submit strings, input() returns a string, and even JSON from APIs frequently encodes numbers as quoted strings. So a quantity or price shows up as '42' or '19.99', not as a number. The problems are twofold. First, arithmetic misbehaves — '42' + 5 raises a TypeError, and '42' + '5' concatenates to '425' instead of adding. Second, comparisons are wrong — '9' > '10' is True because strings compare character by character. The fix is to convert every incoming value to its intended type at the boundary of your program, before any calculation or comparison touches it.
Explain truthiness in Python. Which values are falsy, and why is bool('0') a common trap?
Every Python value is either truthy or falsy in a boolean context, which is what lets you write 'if value:' instead of an explicit comparison. The falsy values are a short list: 0, 0.0, '' (empty string), empty containers like [] and {}, None, and False itself. Everything else is truthy. The trap is bool('0'): the string '0' is not empty — it contains one character — so it's truthy, even though the number 0 is falsy. This bites people who read a '0' from a file and expect an 'if' check to treat it as false. The lesson is that truthiness tests presence and emptiness, not numeric value, so if you care about the actual number you must convert with int() or float() first and compare explicitly.
Common Mistakes to Avoid
1) Expecting int() to round — int(3.9) is 3, not 4; use round() when you need nearest. 2) Calling int() on a decimal string — int('3.14') raises a ValueError; use float() for anything with a decimal point. 3) Doing maths on unconverted input — '42' + 5 is a TypeError, and '42' + '5' concatenates to '425' instead of adding. 4) Comparing numeric strings — '9' > '10' is True because strings compare character by character; convert first. 5) Confusing the string '0' with the number 0 — bool('0') is True (non-empty), while bool(0) is False.
Ask the AI Tutor
Try these prompts in the AI Tutor panel: • 'ELI5: why does data from a CSV come in as strings?' • 'Quiz me on which of ten values are truthy or falsy.' • 'Show me the difference between int(3.9) and round(3.9) with output.' • 'Give me five messy string values and let me pick the right converter for each.' • 'Interview mode: ask me to explain implicit vs explicit conversion and grade my answer.'
Glossary
Type conversion (casting) — changing a value from one type to another. Implicit conversion — automatic promotion Python does when mixing numeric types (int → float). Explicit conversion — a conversion you request by calling int(), float(), str(), or bool(). int() — converts to a whole number, truncating any decimal. float() — converts to a decimal number. str() — converts any value to its text form. bool() — converts a value to True or False by its truthiness. Truthy — a value that counts as True (any non-empty, non-zero value). Falsy — a value that counts as False (0, 0.0, '', [], None, False). ValueError — the error raised when a string can't be parsed into the requested number.
Recommended Resources
• Docs: the 'Built-in Functions' reference entries for int(), float(), str(), and bool() spell out exactly what each accepts. • Read: 'Truth Value Testing' in the standard-types docs lists every falsy value from the source. • Practice: take five messy strings ('42', '3.14', 'N/A', '', '0') and predict what int(), float(), and bool() do to each, then verify in a REPL. • Next in DSM: you can convert single values — next you'll hold and compute over many values at once in Python Lists vs. NumPy Arrays.
Recap
✓ Implicit conversion is automatic numeric widening; explicit conversion is a cast you call. ✓ int(), float(), str(), and bool() are the four everyday converters. ✓ int() truncates decimals — it does not round — and int('3.14') raises a ValueError. ✓ Real data arrives as strings, so convert at the boundary before any maths or comparison. ✓ Falsy values are 0, 0.0, '', [], None, and False; everything else is truthy — and '0' is truthy. ✓ Safe conversion means anticipating which values can't be parsed. Next up: Python Lists vs. NumPy Arrays. You can clean and convert single values — next you'll scale up to whole collections and the arrays data science runs on.
Run your code to see the output here.