In Progress
Lesson 1, Topic 12
In Progress

# Python: Shorthand Operators

##### Yasin Cakal 5 Oct 2021
Lesson Progress
0% Complete

Python provides a convenient shortcut for updating variables. When you have a variable with a value and want to add it to the variable’s current value, you can use the += (plus-equals) operator. Here are some examples:

``````# Because, you haven't ordered any food yet.
restaurant_bill = 0

appetizer = 9
meal = 14
desert = 7

# You ordered appetizer first
restaurant_bill += appetizer
print("Your total so far is ", restaurant_bill)

# Then you ordered meal and desert.
restaurant_bill += meal + desert
print("Your grand total is ", restaurant_bill)``````

Output:

```Your total so far is  9

As you can see above, you can use the shorthand operator to add two things simultaneously to the variable. The shorthand operator can also be used with other arithmetic operators such as subtraction, multiplication, division, exponent, even modulo. Here are some more examples:

``````x = 10
x -= 2 # Now x = 8
x *= 2 # Now x = 16
x /= 2 # Now x = 8
x **= 2 # Now x = 64
x %= 30 # Now x = 4``````

Additionally, the plus-equals operator can be used to concatenate strings, as follows:

``````string = "Hello"
string += " " + "World" + "!"

# Prints "Hello World!"
print(string) ``````

Quizzes