Type checking with `type()`, type conversion `int()`, `float()`, `str()`
1. Reading the label: type()
2. Changing the label: conversion functions
These are constructor functions: they don't modify the original, they build a new value.
HOW each one behaves (the rules that bite you)

3. Worked examples
4. Common mistakes (Steel-man + fix)
5. Active recall
Recall Click to test yourself
- What does
type("7")print? →<class 'str'> - What is
int(-3.7)? →-3(toward zero) - What does
input()always return? → astr - Why does
"3"+"4"give"34"? →+on strings = join, not add - How to safely convert
"3.9"to an int? →int(float("3.9")) type()vsisinstance()— which for deciding in code? →isinstance()
Recall Feynman: explain to a 12-year-old
Imagine every box of stuff has a sticker on it: "WHOLE NUMBERS", "DECIMALS", or "WORDS".
type() is you reading the sticker out loud. You can't change what's in the box by reading the sticker.
int(), float(), str() are like copying the contents into a new box with a new sticker — sometimes you lose a bit (decimals get chopped when you copy into a "WHOLE NUMBERS" box), and sometimes the copy refuses (you can't put the word "apple" into a numbers box). The original box stays untouched.
6. Connections
- Variables and Assignment — conversions usually paired with
x = int(x) - Input and Output (input, print) —
input()returnsstr, so conversion is essential - Operators and Expressions —
+meaning depends on type - Booleans and Truthiness —
boolis a subclass ofint - Errors and Exceptions —
ValueError/TypeErrorfrom bad conversions - Floating Point Representation — why
floatis approximate
What does type(x) return?
x, e.g. <class 'int'>; it only reads, never changes x.Result and type of type(7.0)?
<class 'float'>.What does int(3.9) give and why?
3 — int() truncates toward zero (chops the fractional part), it does NOT round.What does int(-3.9) give?
-3 (truncation is toward zero, not toward negative infinity).Why does int("3.9") fail?
int() on a string needs a clean integer string; the dot makes it a ValueError. Use int(float("3.9")).What does "3" + "4" evaluate to?
"34" — + on strings concatenates (joins) rather than adds.How do you numerically add the strings "3" and "4"?
int("3") + int("4") → 7.What type does input() always return?
str, even when the user types digits.Why use str(95) in "score: " + str(95)?
str + int; str() converts the number to text first.int(True) and int(False)?
1 and 0 respectively (bool is a subclass of int).type() vs isinstance() — when to use which?
type() to inspect/print the exact type; isinstance(x, T) to decide in code (also matches subclasses).Does int(x) modify x?
x = int(x).Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, Python mein har value ke upar ek invisible sticker laga hota hai jo batata hai ki ye cheez kya hai — int (pura number), float (decimal wala), ya str (text). Is sticker ko hi type kehte hain. type(x) ka kaam sirf itna hai ki sticker padh ke bata de — <class 'int'> jaisa. Ye kuch change nahi karta, sirf read karta hai.
Ab int(), float(), str() — ye converter hain. Ye original ko nahi badalte, balki ek naya value banate hain naye sticker ke saath. Sabse important baat yaad rakho: int() decimal ko round nahi karta, chop karta hai zero ki taraf. Matlab int(2.99) ka answer 2 hai, 3 nahi! Agar rounding chahiye to round() use karo. Aur int("3.9") directly fail ho jayega (ValueError), kyunki string mein dot allowed nahi — pehle float() se jao: int(float("3.9")).
Sabse common galti exam aur real code dono mein: input() hamesha string deta hai, chahe user ne numbers type kiye ho. Isliye math karne se pehle int(input()) ya float(input()) karna padta hai. Aur jab text ke saath number jodna ho — jaise "Score: " + 95 — to ye crash karega; pehle str(95) karo. Yaad rakho: "3" + "4" matlab "34" (jodna text ka), aur 3 + 4 matlab 7 (add karna). Same +, par type alag to behaviour alag. Type ka dhyan rakhoge to half bugs apne aap khatam.