📑 Table of Contents — किसी भी topic पर सीधे जाएँ
Operator क्या है? — Operand, Operator व उनके परिवार
a + b में — a, b operands हैं और + operator। Chapter 3 में हमने tokens में operators का नाम सुना था — अब पूरा परिवार मिलेगा!👨👩👧 Operators की गिनती के आधार पर 3 श्रेणियाँ
| श्रेणी | Operands | Example |
|---|---|---|
| Unary | 1 operand | -x, not flag, ~n |
| Binary | 2 operands | a + b, x > y |
| Ternary | 3 हिस्से | a if a > b else b |
a, b = 15, 8
largest = a if a > b else b
print("Largest =", largest)🗂️ Python के 7 प्रकार के Operators (इस Chapter का नक्शा)
| # | Type | Operators |
|---|---|---|
| 1 | Arithmetic | + − * / // % ** |
| 2 | Relational | == != > < >= <= |
| 3 | Logical | and or not |
| 4 | Assignment | = += −= *= /= //= %= **= |
| 5 | Bitwise | & | ^ ~ << >> |
| 6 | Membership | in not in |
| 7 | Identity | is is not |
value_if_true if condition else value_if_false। Python में कुल 7 प्रकार के operators।Arithmetic Operators — गणित के 7 सिपाही
| Operator | नाम | Example | Result |
|---|---|---|---|
| + | Addition | 7 + 3 | 10 |
| − | Subtraction | 7 − 3 | 4 |
| * | Multiplication | 7 * 3 | 21 |
| / | Division (हमेशा float) | 7 / 2 | 3.5 |
| // | Floor Division | 7 // 2 | 3 |
| % | Modulus (remainder) | 7 % 2 | 1 |
| ** | Exponent (power) | 2 ** 3 | 8 |
a, b = 17, 5
print("Add:", a + b)
print("Sub:", a - b)
print("Mul:", a * b)
print("Div:", a / b) # float
print("Floor Div:", a // b) # int quotient
print("Mod:", a % b) # remainder
print("Power:", a ** 2)Sub: 12
Mul: 85
Div: 3.4
Floor Div: 3
Mod: 2
Power: 289
⚠️ Negative Floor Division — Exam Trap!
print(10 // 3) # 3.33 -> neeche -> 3 print(-10 // 3) # -3.33 -> NEECHE ki taraf -> -4 ! print(-7 % 3) # modulus ka sign divisor jaisa -> 2
-4
2
⚡ ** की खासियत — Right to Left!
print(2 ** 3 ** 2) # pehle 3**2 = 9, phir 2**9 print((2 ** 3) ** 2) # brackets se order badla
64
Relational (Comparison) Operators — तुलना करने वाले
| Operator | मतलब | Example (a=10, b=5) | Result |
|---|---|---|---|
| == | बराबर है? | a == b | False |
| != | बराबर नहीं है? | a != b | True |
| > | बड़ा है? | a > b | True |
| < | छोटा है? | a < b | False |
| >= | बड़ा या बराबर? | a >= 10 | True |
| <= | छोटा या बराबर? | b <= 4 | False |
marks = 75
print(60 <= marks <= 80) # kya marks 60-80 ke beech?
# dusri languages me likhna padta:
# marks >= 60 and marks <= 80
print("apple" < "banana") # strings ki dictionary tulna!True
Logical Operators — and, or, not की तिकड़ी
📋 Truth Tables (रटने लायक)
| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
age = 20 has_id = True print(age >= 18 and has_id) # dono zaroori print(age < 18 or has_id) # koi ek kaafi print(not has_id) # ulta
True
False
⚡ Short-Circuit Evaluation — Python की चालाकी
print(0 and 10) # pehla falsy -> wahi lauta diya print(5 and 10) # pehla truthy -> DUSRA lauta diya print(0 or 10) # pehla falsy -> dusra dekha print(5 or 10) # pehla truthy -> wahi lauta, dusra dekha hi nahi!
10
10
5
x != 0 and 10/x।Assignment Operators — Value देने वाले shortcuts
x += 5 यानी x = x + 5।| Operator | Example | बराबर है | x=10 पर Result |
|---|---|---|---|
| = | x = 10 | — | 10 |
| += | x += 3 | x = x + 3 | 13 |
| −= | x −= 3 | x = x − 3 | 7 |
| *= | x *= 3 | x = x * 3 | 30 |
| /= | x /= 3 | x = x / 3 | 3.333… |
| //= | x //= 3 | x = x // 3 | 3 |
| %= | x %= 3 | x = x % 3 | 1 |
| **= | x **= 2 | x = x ** 2 | 100 |
total = 0
total += 250 # pehla item
total += 120 # dusra item
total -= 50 # discount
total *= 1.18 # 18% GST laga
print(f"Final Bill: Rs. {round(total, 2)}")Bitwise Operators — Binary bits पर सीधा काम
| Operator | नाम | Rule | a=10, b=4 |
|---|---|---|---|
| & | AND | दोनों bits 1 हों तो 1 | 1010 & 0100 = 0000 → 0 |
| | | OR | कोई एक bit 1 हो तो 1 | 1010 | 0100 = 1110 → 14 |
| ^ | XOR | bits अलग हों तो 1 | 1010 ^ 0100 = 1110 → 14 |
| ~ | NOT | ~n = −(n+1) | ~10 → −11 |
| << | Left Shift | × 2 हर shift पर | 10 << 2 → 40 |
| >> | Right Shift | ÷ 2 हर shift पर | 10 >> 2 → 2 |
a, b = 10, 4 # 1010, 0100
print("AND :", a & b)
print("OR :", a | b)
print("XOR :", a ^ b)
print("NOT :", ~a) # -(10+1) = -11
print("Left Shift :", a << 2) # 10 x 4 = 40
print("Right Shift:", a >> 2) # 10 / 4 = 2OR : 14
XOR : 14
NOT : -11
Left Shift : 40
Right Shift: 2
Membership व Identity Operators — in और is की जोड़ी
🔍 Membership — in, not in
text = "Boosting Skills"
print("Skill" in text)
print("Java" in text)
print("Java" not in text)
fruits = ["apple", "mango", "banana"]
print("mango" in fruits)False
True
True
🆔 Identity — is, is not (vs ==)
list1 = [1, 2, 3] list2 = [1, 2, 3] # same values, ALAG object list3 = list1 # wahi object print(list1 == list2) # values same? True print(list1 is list2) # object same? False! print(list1 is list3) # True — ek hi object ke 2 naam
False
True
x is None से करते हैं।Operator Precedence व Associativity — पहले कौन चलेगा?
Precedence Ladder — ऊपर वाला पहले चलता है
result = 5 + 2 * 3 ** 2 - 4 / 2 # Step 1: 3 ** 2 = 9 (exponent pehle) # Step 2: 2 * 9 = 18 (* aur / same level) # Step 3: 4 / 2 = 2.0 # Step 4: 5 + 18 - 2.0 = 21.0 print(result) print(True or False and False) # and pehle! True or (False) = True
True
True or False and False में पहले and (False), फिर or → True। Confusion से बचना हो तो brackets लगाइए — यही best practice है!Expressions व Statements — Value देने वाले vs काम करने वाले
| आधार | Expression | Statement |
|---|---|---|
| क्या करता है | Value produce करता है | Action perform करता है |
| Example | 2 + 3, a * b, x > 5 | x = 5, if..., for..., print(x) |
| Variable में रख सकते हैं? | हाँ — y = 2 + 3 | नहीं — y = (x = 5) ✘ Error |
| सम्बन्ध | Statement का हिस्सा हो सकता है | Expressions से मिलकर बनता है |
🧩 Types of Expressions
a, b = 8, 3
print(a + b * 2) # Arithmetic expression
print(a > b) # Relational expression
print(a > 5 and b < 5) # Logical expression
print("Py" + "thon") # String expressionTrue
True
Python
📜 Types of Statements
- Assignment statement:
x = 10 - Conditional statement:
if, elif, else(Chapter 2 में देखे) - Looping statement:
for, while - Control statements:
break, continue, pass - Compound statement: header + indented block (if/for/def का पूरा block)
🎯 Output-Based Questions — Operators के Tricky सवाल
Q1. / vs // vs %
print(15 / 4) print(15 // 4) print(15 % 4)
3
3
← / float देता है; // quotient; % remainder।
Q2. Negative Floor Division
print(-17 // 5) print(-17 % 5)
3
← -3.4 नीचे की ओर = -4; remainder = -17 - (5 × -4) = 3 (sign divisor जैसा)।
Q3. ** की Right Associativity
print(2 ** 2 ** 3)
← Right से: 2**3 = 8, फिर 2**8 = 256 (64 नहीं!)।
Q4. Precedence Mix
print(10 - 4 * 2 + 6 // 3)
← पहले 4*2=8 और 6//3=2; फिर 10-8+2 = 4।
Q5. Short-Circuit Values
print(4 and 7)
print(0 or "Hello")
print("" and 100)Hello
(खाली string)
← and: पहला truthy तो दूसरा; or: पहला falsy तो दूसरा; "" falsy है तो and ने वही लौटाया।
Q6. Logical Precedence
print(not True and False) print(not (True and False))
True
← पहली line: not True = False, फिर False and False = False; दूसरी में bracket पहले — and का False, फिर not = True।
Q7. Bitwise की गिनती
print(6 & 3) print(6 | 3) print(6 ^ 3)
7
5
← 110 & 011 = 010(2); 110 | 011 = 111(7); 110 ^ 011 = 101(5)।
Q8. Shift Operators
print(5 << 3) print(80 >> 2) print(~7)
20
-8
← 5×2³=40; 80÷2²=20; ~n = -(n+1) = -8।
Q9. Chained Comparison
x = 15 print(10 < x < 20) print(10 < x > 20)
False
← पहली: 10<15 और 15<20 दोनों true; दूसरी: 15>20 false — chaining में सब true होने चाहिए।
Q10. is vs ==
a = [10, 20] b = [10, 20] c = a print(a == b, a is b) print(a == c, a is c)
True True
← a, b की values same पर objects अलग; c तो a का ही दूसरा नाम है।
Q11. Augmented Assignment Chain
x = 8 x //= 3 x **= 2 x += 1 print(x)
← 8//3 = 2 → 2**2 = 4 → 4+1 = 5।
Q12. Membership + Logical Combo
s = "python"
print("p" in s and "z" not in s)
print("P" in s)False
← "p" है और "z" नहीं है — दोनों true। "P" (capital) नहीं है — case-sensitive!
💻 Practice Programs — Operators का असली उपयोग
Program 1 — Simple Calculator (सभी arithmetic)
a = float(input("Pehla number: "))
b = float(input("Dusra number: "))
print(f"Sum = {a + b}")
print(f"Difference = {a - b}")
print(f"Product = {a * b}")
print(f"Division = {a / b}")
print(f"Floor Div = {a // b}")
print(f"Remainder = {a % b}")
print(f"Power = {a ** b}")Difference = 7.0
Product = 18.0
Division = 4.5
Floor Div = 4.0
Remainder = 1.0
Power = 81.0
Program 2 — Bitwise से Even/Odd (नई trick!)
n = int(input("Number: "))
if n & 1:
print("Odd") # aakhri bit 1 = odd
else:
print("Even") # aakhri bit 0 = evenProgram 3 — XOR से Swap (temp भी नहीं, comma भी नहीं!)
a, b = 25, 60
a = a ^ b
b = a ^ b # (a^b)^b = a
a = a ^ b # (a^b)^a = b
print("a =", a, ", b =", b)← XOR का जादू: x^y^y = x हमेशा!
Program 4 — Voting Eligibility (logical operators)
age = int(input("Age: "))
citizen = input("Indian citizen? (yes/no): ")
eligible = age >= 18 and citizen == "yes"
print("Vote de sakte hain!" if eligible else "Abhi eligible nahi.")Program 5 — Discount Calculator (chained comparison)
amount = float(input("Bill amount: "))
if amount >= 5000:
discount = 20
elif 2000 <= amount < 5000: # chaining!
discount = 10
elif 500 <= amount < 2000:
discount = 5
else:
discount = 0
final = amount - (amount * discount / 100)
print(f"Discount: {discount}% | Final: Rs. {final}")Program 6 — Seconds को HH:MM:SS में (// और %)
total = int(input("Total seconds: "))
hours = total // 3600
minutes = (total % 3600) // 60
seconds = total % 60
print(f"{hours} hr : {minutes} min : {seconds} sec")Program 7 — Power Table (** का उपयोग)
n = int(input("Number: "))
for p in range(1, 6):
print(f"{n} ^ {p} = {n ** p}")3 ^ 2 = 9
3 ^ 3 = 27
3 ^ 4 = 81
3 ^ 5 = 243
Program 8 — Password Strength (membership + logical)
pwd = input("Password: ")
has_digit = any(ch in "0123456789" for ch in pwd)
has_special = "@" in pwd or "#" in pwd or "$" in pwd
long_enough = len(pwd) >= 8
if has_digit and has_special and long_enough:
print("Strong password ✔")
else:
print("Weak password — digit, @/#/$ aur 8+ length rakhein")Summary — Quick Revision (Exam से पहले पढ़ें)
- Operator = symbol, Operand = value; Unary (1), Binary (2), Ternary (a if cond else b)।
- 7 प्रकार: Arithmetic, Relational, Logical, Assignment, Bitwise, Membership, Identity।
- Arithmetic: / हमेशा float; // floor (नीचे — -10//3 = -4); % का sign divisor जैसा; ** right-to-left (2**3**2 = 512)।
- Relational: हमेशा True/False; chaining valid (10 < x < 20); strings dictionary order में।
- Logical: not > and > or; short-circuit — and/or असली values लौटाते हैं (5 and 10 → 10; 5 or 10 → 5)।
- Assignment: x += 5 ⟺ x = x+5; Python में ++ / -- नहीं होता।
- Bitwise: & | ^ bits पर; ~n = −(n+1); n<<k = n×2ᵏ; n>>k = n÷2ᵏ; a^a = 0।
- Membership: in / not in (sequence में?); Identity: is / is not (same object? — == से अलग!)।
- Precedence: () → ** → unary → * / // % → + − → bitwise → comparison → not → and → or।
- Expression value देता है (2+3); Statement action करता है (x = 5, if, for); compound statement = header + indented block।
Model Questions — 50 MCQs + 15 Theory Questions
❓ A. Multiple Choice Questions (50)
| # | प्रश्न |
|---|---|
| 1 | a + b में a और b क्या कहलाते हैं? (a) Operators(b) Operands(c) Keywords(d) Literals ✔ सही उत्तर: (b) Operands+ operator है; जिन values पर operation हो वे operands। |
| 2 | केवल 1 operand वाला operator कहलाता है — (a) Binary(b) Ternary(c) Unary(d) Single ✔ सही उत्तर: (c) Unaryजैसे -x, not flag, ~n। |
| 3 | a if a > b else b किस operator का उदाहरण है? (a) Unary(b) Binary(c) Ternary(d) Logical ✔ सही उत्तर: (c) Ternaryएक line का if-else — 3 हिस्से होते हैं। |
| 4 | Python में operators के कितने मुख्य प्रकार हैं? (a) 4(b) 5(c) 7(d) 10 ✔ सही उत्तर: (c) 7Arithmetic, Relational, Logical, Assignment, Bitwise, Membership, Identity। |
| 5 | 10 / 5 का output क्या होगा? (a) 2(b) 2.0(c) 5(d) Error ✔ सही उत्तर: (b) 2.0/ हमेशा float देता है — पूरा divide होने पर भी। |
| 6 | 17 // 5 का मान है — (a) 3.4(b) 3(c) 4(d) 2 ✔ सही उत्तर: (b) 3Floor division — quotient का पूर्णांक भाग। |
| 7 | -10 // 3 का output — (a) -3(b) -4(c) 3(d) -3.33 ✔ सही उत्तर: (b) -4-3.33 को नीचे की ओर round किया जाता है — -4। |
| 8 | 17 % 5 का मान — (a) 3(b) 2(c) 3.4(d) 12 ✔ सही उत्तर: (b) 217 = 5×3 + 2 — remainder 2। |
| 9 | 2 ** 3 ** 2 का output — (a) 64(b) 512(c) 36(d) 12 ✔ सही उत्तर: (b) 512** right-to-left है — पहले 3**2=9, फिर 2**9=512। |
| 10 | कौन-सा operator power (घात) निकालता है? (a) ^(b) **(c) //(d) %% ✔ सही उत्तर: (b) **^ Python में XOR है, power नहीं! |
| 11 | Relational operators का result हमेशा होता है — (a) int(b) float(c) True/False(d) string ✔ सही उत्तर: (c) True/FalseBoolean values — इसीलिए conditions में उपयोग होते हैं। |
| 12 | = और == में अंतर — (a) दोनों same(b) = assignment, == comparison(c) = comparison, == assignment(d) दोनों गलत ✔ सही उत्तर: (b) = assignment, == comparisonif x = 5 लिखना SyntaxError है! |
| 13 | x = 15 हो तो 10 < x < 20 का output — (a) True(b) False(c) Error(d) 15 ✔ सही उत्तर: (a) TrueChaining — दोनों comparisons true हैं। |
| 14 | "Apple" < "apple" का output — (a) False(b) True(c) Error(d) 0 ✔ सही उत्तर: (b) TrueUppercase की ASCII value छोटी होती है (A=65, a=97)। |
| 15 | कौन-सा operator "बराबर नहीं" check करता है? (a) <>(b) !=(c) ==(d) not ✔ सही उत्तर: (b) !=<> पुराने Python 2 में था, अब नहीं चलता। |
| 16 | True and False का मान — (a) True(b) False(c) 1(d) Error ✔ सही उत्तर: (b) Falseand में दोनों true होने चाहिए। |
| 17 | False or True का मान — (a) False(b) True(c) 0(d) None ✔ सही उत्तर: (b) Trueor में कोई एक true काफी है। |
| 18 | not (5 > 3) का output — (a) True(b) False(c) 5(d) Error ✔ सही उत्तर: (b) False5>3 True है, not से उल्टा — False। |
| 19 | print(3 and 8) का output — (a) True(b) 3(c) 8(d) False ✔ सही उत्तर: (c) 8and में पहला truthy हो तो दूसरा operand return होता है। |
| 20 | print(0 or 7) का output — (a) 0(b) 7(c) True(d) False ✔ सही उत्तर: (b) 7or में पहला falsy (0) हो तो दूसरा return होता है। |
| 21 | Short-circuit में "and" का दूसरा operand check नहीं होता जब — (a) पहला True हो(b) पहला False हो(c) दोनों True हों(d) कभी नहीं ✔ सही उत्तर: (b) पहला False होFalse and X — answer False ही रहेगा, X देखने की ज़रूरत नहीं। |
| 22 | इनमें से falsy value नहीं है — (a) 0(b) ""(c) None(d) "0" ✔ सही उत्तर: (d) "0""0" एक non-empty string है — truthy! |
| 23 | x += 5 बराबर है — (a) x = 5(b) x = x + 5(c) x + 5(d) x == 5 ✔ सही उत्तर: (b) x = x + 5Augmented assignment operator। |
| 24 | Python में x++ लिखने पर — (a) x बढ़ जाता है(b) SyntaxError(c) x घट जाता है(d) 0 हो जाता है ✔ सही उत्तर: (b) SyntaxErrorPython में ++/-- नहीं होते — x += 1 लिखते हैं। |
| 25 | x = 10; x //= 3 के बाद x — (a) 3.33(b) 3(c) 1(d) 30 ✔ सही उत्तर: (b) 3x = 10 // 3 = 3। |
| 26 | x = 4; x **= 2 के बाद x — (a) 8(b) 16(c) 6(d) 2 ✔ सही उत्तर: (b) 16x = 4 ** 2 = 16। |
| 27 | Bitwise AND operator है — (a) and(b) &&(c) &(d) AND ✔ सही उत्तर: (c) &and logical है; && Python में नहीं होता। |
| 28 | 6 & 3 का output — (110 & 011) (a) 7(b) 2(c) 5(d) 9 ✔ सही उत्तर: (b) 2110 & 011 = 010 = 2 (दोनों 1 हों तभी 1)। |
| 29 | 6 | 3 का output — (a) 7(b) 2(c) 5(d) 3 ✔ सही उत्तर: (a) 7110 | 011 = 111 = 7 (कोई एक 1 हो तो 1)। |
| 30 | 6 ^ 3 का output — (a) 7(b) 5(c) 2(d) 216 ✔ सही उत्तर: (b) 5110 ^ 011 = 101 = 5 (bits अलग हों तो 1)। ^ power नहीं है! |
| 31 | ~5 का मान — (a) 5(b) -5(c) -6(d) 4 ✔ सही उत्तर: (c) -6Formula: ~n = -(n+1)। |
| 32 | 4 << 2 का output — (a) 8(b) 16(c) 1(d) 6 ✔ सही उत्तर: (b) 16हर left shift ×2 — 4×2² = 16। |
| 33 | 32 >> 3 का output — (a) 4(b) 8(c) 256(d) 29 ✔ सही उत्तर: (a) 432 ÷ 2³ = 4। |
| 34 | a ^ a का मान हमेशा — (a) a(b) 2a(c) 0(d) 1 ✔ सही उत्तर: (c) 0Same bits XOR में cancel हो जाते हैं — XOR swap इसी पर आधारित। |
| 35 | Membership operator कौन-से हैं? (a) is, is not(b) in, not in(c) and, or(d) ==, != ✔ सही उत्तर: (b) in, not inSequence में value की जाँच के लिए। |
| 36 | "a" in "apple" का output — (a) True(b) False(c) 1(d) a ✔ सही उत्तर: (a) True"a" string "apple" में मौजूद है। |
| 37 | is operator तुलना करता है — (a) Values की(b) Object identity (memory) की(c) Types की(d) Length की ✔ सही उत्तर: (b) Object identity कीid() same है या नहीं — == values देखता है। |
| 38 | a=[1,2]; b=[1,2] हो तो a is b — (a) True(b) False(c) Error(d) [1,2] ✔ सही उत्तर: (b) FalseValues same पर memory में दो अलग objects — a == b True होगा। |
| 39 | None की जाँच का सही तरीका — (a) x == None(b) x is None(c) x = None(d) None in x ✔ सही उत्तर: (b) x is NoneIdentity check — यही Pythonic best practice है। |
| 40 | सबसे ऊँची precedence किसकी है? (a) **(b) ( )(c) *(d) or ✔ सही उत्तर: (b) ( )Parentheses सबसे पहले — फिर **। |
| 41 | सबसे नीची precedence (दिए गए में) — (a) and(b) not(c) or(d) == ✔ सही उत्तर: (c) orक्रम: not → and → or (or सबसे बाद में)। |
| 42 | 2 + 3 * 4 का output — (a) 20(b) 14(c) 24(d) 9 ✔ सही उत्तर: (b) 14* की precedence + से ऊँची — पहले 3×4। |
| 43 | True or False and False का output — (a) False(b) True(c) Error(d) None ✔ सही उत्तर: (b) Trueand पहले चलता है — True or (False) = True। |
| 44 | कौन-सा operator right-to-left associative है? (a) +(b) *(c) **(d) // ✔ सही उत्तर: (c) **बाकी arithmetic operators left-to-right हैं। |
| 45 | 10 - 4 + 2 का output — (a) 4(b) 8(c) 16(d) 12 ✔ सही उत्तर: (b) 8Left-to-right: (10-4)+2 = 8। |
| 46 | Value produce करने वाला combination कहलाता है — (a) Statement(b) Expression(c) Keyword(d) Block ✔ सही उत्तर: (b) Expressionजैसे 2+3, a*b — इन्हें variable में रख सकते हैं। |
| 47 | x = 5 है — (a) Expression(b) Assignment statement(c) Operator(d) Literal ✔ सही उत्तर: (b) Assignment statementयह action करता है, value produce नहीं। |
| 48 | a > 5 and b < 3 किस प्रकार का expression है? (a) Arithmetic(b) String(c) Logical(d) Assignment ✔ सही उत्तर: (c) LogicalRelational expressions को and से जोड़ा गया है। |
| 49 | if/for जैसे header + indented block वाले statements कहलाते हैं — (a) Simple(b) Compound(c) Empty(d) Nested ✔ सही उत्तर: (b) Compound statementsHeader में : और नीचे indented body। |
| 50 | print(5 > 3 > 1) का output — (a) True(b) False(c) Error(d) 1 ✔ सही उत्तर: (a) TrueChaining: 5>3 True और 3>1 True — दोनों true। |
📝 B. 15 Theory Questions (Short Answers)
- Operator और Operand में अंतर बताइए।Operator वह symbol है जो operation करता है (+, ==, and); Operand वे values हैं जिन पर operation होता है। a + b में a, b operands और + operator।
- Unary, Binary व Ternary operators उदाहरण सहित लिखिए।Unary — 1 operand (-x, not flag, ~n); Binary — 2 operands (a+b, x>y); Ternary — condition वाला (largest = a if a>b else b)।
- Python के 7 प्रकार के operators के नाम लिखिए।Arithmetic, Relational (Comparison), Logical, Assignment, Bitwise, Membership (in/not in), Identity (is/is not)।
- / और // में अंतर उदाहरण सहित बताइए।/ true division — हमेशा float (10/3 = 3.333, 10/5 = 2.0); // floor division — नीचे की ओर rounded quotient (10//3 = 3, -10//3 = -4)।
- -10 // 3 का उत्तर -4 क्यों है, -3 क्यों नहीं?Floor यानी नीचे की ओर rounding — -3.33 के नीचे number line पर -4 आता है, -3 नहीं (यह ऊपर है)। इसीलिए negative floor division में हमेशा नीचे वाला integer।
- ** operator की associativity समझाइए।** अकेला right-to-left associative operator है — 2**3**2 में पहले दाईं ओर 3**2 = 9, फिर 2**9 = 512 (न कि (2**3)**2 = 64)।
- Comparison chaining क्या है?Python में कई comparisons एक साथ लिख सकते हैं — 10 < x < 20 यानी (10 < x) and (x < 20)। सभी comparisons true हों तभी result True।
- Short-circuit evaluation समझाइए।and में पहला operand falsy हो तो दूसरा evaluate नहीं होता (वही return); or में पहला truthy हो तो दूसरा skip। इससे speed बढ़ती है और x != 0 and 10/x जैसे patterns से errors से बचाव होता है।
- Python में falsy values कौन-सी हैं?0, 0.0, "" (empty string), None, False, खाली list []/tuple ()/dict {} — बाकी सब values truthy मानी जाती हैं (जैसे "0" string truthy है)।
- Augmented assignment operators क्या हैं? कोई 4 लिखिए।Operation + assignment के shortcuts — x += 5 (x = x+5), x -= 2, x *= 3, x //= 2, x **= 2। Python में ++/-- नहीं होते।
- ~ (bitwise NOT) का formula व उदाहरण लिखिए।~n = -(n+1) — इसलिए ~5 = -6, ~10 = -11, ~(-3) = 2। यह सभी bits उलट देता है (two's complement के कारण negative)।
- Left shift व Right shift की tricks बताइए।n << k = n × 2ᵏ (हर shift पर double); n >> k = n ÷ 2ᵏ (हर shift पर आधा, integer) — 5<<3 = 40, 80>>2 = 20।
- == और is में अंतर उदाहरण सहित बताइए।== values की equality (क्या बराबर हैं?); is object identity (क्या memory में एक ही object? id() same?)। a=[1,2]; b=[1,2] पर a==b True पर a is b False। None की जाँच x is None से।
- Operator precedence का पूरा क्रम लिखिए।() → ** → unary (~, +x, -x) → * / // % → + - → << >> → & → ^ → | → comparisons (==, >, in, is) → not → and → or।
- Expression और Statement में अंतर बताइए।Expression value produce करता है (2+3, a>b) — variable में रख सकते हैं; Statement action करता है (x=5, if, for, print) — ज़रूरी नहीं value दे। if/for = compound statements (header + indented block)।