Home Lesson-1.3

Lesson-1.2

Operators

Arithmetic

The anatomy of an operation is given below:

The following table gives the symbols for arithmetic operators and the operations that they correspond to:

OperatorOperation
+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:

The last three operators might be new. In more familiar terms, these are the mathematical operations that they correspond to:

/ 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:

In all the operations that we have seen so far, the operands have been literals. In general, the operands can also be variables:

 

Relational

The following table gives the symbols for relational operators and the operations that they correspond to:

OperatorOperation
>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:

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

The following table gives the logical operators and the operations that they correspond to:

OperatorOperation
notnegation
andlogical conjunction
orlogical 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.

 

Expressions

An expression is some combination of literals, variables and operators. For example, the following are expressions:

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:

 

Type 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:

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:

XYX or Y
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse