Lesson-1.2
Operators
Arithmetic Operators
The anatomy of an operation is given below:
The following table gives the symbols for arithmetic operators and the operations that they correspond to:
Operator | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
// | Floor division |
% | Modulus |
** | Exponentiation |
All the operators in the above table are binary, i.e., they operate on two operands. Let us now take a look at each operator:
>>> 10 + 5
15
>>> 10 - 5
5
>>> 10 * 5
50
>>> 10 / 5
2.0
>>> 10 // 5
2
>>> 10 % 5
0
>>> 10 ** 5
100000
The last three operators might be new. In more familiar terms, these are the mathematical operations that they correspond to:
//
is called the floor division operator.x // y
gives the quotient whenx
is divided byy
. For example,8 // 3
is2
.%
is called the modulus operator.x % y
gives the remainder whenx
is divided byy
. For example,10 % 3
is1
.**
is called the exponentiation operator.x ** y
returns \(x^y\).
/
and //
are two different operators. /
gives the complete result of division, while //
returns the quotient. For example, 5 / 2
results in 2.5
while 5 // 2
gives 2
. There are two more arithmetic operators of interest to us, unary plus and unary minus. These are the +
and -
signs. Unlike the operators that we have seen so far, these two are unary operators, i.e., they operate on one operand. For example:
It is important to note that the symbols for plus and minus operators are the same as the ones for addition and subtraction. The context determines the nature of the operator:
Sometimes both of them could come together in the same expression:
>>> 1 - - 1
2
>>> # The minus on the left is subtraction
>>> # The minus on the right is unary minus
In all the operations that we have seen so far, the operands have been literals. In general, the operands can also be variables:
Relational Operators
The following table gives the symbols for relational operators and the operations that they correspond to:
Operator | Operation |
---|---|
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
== | double equal to |
!= | not equal to |
All the operators in the above table are binary. Let us now take a look at each of them:
>>> 10 > 5
True
>>> 10 < 5
False
>>> 10 >= 5
True
>>> 10 <= 5
False
>>> 10 == 5
False
>>> 10 != 5
True
Relational operators are also called comparison operators. The result of any comparison operation is a boolean value: True
or False
. The result of a comparison operation can be assigned to a variable:
The ==
symbol corresponds to the equality operator and should not be confused with =
, the assignment operator.
Logical Operators
The following table gives the logical operators and the operations that they correspond to:
Operator | Operation |
---|---|
not | negation |
and | logical conjunction |
or | logical disjunction |
and
and or
are binary operators; not
is a unary operator. Let us now take a look at each of them:
The use of parenthesis after not
is optional. For example:
Convention
Consider the following lines of code:
Both lines 1 and 3 give the same output. Line-1 has a space before and after the +
operator, while line-3 doesn't. Both ways are syntactically correct. In this course, we will be following the first convention: there is always a space separating the operator from the operands. This is also true for the =
operator.
Operator Chaining
Python supports chaining relational operators. This enables you to evaluate chains of comparison without having to use logical operators.
Expressions
An expression is some combination of literals, variables and operators. For example, the following are expressions:
1 + 4 / 4 ** 0
x / y + z * 2.0
3 > 4 and 1 < 10
not True and False
Each expression evaluates to some value. This value has a type. In the above examples, the first two expressions result in a float
, while the next two expressions result in a bool
. In the next few sections, we shall study two types of expressions:
- Arithmetic: an expression whose type is either
int
orfloat
- Boolean: an expression whose type is
bool
Types of Expressions
Arithmetic Expressions
Let us now look at the type
of simple arithmetic operations. In mathematics, the result of adding two integers is another integer. Is this true in the case of Python? First, let us execute the following statement in the interpreter and see what we get:
The way to check the type of this expression is to use the type()
function. For example, we have:
So far the interpreter's behaviour conforms to our intuition. Let us now change this code slightly:
We see that the result is 3.0
which is of type float
. The conclusion is that float
is more dominant than int
as far as the addition operation is concerned. What about other operations? Let us check with the help of the following examples:
>>> type(7.0 * 5)
<class 'float'>
>>> type(7.0 / 5)
<class 'float'>
>>> type(7.0 // 5)
<class 'float'>
>>> type(7.0 ** 5)
<class 'float'>
>>> type(7.0 % 5)
<class 'float'>
All the operations result in a float
. From this we see that float
is more dominant than int
, irrespective of the operator involved.
Boolean Expressions
Expressions that involve a relational operator will result in a bool
. For example:
Expressions that involve logical operators will naturally result in a bool
. For example:
One way to analyze the outcome of boolean expressions that involve variables is to exhaustively list down the different combinations of values that variables can take and evaluate the expression for each such combination. For example, assume that X and Y are two boolean variables. Now, consider the following expression:
We can take the help of a concept called truth table to analyze the outcomes:
X | Y | X or Y |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |