CHAPTER 3

image

Operators

Python supports several operators. Here, we review some of the key ones.

Arithmetic Operators

We use the arithmetic operators to do simple math on two variables or literals. Be sure to only try to use arithmetic operators on like types. For example, if you try to add a string value to a integer (3 + “ is a number”) you will get an error.

+ Addition

Adds the left value to the right:

>>> lvariable = 5>>> sum = lvariable + 3>>> sum8

− Subtraction

Subtracts the right value from left:

>>> lvariable = 5>>> difference = lvariable - 3>>> difference2

* Multiplication

Multiplies left value by right:

>>> lvariable = 5>>> product ...

Get The Python Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.