Lesson-1.2OperatorsArithmeticRelationalLogicalConventionExpressionsType of ExpressionsArithmetic ExpressionsBoolean Expressions
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:
xxxxxxxxxx
141>>> 10 + 5
215
3>>> 10 - 5
45
5>>> 10 * 5
650
7>>> 10 / 5
82.0
9>>> 10 // 5
102
11>>> 10 % 5
120
13>>> 10 ** 5
14100000
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 when x
is divided by y
. For example, 8 // 3
is 2
.%
is called the modulus operator. x % y
gives the remainder when x
is divided by y
. For example, 10 % 3
is 1
. **
is called the exponentiation operator. x ** y
returns ./
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:
xxxxxxxxxx
41>>> - 2
2-2
3>>> + 2
42
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:
xxxxxxxxxx
31>>> - 1 # unary minus
2-1
3>>> 1 - 1 # subtraction operator
Sometimes both of them could come together in the same expression:
xxxxxxxxxx
41>>> 1 - - 1
22
3>>> # The minus on the left is subtraction
4>>> # 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:
xxxxxxxxxx
41>>> x = 1
2>>> y = x * 5
3>>> print(x, y)
41 5
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:
xxxxxxxxxx
121>>> 10 > 5
2True
3>>> 10 < 5
4False
5>>> 10 >= 5
6True
7>>> 10 <= 5
8False
9>>> 10 == 5
10False
11>>> 10 != 5
12True
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:
xxxxxxxxxx
51>>> x = 10
2>>> y = 15
3>>> z = y > x
4>>> print(z)
5True
The ==
symbol corresponds to the equality operator and should not be confused with =
, the assignment operator.
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:
xxxxxxxxxx
81>>> True and False
2False
3>>> True or False
4True
5>>> x = False
6>>> y = not x
7>>> print(y)
8True
The use of parenthesis after not
is optional. For example:
xxxxxxxxxx
61>>> x = True
2>>> not x
3False
4>>> x = False
5>>> not(x)
6True
Consider the following lines of code:
xxxxxxxxxx
41>>> print(1 + 2)
23
3>>> print(1+2)
43
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.
xxxxxxxxxx
31>>> x = 2 # We will follow this
2>>> x=2 # We will NOT follow this
3# But both conventions are valid
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:
int
or float
bool
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:
xxxxxxxxxx
21>>> 1 + 2
23
The way to check the type of this expression is to use the type()
function. For example, we have:
xxxxxxxxxx
41>>> 1 + 2
23
3>>> type(1 + 2)
4<class 'int'>
So far the interpreter's behaviour conforms to our intuition. Let us now change this code slightly:
xxxxxxxxxx
41>>> 1.0 + 2
23.0
3>>> type(1.0 + 2)
4<class 'float'>
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:
xxxxxxxxxx
101>>> type(7.0 * 5)
2<class 'float'>
3>>> type(7.0 / 5)
4<class 'float'>
5>>> type(7.0 // 5)
6<class 'float'>
7>>> type(7.0 ** 5)
8<class 'float'>
9>>> type(7.0 % 5)
10<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.
Expressions that involve a relational operator will result in a bool
. For example:
xxxxxxxxxx
41>>> 2 > 1
2True
3>>> type(2 > 1)
4<class 'bool'>
Expressions that involve logical operators will naturally result in a bool
. For example:
xxxxxxxxxx
41>>> True and False
2False
3>>> type(True and False)
4<class 'bool'>
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:
xxxxxxxxxx
11>>> X or Y
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 |