1.2.5Introduction to Programming (Python)

Type checking with `type()`, type conversion `int()`, `float()`, `str()`

1,775 words8 min readdifficulty · medium2 backlinks

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)

Figure — Type checking with `type()`, type conversion `int()`, `float()`, `str()`

3. Worked examples


4. Common mistakes (Steel-man + fix)


5. Active recall

Recall Click to test yourself
  1. What does type("7") print? → <class 'str'>
  2. What is int(-3.7)? → -3 (toward zero)
  3. What does input() always return? → a str
  4. Why does "3"+"4" give "34"? → + on strings = join, not add
  5. How to safely convert "3.9" to an int? → int(float("3.9"))
  6. type() vs isinstance() — 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() returns str, so conversion is essential
  • Operators and Expressions+ meaning depends on type
  • Booleans and Truthinessbool is a subclass of int
  • Errors and ExceptionsValueError / TypeError from bad conversions
  • Floating Point Representation — why float is approximate
What does type(x) return?
The class/type object of 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?
3int() 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?
A str, even when the user types digits.
Why use str(95) in "score: " + str(95)?
You can't concatenate 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?
No — it returns a new value; you must reassign with x = int(x).

Concept Map

inspected by

converted by

converted by

converted by

prefer for deciding

from float

constructor

constructor

constructor, rarely fails

governs

same + differs by type

Every value has a type label

type() reads the label

isinstance() for deciding

int()

float()

str()

Builds a new value

Truncates toward zero

Type decides legal operations

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.

Test yourself — Introduction to Programming (Python)

Connections