Operators, Expressions और Precedence
Operators और operands
Operator एक symbol है जो कोई operation करता है; जिन values पर वह काम करता है वे operands हैं। marks + 5 में + operator है और marks और 5 operands। एक operand वाला operator (जैसे -x या not x) unary है; दो वाला binary।
Arithmetic operators
| Operator | मतलब | 17 ? 5 |
|---|---|---|
+ | जोड़ | 22 |
- | घटाव | 12 |
* | गुणा | 85 |
/ | भाग (हमेशा float) | 3.4 |
// | Floor division (भागफल) | 3 |
% | Modulus (शेषफल) | 2 |
** | Exponent (घात) | 17**2 = 289 |
a, b = 17, 5 print(a + b, a - b, a * b) print(a / b, a // b, a % b, a ** 2) print(-17 // 5, -17 % 5) # floor division rounds DOWN
n % 2 == 0 even number जाँचता है; n % 10 आख़िरी digit देता है; n // 10 आख़िरी digit हटाता है। + strings जोड़ता भी है ('Py' + 'thon') और * उन्हें दोहराता है ('-' * 20)।
Relational (comparison) operators
ये दो values compare करते हैं और हमेशा True या False देते हैं।
a, b = 17, 5
print(a > b, a < b, a == 17, a != b, a >= 17, b <= 4)
print('apple' < 'banana', 'Zoo' < 'apple') # compared by Unicode codesStrings अक्षर-दर-अक्षर Unicode codes से compare होती हैं; capital letters (65–90) small letters (97–122) से पहले आते हैं, इसलिए 'Zoo' < 'apple' True है।
Assignment operators
x += 5 जैसा compound assignment x = x + 5 का छोटा रूप है।
x = 10 x += 5; print(x) x -= 3; print(x) x *= 2; print(x) x //= 5; print(x) x **= 3; print(x) x %= 10; print(x)
Logical operators
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
age, has_id = 20, False print(age >= 18 and has_id) # both must be True print(age >= 18 or has_id) # any one True is enough print(not has_id) print(0 and 5, 3 and 5, 0 or 'default', '' or 'guest')
Short-circuit: and पहली false value पर रुक जाता है; or पहली true value पर, और वे वही value return करते हैं। इसीलिए '' or 'guest' से 'guest' मिलता है — default values के लिए काम का।
Identity और membership operators
L = [1, 2, 3]
M = L
N = [1, 2, 3]
print(M is L, N is L) # identity: same object?
print(N == L, N is not L) # equality: same value?
print('a' in 'class', 5 in [1, 2, 3], 'z' not in 'python')== (equality)
- क्या values same हैं?
- N == L → True
- Data compare करने के लिए
is (identity)
- क्या same object है?
- N is L → False (दो अलग lists)
- मुख्यतः x is None के लिए
Bitwise operators
ये integers के binary digits पर काम करते हैं: & AND, | OR, ^ XOR, ~ NOT, << left shift, >> right shift।
print(12 & 10, 12 | 10, 12 ^ 10) # 1100 and 1010
12 = 1100 और 10 = 1010 → AND 1000 = 8, OR 1110 = 14, XOR 0110 = 6।
Expressions और precedence
Expression values, variables और operators का मेल है जिसे Python एक value में evaluate करता है, जैसे (a + b) / 2। Expression में कई operators हों तो precedence तय करती है कि पहले क्या होगा, और associativity बराबर precedence वाले operators का क्रम (ज़्यादातर बाएँ से दाएँ; ** दाएँ से बाएँ)।
print(2 + 3 * 4) # * before + print((2 + 3) * 4) # brackets first print(2 ** 3 ** 2) # ** is right to left: 2 ** 9 print(10 - 4 - 3) # left to right print(100 / 10 * 2) # left to right print(5 + 4 > 8 and not 3 > 4)
2 ** 2 = 4 → 3 * 4 = 12 → 12 // 4 = 3 → 12 + 3 = 15 → 15 − 1 = 14।
सारांश और practice
/float देता है,//floor quotient,%remainder,**power।- Comparisons True/False देते हैं; logical operators short-circuit करते हैं।
isidentity,==value,inmembership जाँचता है।- Precedence: () → ** → unary → * / // % → + - → comparisons → not → and → or।
Output बताइए: (a) print(15 % 4 + 2 ** 3) (b) print(-9 // 2) (c) print(not 5 > 3 or 2 == 2) (d) print(10 / 4 * 2)
Answers: (a) 11 (b) -5 (c) True (d) 5.0
