Operators are the symbols used to work with variables. You’re already familiar with operators from simple arithmetic; plus and minus are operators. See Table 1.3 for the full list of operators.
Operator | What it does |
---|---|
x + y (Numeric) | Adds x and y together |
x + y (String) | Concatenates x and y together |
x - y | Subtracts y from x |
x * y | Multiplies x and y together |
x / y | Divides x by y |
x % y | Modulus of x and y (i.e., the remainder when x is divided by y) |
x++, ++x | Adds one to x (same as x = x + 1) |
x--, --x | Subtracts one from x (same as x = x - 1) |
-x | Reverses the sign on x |
✓ Tips
While both x++ and ++x add one to x, they are not identical; the former increments x after the assignment is complete, and the latter before. For example, if x is 5, y=x++ results in y set to 5 and x set to 6, while y=++x results in both x and y set to 6. The operator–- (minus sign) works similarly.
If you mix numeric and string values when adding two values together, the result is a string. For example, cat + 5 results in cat5.
18.223.195.101