1.2.5 · HinglishIntroduction to Programming (Python)

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

1,696 words8 min readRead in English

1.2.5 · Coding › Introduction to Programming (Python)


1. Label padhna: type()


2. Label badalna: conversion functions

Yeh constructor functions hain: yeh original ko modify nahi karte, yeh nai value build karte hain.

HAR EK KAISA BEHAVE KARTA HAI (woh rules jo tumhe kaatenge)

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

3. Worked examples


4. Common mistakes (Steel-man + fix)


5. Active recall

Recall Khud test karne ke liye click karo
  1. type("7") kya print karta hai? → <class 'str'>
  2. int(-3.7) kya hai? → -3 (toward zero)
  3. input() hamesha kya return karta hai? → ek str
  4. "3"+"4" kyun "34" deta hai? → strings par + = join, add nahi
  5. "3.9" ko safely int mein kaise convert karein? → int(float("3.9"))
  6. type() vs isinstance() — code mein decide karne ke liye kaun sa? → isinstance()
Recall Feynman: ek 12-saal ke bachchey ko explain karo

Imagine karo ki cheezón ke har box par ek sticker hai: "WHOLE NUMBERS", "DECIMALS", ya "WORDS". type() woh hai jab tum sticker oonchi awaaz mein padhte ho. Sticker padhne se box ke andar kya hai woh nahi badalta. int(), float(), str() jaise hain copy karna — contents ko ek naye box mein naye sticker ke saath — kabhi kabhi kuch kho jaata hai (decimals chop ho jaate hain jab "WHOLE NUMBERS" box mein copy karte hain), aur kabhi kabhi copy refuse kar deta hai (tum "apple" word ko numbers box mein nahi daal sakte). Original box untouched rehta hai.


6. Connections

  • Variables and Assignment — conversions usually x = int(x) ke saath paired hote hain
  • Input and Output (input, print)input() str return karta hai, toh conversion zaroori hai
  • Operators and Expressions+ ka matlab type par depend karta hai
  • Booleans and Truthinessbool ek subclass hai int ka
  • Errors and ExceptionsValueError / TypeError buri conversions se
  • Floating Point Representation — kyun float approximate hota hai
type(x) kya return karta hai?
x ka class/type object, jaise <class 'int'>; yeh sirf padhta hai, kabhi x nahi badalta.
type(7.0) ka result aur type?
<class 'float'>.
int(3.9) kya deta hai aur kyun?
3int() truncates toward zero (fractional part kaatta hai), yeh round NAHI karta.
int(-3.9) kya deta hai?
-3 (truncation toward zero hota hai, negative infinity ki taraf nahi).
int("3.9") kyun fail karta hai?
string par int() ko ek clean integer string chahiye; dot ise ValueError banata hai. int(float("3.9")) use karo.
"3" + "4" kya evaluate hota hai?
"34" — strings par + concatenate (join) karta hai, add nahi karta.
Strings "3" aur "4" ko numerically kaise add karte hain?
int("3") + int("4")7.
input() hamesha kaun sa type return karta hai?
Ek str, chahe user ne digits type ki hon.
"score: " + str(95) mein str(95) kyun use karte hain?
Tum str + int concatenate nahi kar sakte; str() number ko pehle text mein convert karta hai.
int(True) aur int(False)?
Respectively 1 aur 0 (bool int ka subclass hai).
type() vs isinstance() — kaun sa kab use karna hai?
type() exact type inspect/print karne ke liye; isinstance(x, T) code mein decide karne ke liye (subclasses bhi match karta hai).
Kya int(x) x ko modify karta hai?
Nahi — yeh ek nai value return karta hai; tumhe x = int(x) se reassign karna hoga.

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