Operators
Operators are defined on types and return a new value, usually of the same but sometimes of a different type. The most familiar are the arithmetic operators + - * /
(addition, subtraction, multiplication, division). Python also provides an exponentiation operator **
, e.g. a**b
. Python also accepts pow(a,b)
for exponentiation.
The equal sign (=) is an assignment operator in Python. It does not indicate equality; rather it assigns a value to a variable. Since variables represent locations in memory, the assignment tells the interpreter to place the value into that memory location. This means that a mathematically nonsensical expression like
x=x+1
is perfectly correct Python. The interpreter fetches the value of x from memory, adds one to it, and stores the result back into the same memory location.
Python supports add/assign and similar operators. Thus
x+=1
is the same thing as
x=x+1
Experienced programmers tend to use operator/assignment (+=, -=, *=, /=) but it is not required.
Integer Operators
The arithmetic operators are defined on all numerical types. Two additional operators are generally or always used to return integers.
- Integer division
//
returns the integer part of the division. For example,7//2=3
. - The mod or modulo operator
%
returns the remainder of the division, so7%2=1
.
These operators are also defined on floating-point numbers, but the results may not be what you expect! Also avoid using %
with negative numbers, for the same reason.
Special note for Python 2.7 users: In Python 2.7, 2/3=0
. This is because each operand is an integer, so the operator returns an integer. However, in Version 3 and up, 2/3=0.6666666666666666
(this value is the result of an expression evaluation). Python 2.7 users who want the newer behavior should begin each file with
from __future__ import division
If you’d like the new print function as well, modify this line to
from __future__ import division, print_function
Exercises
Type or paste into your choice of Spyder’s interpreter pane or a JupyterLab cell the following assignments.
x=17.
Xs=11.
num_1=10
num_2=14
Type the following lines one at a time and examine the results. In JupyterLab each line must be in its own cell. In the Spyder interpreter pane it will evaluate each line when you go to the next one.
x
Xs/x
Xs//x
Xs/x+x
Xs/(x+x)
x/num_1
num_1/num_2
num_2/num_1
Use the same method of typing one line at a time (using a separate cell for each in Jupyter) to study the outcome of the following operations:
4+2*3
(4+2)*3
20/4*5
20/(4*5)
.1+.2
5//2
5//-2
11//3
11.4//3.5
11%3
11.4%3.5 #?
11.4-(11.4//3.5)*3.5
Boolean Operators
Boolean operators operate on Boolean expressions.
- Negation
not someVar
- AND
someBool and anotherBool
- OR
someBool or anotherBool
- Warning: in Python
or
is nonexclusive. The expression is True if either or both are True. - This is different from the usual meaning of or in natural languages. “You can have ice cream or cake” usually implies “but not both.” But in Python,
if ice_cream_OK or cake_OK:
is true if both are True.
- Warning: in Python
Comparison Operators
Comparison operators operate on other types but return Boolean values. They are also called conditional operators or relational operators.
Comparison operators represent relationships between two or more variables. They can be defined on any type, but arithmetic and string comparisons are the most common.
Arithmetic Comparison Operators
- Equal, not equal. Note that equality is a double equals sign.
- ==, !=
- Less than, greater than, less than or equal to, greater than or equal to
- <, >, <=, >=
Chaining
In Python we can write an expression like
0<a<=1
just as in the analogous mathematical expression. An and
operator is always assumed. This chain is equivalent to
0<a and a<=1