Вы находитесь на странице: 1из 11

Introduction to Expression and

Operators
 An expression in Python is a block of code
that produces a result or value upon
evaluation.

 An expression can be broken down into


operators and operands.

 Operators are symbols help the user or command


computer to perform mathematical operations.

Example:
In the expression 6 + 3 the ‘+’ act as
operator. Where operator requires operands to
perform operations. Therefore 6 and 3 act as
operands.
Arithmetic Operators
There are two types of Arithmetic Operators
a) Unary Operator
b) Binary Operator
Unary Operators:
Unary arithmetic operators perform mathematical operations on one operand
only. The ‘+’ and ‘-’ are two unary operators.
Example:
>>> x = -5 #Negates the value of X
>>> x
-5
Binary Operators
Binary operators are operators which require two operands .
Operator is written in between two operands.
Example:
>>> 3 + 10
>>>13
>>> 10 – 7
>>> 3
Division(/) Operators and Floor
Division Operators
Division Operator
If the division operator is applied in between two
operands, it returns the result as the arithmetic quotient
of the operands.

Example:
>>> 4/2
>>> 2.0
>>> 2/3
0.6666666666666666
Floor Division (//) Operator
 If the floor division operator is applied in
between two operands, it returns the result as the
arithmetic quotient of the operands .

 Python floor division (//) operator when applied


on two int operands, Python returns an int result.

Example:
>>> 9//4
2
Modulo (%) Operator
When the second number divides the first number,
the modulo operator returns the remainder.

Example:
>>> 21 % 9
3

Note:
X%Y is equivalent to X – Y * (x//Y)
The Exponent ** Operator
 The ‘**’ exponent operator is used to calculate the
power or exponent of a number

 To compute xY (X raised to Y), the expression is


written as X**Y.

Example
>>> 5**4
625
Operator Precedence and Associativity

 The operator precedence determines the order in which the


python interpreter evaluates the operators in an expression.

 Where as associativity property decides which operation to


be performed first.

 Associativity is of two types.


a) Left to Right
Example:
4 + 6 – 3 + 2 = 9

b) Right to Left
X = Y = Z = Value
Note:
When the operators of same priority are found in the
expression, precedence is given to the left most operator.
Associativity Continued….

Example:
Z = 4 * 6 + 8 // 2
= 28
In the above expression * is evaluated first, even though * and // have the
same priorities. The operator * occurs before // and hence the evaluation
starts from left to right.

Precedence Operators Associativity


Highest () Innermost to Outermost

  ** Highest
Asociativity table for Arithmetic Operators
*,/,//,% Left to Right
+ - Left to Right
Lowest
Bitwise Operators
 Python has six bitwise operators.
 Python support bitwise operator for bit wise
manipulation.
 It allows the programmer to access and manipulate
individual bits within a piece of data.
 Following are the six bitwise operators supported
by

& Bitwise AND


| Bitwise OR
^ Bitwise XOR
>>  Right Shift
<<  Left Shift
~ Bitwise NOT
Compound Assignment Operator
 The operators +, *, //, /, % and ** are used with assignment
operator (=) to form compound or augmented assignment operator.

Example:
X = X + 1

But python allows programmer to combine assignment and addition


operator.

Thus the above statement X = X + 1 can also be written as


X + = 1

Various other Compound Assignment operators are as follows


*= , /= //=, %=, **=, -= , and !=
Conclusion……
 Various binary operators such as addition (+),
subtraction (-), multiplication (*), integer division,
floor division, modulus operators (%), and exponent (**)
are covered.

 Operator precedence and Associativity is explained


in brief.

 Programmer has also came to know various augmented


operators.