← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 16 of 16

Python के Operators

Python अपने operators को इस आधार पर कई परिवारों में बाँटता है कि वे क्या करते हैं: arithmetic operators गणित करते हैं, comparison operators values के बीच संबंध जाँचते हैं, और logical operators boolean शर्तों को जोड़ते हैं।
Syntax
python
result = operand1 operator operand2
result = operator operand

Arithmetic Operators: गणित करना

Arithmetic operators -- +, -, *, /, // (floor division), % (modulo), और ** (exponentiation) -- संख्याओं पर गणितीय गणनाएँ करते हैं, और (विशेष रूप से + ) strings और lists पर जोड़ के बजाय concatenation या दोहराव के अर्थ में भी काम करते हैं।

Note: याद रखिए कि Python 3 में / हमेशा float नतीजा देता है, भले ही दो integers को बराबर-बराबर भाग दिया गया हो -- जब आपको विशेष रूप से integer (floor-divided) नतीजा चाहिए तब // का उपयोग कीजिए।
Warning: / (true division, हमेशा float देता है) को // (floor division, नीचे की ओर round करके integer देता है) से confuse करना अनपेक्षित float results का एक आम कारण है।

उदाहरण: Arithmetic Operators: Doing Math

python
print(7 + 3, 7 - 3, 7 * 3, 7 / 3, 7 // 3, 7 % 3, 7 ** 2)

Comparison Operators: संबंध जाँचना

Comparison operators -- ==, !=, <, >, <=, >= -- दो values की तुलना करते हैं और हमेशा boolean नतीजा (True या False) देते हैं, जो if statement या while loop में इस्तेमाल होने वाली हर शर्त के बुनियादी घटक हैं।

Note: values की तुलना के लिए == इस्तेमाल कीजिए, और अकेले = को सख्ती से assignment के लिए रखिए -- इन्हें गड़बड़ाना अन्य संदर्भों से आने वाले शुरुआती लोगों की सबसे आम syntax गलतियों में से एक है।
Warning: बहुत अलग, असंगत types (जैसे string और integer) की values की तुलना < या > से करना Python 3 में TypeError देता है, कुछ ढीली भाषाओं के उलट।

उदाहरण: Comparison Operators: Testing Relationships

python
print(5 == 5, 5 != 3, 5 > 3, 5 < 3)

Logical Operators: शर्तों को जोड़ना

Logical operators -- and, or, not -- कई boolean expressions को एक समग्र नतीजे में जोड़ते हैं। and के लिए दोनों पक्षों का True होना ज़रूरी है, or के लिए कम से कम एक पक्ष का True होना, और not सिर्फ़ एक boolean value को उलट देता है।

Note: जटिल logical expression के हिस्सों को स्पष्ट रूप से समूहित करने के लिए parentheses इस्तेमाल कीजिए, भले ही सख्ती से ज़रूरी न हों, ताकि पाठक को इच्छित तर्क साफ़ दिखे।
Warning: Python, and/or को short-circuit करता है -- अगर and के बाईं तरफ का हिस्सा False है, तो दाईं तरफ का हिस्सा बिल्कुल भी evaluate नहीं होता, जो मायने रखता है अगर उस दाईं तरफ किसी function को कॉल करने जैसा कोई side effect हो।

उदाहरण: Logical Operators: Combining Conditions

python
a, b = True, False
print(a and b, a or b, not a)

Assignment Operators: Values को Store और Update करना

value assign करने के बुनियादी = से आगे, Python compound assignment operators देती है -- +=, -=, *=, /= और अन्य -- जो किसी variable को उसी के वर्तमान value के आधार पर एक संक्षिप्त चरण में अपडेट करते हैं, जैसे counter बढ़ाने के लिए count += 1।

Note: जब भी किसी variable को उसी के वर्तमान value के आधार पर अपडेट करना हो, compound assignment (जैसे total += price) इस्तेमाल कीजिए, क्योंकि यह पूरा लिखे रूप से ज़्यादा संक्षिप्त और मंशा में ज़्यादा स्पष्ट है।
Warning: total += price बिल्कुल total = total + price के बराबर है -- यह बराबरी भूल जाना compound रूप को उससे ज़्यादा रहस्यमय महसूस करा सकता है जितना यह असल में है।

उदाहरण: Assignment Operators: Storing and Updating Values

python
total = 10
total += 5  # compound assignment: adds 5 and stores the result back in total
print(total)

विशेष परिवार: Identity, Membership और Bitwise

रोज़मर्रा के operator परिवारों से आगे, Python में तीन और विशेष परिवार हैं: identity operators (is, is not) जाँचते हैं कि दो variables ठीक एक ही object का संदर्भ देते हैं या नहीं; membership operators (in, not in) जाँचते हैं कि कोई value collection के भीतर मौजूद है या नहीं; और bitwise operators (&, |, ^, ~, <<, >>) integers के अलग-अलग bits को सीधे संचालित करते हैं।

Note: बराबरी के लिए VALUES की तुलना करने में == इस्तेमाल कीजिए, और is को विशेष रूप से यह जाँचने के लिए रखिए कि दो variables memory में शाब्दिक रूप से एक ही object का संदर्भ देते हैं या नहीं -- ये सचमुच अलग सवालों के जवाब देते हैं।
Warning: == (value equality) को is (identity/same-object check) से confuse करना एक सूक्ष्म लेकिन असली bug का स्रोत है, खासकर lists जैसे mutable objects के साथ।

उदाहरण: Specialized Families: Identity, Membership, and Bitwise

python
x = [1, 2]
y = x  # y references the same list object as x
print(x is y)  # identity operator: True, same object
print(1 in x)  # membership operator: True, 1 is in the list
print(2 & 3)  # bitwise operator: AND on the bits of 2 and 3
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. = (assignment) को == (comparison) से confuse करना -- एक single = किसी variable की value set करता है, जबकि == check करता है कि दो values equal हैं या नहीं, और इन्हें मिला देना beginners की सबसे common गलतियों में से एक है।
  2. यह मान लेना कि हर operator family अलग-अलग data types में एक जैसे व्यवहार करती है -- + numbers के लिए addition का मतलब है लेकिन strings के लिए concatenation, और यह context-dependence समझना ज़रूरी है।
  3. operator precedence भूल जाना और यह मान लेना कि operators strictly left to right evaluate होते हैं -- उदाहरण के लिए, multiplication addition से पहले होता है, जब तक parentheses कुछ और न कहें।
चैप्टर सारांश
  • Python operators को families में group करता है: arithmetic (math), comparison (relationships), logical (conditions combine करना), assignment (values store करना), identity, membership, और bitwise।
  • हर operator family एक अलग तरह की problem solve करती है -- arithmetic values compute करता है, comparison और logical operators conditions में इस्तेमाल होने वाले True/False results produce करते हैं।
  • Operator precedence तय करता है कि एक single expression में operators किस क्रम में evaluate होते हैं, mathematics के order of operations जैसा।
ब्राउज़र सपोर्ट

यहाँ बताई गई सभी operator families core Python syntax का हिस्सा हैं, और हर Python 3 version में समान रूप से supported हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.