Decrease the value of a variable #
Compound subtraction is a shorthand way of subtracting a value from a variable and then assigning the result back to the variable. It is achieved by using the -= operator.
For example:
x = 20
x -= 1 # equivalent to x = x - 1
print(x) # output: 19
Exercise #
- Rewrite the code on line 3 to use
-=to increase the value ofxby1 - Value of
xshould be19
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules