You are on page 1of 10

07-04-2023

Operators and Expressions in


Python
Dr. Kusum Lata
Assistant Professor
USME, East Delhi Campus
Delhi Technological University

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

Operators and Expressions


An expression in Python is a block of code that produces a result
or value upon evaluation. A simple example of expression is 3+8.
An expression can be broken into operators and operands.
Operators are symbols which help the user or command computer
to perform a mathematical or logical operation.
In expression 3+8; 3, 8 are operand and + is operator.
Operator requires data to operate and this data is called operand.

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))

Arithmetic operators: Binary


Binary operators require two operands. They are written in infix form i.e.,
operator is written between two operands.
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y

3
07-04-2023

Arithmetic operators: Binary


Syntax and semantics of the addition operator in python using its 3
numeric types i.e., int, float and complex.

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

Arithmetic operators: Binary


The ‘-’ operator in Python can be used with binary and unary form. If
subtraction operator is applied in between two operands, the result is
returned as the arithmetic difference of the operands.
Example:>>>4-7 #subtraction
Syntax and semantics of the subtraction operator in python using its 3
numeric types i.e., int, float and complex.
Syntax Example
(int, int)-> int 2-4 returns -2
(int, float-> float 1-3.0 returns -2.0
(float, int)-> float 3.0-1.0 returns 2.0
(float, float)-> float 4.0-8.0 returns -4.0
(complex, complex)->complex 2-3j returns 2-3j

4
07-04-2023

Arithmetic operators: Binary


The ‘*’ operator in Python can be used with binary form only. If
subtraction operator is applied in between two operands, the result is
returned as the arithmetic product of the operands.
Example:>>>4*7 #multiplication
Syntax and semantics of the multiplication operator in python using its 3
numeric types i.e., int, float and complex.
Syntax Example
(int, int)-> int 2*4 returns 8
(int, float-> float 1*3.0 returns 3.0
(float, int)-> float 3*1.0 returns 3.0
(float, float)-> float 4.0*8.0 returns 32.0
(complex, complex)->complex 2*3j returns 6j

Arithmetic operators: Binary


The division operator ‘/’ operator in Python can be used with binary form
only. If division operator is applied in between two operands, the result is
returned as the quotient of the operands.
Example:>>>10/3
3.33333333333 #division
Syntax and semantics of the division in python using its 3 numeric types
i.e., int, float and complex. Syntax Example
(int, int)-> float 25/5 returns 5.0
(float, float-> float 0.6/0.2 returns 0.3
(int, float)-> float 4/0.2.0 returns 20.0
(float, int)-> float 1.5/2 returns 0.75
(complex, complex)->complex 6j/2j returns 3+0j

5
07-04-2023

Arithmetic operators: Binary


The ‘//’ operator in Python can be used with binary form only. If floor
operator is applied in between two operands, the result is returned as the
quotient of the operands.
Example:>>>10//3
3
Syntax and semantics of the floor in python using its numeric types i.e.,
int, float. Syntax Example
(int, int)-> int 25/5 returns 5
(float, float-> float 10.0/2.0 returns 5.0
(int, float)-> float 11/2.5 returns 4.0
(float, int)-> float 4.0/3 returns 1.0

Arithmetic operators: Binary


The modulo ‘%’ operator in Python can be used with binary form only. If %
operator is applied in between two operands, the result is returned as the
reminder of the division
Example:>>>10%3
1
Syntax and semantics of the modulo in python using its numeric types i.e.,
int, float. Syntax Example
(int, int)-> int 25%5 returns 0
(float, float-> float 2.5.0%1.2 returns 0.10
(int, float)-> float 13%2.0 returns 1.0
(float, int)-> float 1.5%2 returns 1.5

6
07-04-2023

Arithmetic operators: Binary


The exponent ‘**’ operator in Python is used to calculate the power of a
number. It is also called power operator.
example:>>>2**3
8
Syntax and semantics of the exponent in python using its numeric types
i.e., int, float.
Syntax Example
(int, int)-> int 2**5 returns 32
(float, float-> float 2.0**3.0 returns 8.0
(int, float)-> float 5**2.0 returns 25.0
(float, int)-> float 4.0**3 returns 64.0

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.

Membership Description Example


operator
in Returns true if values in left >>>str=“ hello how r
operand appears in sequence you”
found in right operand >>>”Hello” in str
False
not in Returns true if values in left >>>str=“hello how r
operand does not appears in you”
sequence found in right operand >>>”Hello” not in str
True

7
07-04-2023

Identity operator
The identity operator is used to check whether an expression is of certain
type or class.

Identity operator Description Example

is Returns true if the type of the >>> x=10


value in left operand is of same >>>type(x) is int
type as that of value present in True
the right operand.
is not Returns true if the type of the >>> x=10.5
value in left operand is not of >>>type(x) is not int
same type as that of value True
present in the right operand.

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

Changing precedence and Associativity


One can change the precedence and associativity of the arithmetic
operators by using (). The () operator has higher precedence and it can be
used to force an expression to evaluate in any order. () makes an
expression more readable.
Example:
>>>x=(5+6)*10
>>>x
110
>>>a=4+(5*(4/2)+(4+3))
>>>a
21

Translating mathematical expression into


equivalent Python expressions
Consider the following formula:
(i)
𝑏 − 𝑏 2 − 4𝑎𝑐
2𝑎
2+8𝑝 𝑝−𝑞 𝑝+𝑞 (𝑝+𝑞)
(ii) − −4∗
2 2 2

10

You might also like