This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 5: Operators and Expressions

Overloaded operators

face Josiah Wang

Some operators are not limited to arithmetic operations with numbers.

For example, + and * can be used for a different kind of operation when used with strs.

Try to guess the output value of the following expressions (enter “Error” if it is not a valid expression). Remember to put quotes around your output strings in your answer (e.g. "hello").

Feel free to test and experiment with an interactive prompt.

1 2 3 4 5 6 7 8 9 10

Question 1

What is the value of the following expression?

"imperial" + "college"

'imperialcollege'
Explanation:

The + operator concatenates (joins) two strings together.

Question 2

What is the value of the following expression?

"imperial" + 3

Error
Explanation:

Python returns a TypeError: can only concatenate str (not "int") to str. You cannot add an int to a str in Python. You can only concatenate two str instances.

Question 3

What is the value of the following expression?

"imperial" - "college"

Error
Explanation:

Python returns a TypeError: unsupported operand type(s) for -: 'str' and 'str'. Python is basically saying: you cannot use the "minus" operator with str operands.

Question 4

What is the value of the following expression?

"imperial" * 3

'imperialimperialimperial'
Explanation:

The multiplication operator actually works for strings. It will replicate the string N times.

Question 5

What is the value of the following expression?

3 * "imperial"

'imperialimperialimperial'
Explanation:

The multiplication operator also works with an integer as the first operand and a string as the second. Like the previous question, it will replicate the string N times.

Question 6

What is the value of the following expression?

"imperial" * "imperial"

Error
Explanation:

Alas, this does not work! What is the error returned by Python? (Try this in an interactive prompt!). In summary, you can only multiply a str by an int. You cannot multiply two str (what are you expecting to happen anyway?)

Question 7

What is the value of the following expression?

"3" * "imperial"

Error
Explanation:

Again, "3" is a str. You cannot multiply two strings.

Question 8

What is the value of the following expression?

"i am" + " " + "happy" * 3

'i am happyhappyhappy'
Explanation:

According to operator precedence, Python will first execute the multiplication operation "happy" * 3 resulting in "happyhappyhappy". It will then concatenate the three strings: "i am", " " (space), "happyhappyhappy", resulting in "i am happyhappyhappy".

Question 9

What is the value of the following expression?

"i am" * 3 + "happy"

'i ami ami amhappy'
Explanation:

According to operator precedence, Python will first execute the multiplication operation "i am" * 3 resulting in "i ami ami am" (note the lack of space). It will then concatenate the two strings resulting in "i ami ami amhappy".

Question 10

What is the value of the following expression?

("i am" + " " + "happy") * 3

'i am happyi am happyi am happy'
Explanation:

According to operator precedence, Python will first execute the expression inside the parenthesis, and so will concatenate the three strings resulting in "i am happy". It will then perform the multiplication and so will replicate the string, resulting in "i am happyi am happyi am happy".