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

Python Increment और Decrement

Python में plus-plus या minus-minus नहीं है; इसकी बजाय आप plus-equals या minus-equals से एक जोड़ते या घटाते हैं। यह हर बार कुछ होने पर counter में एक tally mark जोड़ने जैसा है।
Syntax
python
variable += 1  # increment
variable -= 1  # decrement

Python में ++ या -- नहीं

Python में जान-बूझकर ++ या -- operator नहीं है — x++ लिखना syntax error है, अप्रत्याशित व्यवहार वाला काम करता statement नहीं। किसी variable को बढ़ाने या घटाने का मानक तरीका इसके बजाय x += 1 या x -= 1 है।

उदाहरण: No ++ or -- in Python

python
x = 5
# x++  # SyntaxError: invalid syntax
x += 1
print(x)

क्रमिक जोड़

+= आपको सिर्फ़ एक नहीं, कोई भी मात्रा एक ही चरण में जोड़ने देता है: score += 10 अलग जोड़ और दोबारा assign की line के बिना तुरंत score को नए योग के साथ अपडेट कर देता है।

उदाहरण: Incremental Addition

python
score = 0
score += 10  # add 10 to score in one step
print(score)

क्रमिक घटाव

-= घटाव के लिए += का प्रतिबिंब है, जो आमतौर पर बचे हुए प्रयासों, जीवनों या retries को हर बार किसी निश्चित या गणना की गई मात्रा से घटाकर गिनती उलटी करने में इस्तेमाल होता है।

उदाहरण: Decremental Subtraction

python
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

python
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

python
value = 4
value *= 2  # scale value up
print(value)
value /= 4  # scale value down
print(value)
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. #}

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.