4.8. Other Operators

Earlier, you learned how to assign, and then reassign a variable:

1
2
3
4
day = "Thursday"
print(day)
day = "Friday"
print(day)

Console Output

Thursday
Friday

One of the most common forms of this involves making the new value of the variable depend on the old value.

Example

1
2
3
4
my_number = 10
print(my_number)
my_number = my_number + 2
print(my_number)

Console Output

10
12

On line 3, the new value of my_number becomes its old value plus 2.

4.8.1. Updating Variables

The statement my_number = my_number + 2 takes the current value of my_number, increases it by 2, then reassigns the result back to my_number.

Think of the statement as a series of steps:

my_number = my_number + 2
my_number = 10 + 2         # Insert the old value of my_number
my_number = 12             # Do the math and assign the result to my_number

This type of update is so common in Python (and programming in general) that we have an operator to use as a shortcut. Another way to write my_number = my_number + 2 is below:

my_number += 2

The operator += increases the value of my_number by 2.

+= always increases the value of a first operand by the amount of the second.

+= is an example of a compound assignment operator, or an operator that performs two actions in the same statement. These actions are a calculation and a variable assignment. The table below summarizes four examples of compound assignment operators.

Compound Assignment Operators

Operator

Meaning

a += b

a = a + b

a -= b

a = a - b

a *= b

a = a * b

a /= b

a = a / b

4.8.2. String Operators

So far, we have studied operators that work on numbers, but there are operators that work on other data types as well. In particular, the + and * operators can be used with strings.

4.8.2.1. Try It!

Let’s compare using + and * with numbers vs. strings.

Examples

Run the following code and examine the output.

Try changing the int and str values to see what happens!

These examples show that the + and * operators behave differently based on the data type of the operands.

  1. For int and float data types, + adds two numbers together and returns the result.

    2 + 3 returns 5.

  2. For the str data type, + attaches the second string to the end of the first and returns the new, longer string.

    'Launch' + 'Code' returns 'LaunchCode'.

  3. For int and float data types, * multiplies two numbers together and returns the result.

    12 * 3 returns 36.

  4. Between the str and int data types, * performs a repetition. 'Fun' * 3 returns 'FunFunFun'.

    • The * operator acts like multiple + operators.

    • 'Fun' * 3 does the same thing as 'Fun' + 'Fun' + 'Fun'.

Note

Combining strings together to form a new, longer string is called string concatenation.

What would this statement print? Paste it into the editor to see!

print('Python' + '!' * 3)

4.8.3. Check Your Understanding

Question

What is printed by the following statement?

1
2
3
first_word = "Python"
second_word = "ROCKS"
print(first_word + second_word)
  1. Python ROCKS
  2. PythonROCKS
  3. Python+ROCKS
  4. ROCKSPython

Question

What is printed by the following statement?

1
2
3
word = "Python"
excl = "!"
print(word + excl * 3)
  1. Python!!!
  2. PythonPythonPython!
  3. Python!Python!Python!
  4. PythonPythonPython!!!

Question

Which TWO of the following will print Python ROCKS!?

  1. print("Python" + "ROCKS" + "!")
  2. print("Python", "ROCKS", "!")
  3. print("Python", "ROCKS" + "!")
  4. print("Python" + "ROCKS", "!")
  5. print("Python " + "ROCKS" + "!")