Arithmetic Shortcuts
Part of the Fundamentals section of Coddy's Python journey — lesson 12 of 77.
Python created a cool shortcut for self-arithmetic operations.
For example, instead of writing:
a = 5
a = a + 3 # a holds 8We can simplify it by writing +=:
a = 5
a += 3 # a holds 8The += operator adds the value 3 to a.
This operation is valid for all arithmetic operations:
| Operator | Shortcut |
|---|---|
| + | += |
| - | -= |
| * | *= |
| / | /= |
| % | %= |
Challenge
BeginnerYou are given a code with initialization of count. (Don't delete this line!)
Your task is to add the following operations, in this order:
- Add
4tocount - Multiply
countby2 - Subtract
1fromcount
Use the arithmetic shortcuts to do so!
Cheat sheet
Python provides shortcuts for self-arithmetic operations:
| Operation | Shortcut | Example |
|---|---|---|
| Addition | += | a += 3 (equivalent to a = a + 3) |
| Subtraction | -= | a -= 3 |
| Multiplication | *= | a *= 3 |
| Division | /= | a /= 3 |
| Modulus | %= | a %= 3 |
Try it yourself
# Type your code below
count = 0
# Don't change the line below
print(f"count = {count}")This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2