Python Increment और Decrement
In this page:
variable += 1 # increment
variable -= 1 # decrement
Python में ++ या -- नहीं
Python में जान-बूझकर ++ या -- operator नहीं है — x++ लिखना syntax error है, अप्रत्याशित व्यवहार वाला काम करता statement नहीं। किसी variable को बढ़ाने या घटाने का मानक तरीका इसके बजाय x += 1 या x -= 1 है।
उदाहरण: No ++ or -- in Python
x = 5
# x++ # SyntaxError: invalid syntax
x += 1
print(x)
क्रमिक जोड़
+= आपको सिर्फ़ एक नहीं, कोई भी मात्रा एक ही चरण में जोड़ने देता है: score += 10 अलग जोड़ और दोबारा assign की line के बिना तुरंत score को नए योग के साथ अपडेट कर देता है।
उदाहरण: Incremental Addition
score = 0
score += 10 # add 10 to score in one step
print(score)
क्रमिक घटाव
-= घटाव के लिए += का प्रतिबिंब है, जो आमतौर पर बचे हुए प्रयासों, जीवनों या retries को हर बार किसी निश्चित या गणना की गई मात्रा से घटाकर गिनती उलटी करने में इस्तेमाल होता है।
उदाहरण: Decremental Subtraction
lives = 3
lives -= 1 # subtract 1 from lives in one step
print(lives)
List Items को बढ़ाना
list में रखे किसी value को अपडेट करने के लिए, पहले उसे index से एक्सेस कीजिए और फिर उसी indexed स्थान पर सीधे compound operator लगाइए, जैसे scores[0] += 5, जो उस अकेले list slot को वहीं पढ़ता और दोबारा लिखता है।
उदाहरण: Incrementing List Items
scores = [10, 20, 30]
scores[0] += 5 # read and update the value at index 0 in place
print(scores)
अन्य संयुक्त Assignments
वही compound पैटर्न गुणा और भाग तक फैलता है — *= variable को बढ़ाता है और /= घटाता है — जिससे Python को "यह operation करो, फिर नतीजा वापस store करो" के लिए एकसमान शॉर्टहैंड परिवार मिल जाता है।
उदाहरण: Other Combined Assignments
value = 4
value *= 2 # scale value up
print(value)
value /= 4 # scale value down
print(value)
Chapter Quiz — Complete all 16 topics to unlock
0/16 topics done
Complete these topics first:
- Python print()
- Python input()
- Python Format Strings
- Python f-strings
- Python String Formatting
- Python Arithmetic Operators
- Python Relational Operators
- Python Logical Operators
- Python Bitwise Operators
- Python Assignment Operators
- Python Increment & Decrement
- Python Ternary Operator
- Python Operator Precedence
- Python Identity Operators
- Python Membership Operators
- Python Operators