Operator Precedence in Python in Hindi

Arpit Nageshwar
⏰ 4 min read

Operator Precedence in Python in Hindi – Operator Precedence Kya Hai?

Table of Contents

Introduction to Operator Precedence in Hindi – Operator Precedence Kya Hai?

  • जब भी किसी expression में एक से ज्यादा operators (जैसे +, *, /) एक साथ इस्तेमाल किए जाते हैं, तो Python को यह decide करना पड़ता है कि पहले किस operator को evaluate करे, और यही decide करने वाला rule Operator Precedence कहलाता है।

  • Simple भाषा में कहें तो Operator Precedence एक तरह की priority list होती है, जो यह बताती है कि किसी expression में कौन सा operator पहले काम करेगा और कौन सा बाद में।

  • जैसे स्कूल में हमें maths में BODMAS rule (Brackets, Order, Division, Multiplication, Addition, Subtraction) सिखाया जाता है, ठीक उसी तरह Python में भी हर operator की एक fixed priority (precedence) होती है, जिसके हिसाब से ही expression evaluate होती है।

  • अगर Operator Precedence का rule न हो, तो एक ही expression के अलग-अलग लोग अलग-अलग तरीके से interpret कर सकते थे, और हर बार अलग result आता, इसलिए यह rule हर programming language में fix किया जाता है।

  • जैसे expression 10 + 2 * 3 को देखते ही अगर आप इसे left से right पढ़ें तो लगेगा result 36 आएगा (10+2=12, फिर 12*3=36), लेकिन Multiplication की precedence Addition से ज्यादा होने की वजह से पहले 2 * 3 = 6 होगा, और फिर 10 + 6 = 16, यही सही answer है।

  • Operator Precedence को समझना इसलिए बहुत जरूरी है क्योंकि इसके बिना बड़े-बड़े mathematical या logical expressions में गलत result आने का खतरा हमेशा बना रहता है।

  • Exam की नज़र से देखा जाए तो Operator Precedence का मतलब है – "ek fixed order jo yeh decide karta hai ki kisi expression mein multiple operators hone par kaunsa operator pehle evaluate hoga."

Operator Precedence Rules in Hindi – Precedence के Rules

  • 1. Higher Precedence Pehle Evaluate Hoti Hai:
    जिस operator की precedence ज्यादा होती है, वह हमेशा कम precedence वाले operator से पहले evaluate होता है, जैसे * हमेशा + से पहले evaluate होगा।

  • 2. Same Precedence Wale Operators – Associativity Rule:
    अगर एक ही priority वाले दो या ज्यादा operators एक साथ हों, तो उन्हें एक fixed direction में evaluate किया जाता है, इसे Associativity कहा जाता है, ज्यादातर operators Left to Right associativity follow करते हैं।

  • 3. Exponentiation (**) Right to Left Chalta Hai:
    लगभग सभी operators Left to Right evaluate होते हैं, लेकिन Exponentiation (**) operator इसका exception है, यह Right to Left associativity follow करता है।

  • print(2 ** 3 ** 2)   # Output: 512   (2 ** (3 ** 2) = 2 ** 9 = 512, na ki (2**3)**2 = 64)
    
  • 4. Parentheses Ki Precedence Sabse Zyada Hoti Hai:
    किसी भी expression में parentheses ( ) के अंदर लिखा हिस्सा हमेशा सबसे पहले evaluate होता है, चाहे बाकी operators की precedence कुछ भी हो।

  • 5. Unary Operators Binary Operators Se Pehle Aate Hain:
    Unary operators (जैसे -x, ~x) आमतौर पर binary arithmetic operators (जैसे *, /) से पहले evaluate होते हैं।

  • print(-2 ** 2)   # Output: -4   (pehle ** evaluate hoga: 2**2=4, phir unary minus: -4)
    
  • 6. Comparison Operators Ki Chaining:
    Python में comparison operators को chain (जोड़कर) भी लिखा जा सकता है, और यह हमेशा left to right evaluate होते हैं, हर comparison independently True/False check होती है और आपस में and से जुड़ी मानी जाती है।

  • print(1 < 2 < 3)   # Output: True   (yani (1 < 2) and (2 < 3))
    

Order of Evaluation in Hindi – Evaluation Order कैसे तय होता है?

  • जब Python किसी complex expression को evaluate करता है, तो वह पहले सबसे highest precedence वाले operators को ढूंढता है और उन्हें सबसे पहले हल करता है, फिर धीरे-धीरे कम precedence वाले operators की तरफ बढ़ता है।

  • अगर expression में parentheses मौजूद हों, तो precedence rule को नजरअंदाज करते हुए, सबसे पहले parentheses के अंदर वाला हिस्सा solve किया जाता है, भले ही उसमें कम precedence वाले operators क्यों न हों।

  • अगर एक ही level की precedence वाले कई operators एक साथ आ जाएं (जैसे 10 - 3 + 2), तो उन्हें उनकी associativity (ज्यादातर Left to Right) के हिसाब से एक-एक करके evaluate किया जाता है।

Step-by-Step Example:

result = 10 + 2 * 3 ** 2 - 4 / 2
Step Operation Expression Ban Gaya
13 ** 2 (Exponent, highest precedence)10 + 2 * 9 - 4 / 2
22 * 9 (Multiplication)10 + 18 - 4 / 2
34 / 2 (Division)10 + 18 - 2.0
410 + 18 (Left to Right, Addition pehle)28 - 2.0
528 - 2.0 (Subtraction)26.0 (Final Result)
print(10 + 2 * 3 ** 2 - 4 / 2)   # Output: 26.0

इस example में देखिए कि सबसे पहले Exponent (**), फिर Multiplication aur Division (जो same precedence par hote hain, isliye left se right evaluate hue), और आखिर में Addition aur Subtraction evaluate हुए, यही पूरा process Order of Evaluation कहलाता है।

Operator Precedence Table in Hindi – Complete Reference (Highest to Lowest)

Precedence Pyramid (High to Low) ( ) Parentheses ** Exponentiation +x, -x, ~x (Unary) *, /, //, % (Mul/Div) +, - (Add/Sub) <<, >> (Shift) & (Bitwise AND) ^ (Bitwise XOR) | (Bitwise OR) <, <=, >, >=, ==, !=, in, is not, and, or (Lowest)
Precedence Operator(s) Description
1 (Highest)( )Parentheses / Grouping
2**Exponentiation (Right to Left)
3+x, -x, ~xUnary Plus, Minus, Bitwise NOT
4*, /, //, %Multiplication, Division, Floor Division, Modulus
5+, -Addition, Subtraction
6<<, >>Left Shift, Right Shift
7&Bitwise AND
8^Bitwise XOR
9|Bitwise OR
10<, <=, >, >=, ==, !=, in, not in, is, is notComparison aur Membership/Identity Operators
11notLogical NOT
12andLogical AND
13 (Lowest)orLogical OR

Exam की नज़र से इस table को याद रखने का सबसे आसान तरीका यह है – सबसे पहले Parentheses, फिर Exponent, फिर Arithmetic Operators (Unary → Mul/Div → Add/Sub), फिर Bitwise Shift/AND/XOR/OR, फिर Comparison Operators, और सबसे आखिर में Logical Operators (not → and → or)।

Role of Parentheses in Changing Precedence in Hindi – Parentheses का इस्तेमाल

  • Parentheses ( ) की precedence हमेशा बाकी सभी operators से सबसे ज्यादा होती है, यानी expression में जो भी हिस्सा parentheses के अंदर होगा, उसे सबसे पहले solve किया जाएगा, चाहे उसमें low-priority operators ही क्यों न हों।

  • इसका सबसे बड़ा फायदा यह है कि programmer default precedence rule को override करके, अपने हिसाब से evaluation order control कर सकता है, ताकि सही business logic के हिसाब से result मिल सके।

result1 = 10 + 5 * 2      # Default precedence: pehle 5*2=10, phir 10+10=20
print(result1)            # Output: 20

result2 = (10 + 5) * 2    # Parentheses ke wajah se pehle 10+5=15, phir 15*2=30
print(result2)            # Output: 30

ऊपर के दोनों examples में numbers same हैं (10, 5, 2), लेकिन सिर्फ parentheses लगाने की वजह से evaluation order पूरी तरह बदल गया, और result भी अलग आया।

Nested Parentheses Ka Example:

result = (10 + (5 - 2)) * 3

जब parentheses एक के अंदर एक (nested) हों, तो सबसे अंदर वाले parentheses को सबसे पहले solve किया जाता है, फिर बाहर वाले को:

Step Operation Expression Ban Gaya
1(5 - 2) innermost bracket solve hua(10 + 3) * 3
2(10 + 3) outer bracket solve hua13 * 3
313 * 339 (Final Result)

Real-world programs में भी, जब भी कोई formula (जैसे Simple Interest, Average निकालना, या कोई physics formula) लिखा जाता है, तो सही result पाने के लिए हमेशा parentheses का सही इस्तेमाल करना बेहद जरूरी होता है, ताकि operator precedence की वजह से कोई गलत calculation न हो जाए।

Frequently Asked Questions (FAQ) – Operator Precedence in Python in Hindi

एक fixed order जो यह decide करता है कि किसी expression में एक से ज्यादा operators होने पर कौन सा operator पहले evaluate होगा, उसे Operator Precedence कहते हैं।

Python में सबसे ज्यादा precedence Parentheses ( ) की होती है, उसके बाद Exponentiation (**) आता है।

जब एक ही precedence वाले कई operators एक साथ हों, तो उन्हें किस direction (Left to Right या Right to Left) में evaluate किया जाएगा, इसे Associativity कहा जाता है।

ज्यादातर operators Left to Right associativity follow करते हैं, लेकिन Exponentiation (**) Right to Left associativity follow करता है।

Parentheses की precedence सबसे ज्यादा होने की वजह से, इनके अंदर लिखा गया हिस्सा default precedence rules को नजरअंदाज करके सबसे पहले evaluate होता है, इसलिए programmer इनकी मदद से evaluation order अपने हिसाब से control कर सकता है।

Arpit Nageshwar

✍️ Arpit Nageshwar

Post-graduated | Web Developer | +3 yr Experience | IIT Kharagpur Certified