Professional Documents
Culture Documents
Introduction
An operator indicates an operation to be performed on data to yield a
result. Python supports different operators which can be used to link
variables and constants.
• Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
1
07-04-2023
Arithmetic operators
There are two type of arithmetic operators in Python.
I. Unary
II. Binary
Unary operators perform mathematical operations on one operand only.
The “+” and “-” are two unary operators.
The unary operator “-” produces the negation of its numeric operand and
The unary operator “+” operator returns the numeric operand without any
change.
Examples:
>>>x = -5 //negates the value of x
>>>x
-5
2
07-04-2023
Arithmetic operators
>>>x= +8
>>>x
8 // returns the numeric operand.
More examples:
>>>1--3 # equivalent to 1-(-3)
>>>2---3 # equivalent to 2-(-(-3))
>>>3+--2 equivalent to 3+(-(-2))
3
07-04-2023
Syntax Example
(int, int)-> int 2+4 returns 6
(int, float-> float 1.0+3.0 returns 4.0
(float, int)-> float 3.0+1.0 returns 4.0
(float, float)-> float 4.0+8.0 returns 12.0
(complex, complex)->complex 2+3j returns 2+3j
4
07-04-2023
5
07-04-2023
6
07-04-2023
Membership operator
The membership operator is used to check presence or absence of values
within the sequence such as string, list, tuple, sets or dictionary.
7
07-04-2023
Identity operator
The identity operator is used to check whether an expression is of certain
type or class.
Operator Precedence
Operator precedence determines the order in the Python interpreter
evaluates the operators in an expression.
Consider the expression 4+5*3. This expression is evaluated to be 19.
An expression may contain a lot of operators, operations on the operators
are carried out according to the priority, also called precedence of the
operator. The following table gives the operator precedence in Python.
The operators on the top row have high precedence that the operators in
the bottom rows. If a row contains multiple operators, it means all the
operators are of the same precedence.
Example of operator precedence:
4+5*3-10 # multiplication is evaluated first; the + and – have same
priority, in this situation left most operation is evaluated.
8
07-04-2023
Operator Precedence
Operators Meaning
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, Identity, Membership operators
not Logical NOT
and Logical AND
or Logical OR
Associativity
When an expression contains operators with equal priority, the
associativity property decides which operation is be performed first.
Associativity implies the direction of execution and is of two types.
Left to right: in this type of expression the evaluation is executed from left
to right.
4+6-3+2→ (((4+6)-3)+2)
Right to left: in this type of expression the evaluation is executed from
right to left.
x=y=z=5
When operators of same priority are found in an expression, precedence
is given to the leftmost operator.
Z=4*6+8//2 # here * would be evaluated first, then // and then +.
9
07-04-2023
10