1.2.4 · HinglishIntroduction to Programming (Python)

Data types — int, float, str, bool, NoneType

1,620 words7 min readRead in English

1.2.4 · Coding › Introduction to Programming (Python)


Paanch core built-in types

Python se KAISE poochho: built-in type(x) type object return karta hai.

type(7)        # <class 'int'>
type(3.14)     # <class 'float'>
type("hi")     # <class 'str'>
type(True)     # <class 'bool'>
type(None)     # <class 'NoneType'>
Figure — Data types — int, float, str, bool, NoneType

Floats strange kyun behave karte hain? (Derive karo)

Ek float ki tarah store hota hai jahan mantissa binary hai. jaisi decimal store karne ke liye, Python ko ise powers of two ki sum ke roop mein likhna padta hai:

Lekin binary mein infinite repeating fraction hai . Hardware sirf 52 mantissa bits rakhta hai, isliye woh round karta hai. Wahi chhoti si rounding ki wajah se:

0.1 + 0.2 == 0.3      # False!
0.1 + 0.2             # 0.30000000000000004

Fix (KYA karo instead): floats ko kabhi == se test mat karo; closeness check karo:

abs((0.1 + 0.2) - 0.3) < 1e-9   # True

bool secretly ek int hai (Is surprise ko Steel-man karo)

isinstance(True, int)   # True
True + True             # 2
["a","b","c"][True]     # 'b'  (index 1)

Truthiness — har object if mein use ho sakta hai. Rule (KAUNSI cheezein "falsy" hain):


NoneType — "nothing" value

def greet(name):
    print("Hi", name)     # print karta hai lekin koi return statement nahi
 
x = greet("Sam")
x            # None
x is None    # True  (hamesha 'is' use karo, kabhi '==' nahi)

Type conversion (casting): KYA, KYUN, KAISE

Type name ko function ki tarah use karke types ke beech convert kar sakte ho. KYUN: users se aata input hamesha str hota hai; math karne ke liye convert karna zaroori hai.

int("42")      # 42      str -> int
int(3.9)       # 3       float -> int  (zero ki taraf TRUNCATE karta hai, rounding nahi!)
float("3.14")  # 3.14
str(99)        # '99'
bool("")       # False   bool(0) -> False, bool("x") -> True
int("3.5")     # ERROR: valid int string nahi hai

Common mistakes (Steel-man + fix)


Recall Ek 12-saal ke bachche ko explain karo (hidden — pehle khud try karo!)

Socho labeled boxes. int wala box sirf poore seb rakh sakta hai (3 seb). float box seb ke hisse bhi rakhta hai (2.5 seb) lekin yeh ek thoda fuzzy measuring cup hai — kabhi kabhi 0.3 ki jagah 0.30000001 bolta hai. str box mein beads hain ek dhaage par (letters). bool box mein ek light switch hai: ON (True=1) ya OFF (False=0). Aur None ek khali box hai jo tumne wahan on purpose rakha hai yeh kehne ke liye "abhi yahan kuch nahi rehta". Box ka label decide karta hai ki tum andar jo hai usse kya kar sakte ho.


Flashcards

type(x) kya return karta hai?
x ka type object (class), e.g. <class 'int'>.
Python mein 0.1 + 0.2 != 0.3 kyun hai?
Floats finite binary (IEEE-754) mein store hote hain; 0.1 ka koi exact binary form nahi, isliye rounding sum ko 0.30000000000000004 bana deti hai.
Do floats ko equality ke liye kaise compare karo?
Ek tolerance ke saath: abs(a-b) < 1e-9 (ya math.isclose), kabhi == nahi.
True aur False kaunsi integer values ke barabar hain?
True == 1, False == 0; bool int ki subclass hai.
int(3.99) kya deta hai aur kyun?
3int() zero ki taraf truncate karta hai, round nahi karta.
Python mein kaunsi values "falsy" hain?
0, 0.0, "", None, aur empty []/{}/().
NoneType kya hai aur kitne None objects exist karte hain?
None ka type, matlab "koi value nahi"; exactly ek singleton None hota hai.
x == None ki jagah x is None kyun use karo?
None ek unique singleton hai; is identity check karta hai aur overloaded __eq__ se fool nahi hota.
Bina return statement wala function kya return karta hai?
None.
int("3.5") error kyun deta hai lekin int(3.5) kaam karta hai?
"3.5" valid integer string literal nahi hai; int(float("3.5")) se convert karo.
"4" + "7" kya produce karta hai?
"47" — string concatenation, addition nahi.

Connections

  • Variables and assignment — types values se attached hote hain, names bas unki taraf point karte hain.
  • Operators and operator overloading+ ka matlab int ke liye add karna hai, str ke liye concatenate karna.
  • input() and user I/O — input hamesha str hota hai; casting zaroori hai.
  • IEEE-754 floating point — floats imprecise kyun hain, iski gehri wajah.
  • Truthiness and conditionals — non-bool values if mein kaise behave karti hain.
  • Type checking — isinstance vs type — robust type tests.

Concept Map

interpreted by

defines

defines

defines

defines

defines

reveals

binary rounding

fix

subclass of

True=1 False=0

means

Bits 0 and 1

Data type = interpretation rule

int whole numbers

float IEEE-754

str char sequence

bool True False

NoneType single None

type&(x&) and isinstance

0.1+0.2 != 0.3

compare with abs diff less than epsilon

sum counts truthy

no value placeholder