Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views12 pages

Python Operators

The document provides an overview of Python operators, categorizing them into arithmetic, assignment, comparison, logical, identity, and membership test operators. It explains the function of each operator with examples, illustrating how they operate on operands and return results. Additionally, it highlights the use of the operator module for performing these operations programmatically.

Uploaded by

arnavkumar90063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Python Operators

The document provides an overview of Python operators, categorizing them into arithmetic, assignment, comparison, logical, identity, and membership test operators. It explains the function of each operator with examples, illustrating how they operate on operands and return results. Additionally, it highlights the use of the operator module for performing these operations programmatically.

Uploaded by

arnavkumar90063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Operators:

Operators are special symbols that perform some operation on operands and
returns the result.

For example, 10 + 6 is an expression where + is an operator that performs


arithmetic add operation on numeric left operand 10 and the right side
operand 6 and returns a sum of two operands as a result.

the operator module is also used for the operations. For example, the +
operator calls the operator.add(a,b) method.
Categories
• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Logical Operators
• Identity Operators
• Membership Test Operators
Arithmetic operators perform the common mathematical
operation on the numeric operands.
Operand may be either complex, float or integer
Operation Operator Function Example
Addition: Sum of + operator.add(a,b) >>> x = 5; y = 6
two operands >>> x + y
11
>>> import operator
>>> operator.add(5,6)
11
Subtraction: Left - operator.sub(a,b) >>> x = 10; y = 5
operand minus >>> x - y
right operand 5
>>> import operator
>>> operator.sub(10, 5)
5
Multiplication * operator.mul(a,b) >>> x = 5; y = 6
>>> x * y
30
>>> import operator
>>> operator.mul(5,6)
30
Exponentiation: Left ** operator.pow(a,b) >>> x = 2; y = 3
operand raised to the >>> x ** y
power of right 8
>>> import operator
>>> operator.pow(2, 3)
8
Division / operator.truediv(a,b) >>> x = 6; y = 3
>>> x / y
2
>>> import operator
>>> operator.truediv(6, 3)
2
Floor // operator.floordiv(a,b) >>> x = 6; y = 5
division: equivilant >>> x // y
to math.floor(a/b) 1
>>> import operator
>>> operator.floordiv(6,5)
1
Modulus: Remind % operator.mod(a, b) >>> x = 11; y = 3
er of a/b >>> x % y 2
>>> import operator
>>> operator.mod(11, 3)
2

Operator Function Example


= >>> x = 5;
>>> x
+= operator.iadd(a,b) >>> x = 5
>>> x += 5
10
>>> import operator
>>> x = operator.iadd(5, 5)
-= operator.isub(a,b) >>> x = 5
>>> x -= 2
3
>>> import operator
>>> x = operator.isub(5,2)
*= operator.imul(a,b) >>> x = 2
>>> x *= 3
6
>>> import operator
Assignment Operator are used to assign values to the variables
Operator Function Example
/= operator.itruediv(a,b >>> x = 6
) >>> x /= 3
2
>>> import operator
>>> x = operator.itruediv(6, 3)
//= operator.ifloordiv(a,b >>> x = 6
) >>> x //= 5 1
>>> import operator
>>> operator.ifloordiv(6,5)

%= operator.imod(a, b) >>> x = 11
>>> x %= 3 2
>>> import operator
>>> operator.imod(11, 3)
2
The comparison operators compare two operands and return a boolean either
True or False.

Operator Function Description Example


> operator.gt(a,b) True if the left operand is >>> x = 5; y = 6
higher than the right one >>> x > y False
>>> import operator
>>> operator.gt(5,6) False
< operator.lt(a,b) True if the left operand is >>> x = 5; y = 6
lower than right one >>> x < y True
>>> import operator
>>> operator.lt(5,6) True
== operator.eq(a,b) True if the operands are >>> x = 5; y = 6
equal >>> x == y False
>>> import operator
>>> operator.eq(5,6) False
!= operator.ne(a,b) True if the operands >>> x = 5; y = 6
are not equal >>> x != y True
>>> import operator
>>> operator.ne(5,6)
True

>= operator.ge(a,b) True if the left >>> x = 5; y = 6


operand is higher >>> x >= y False
than or equal to the >>> import operator
right one >>> operator.ge(5,6)
False

<= operator.le(a,b) True if the left >>> x = 5; y = 6


operand is lower >>> x <= y True
than or equal to the >>> import operator
right one >>> operator.le(5,6)
True
The logical operators are used to combine two boolean
expressions.

Operator Description Example


and True if both are true >>> x = 5; y = 6
>>> x > 1 and y <10
True

or True if at least one is true >>> x = 5; y = 6


>>> x > 6 or y <10
True

not Returns True if an expression >>> x = 5


evalutes to false and vice- >>> not x > 1 False
versa
The identity operators check whether the two objects have the
same id value i.e. both the objects point to the same memory
location.

Operator Function Description Example


is operator.is_(a,b) True if both are >>> x = 5; y = 6
true >>> x is y False
>>> import operator
>>> operator.is_(x,y) False
is not operator.is_not(a True if at least >>> x = 5; y = 6
,b) one is true >>> x is not y True
>>> import operator
>>> operator.is_not(x, y) True
The membership test operators in and not in test whether the sequence has a
given item or not.
Operator Function Description Example in Python Shell
in operator.contains(a,b) Returns True if >>> nums = [1,2,3,4,5]
the sequence >>> 1 in nums True
contains the >>> 10 in nums False
specified item >>> 'str' in 'string' True
else returns >>> import operator
False. >>> operator.contains(nums, 2) True

not in not operator.contains(a,b) Returns True if >>> nums = [1,2,3,4,5]


the sequence >>> 1 not in nums False
does not >>> 10 not in nums True
contains the >>> 'str' not in 'string' False
specified item, >>> import operator
else returns >>> not operator.contains(nums, 2) False
False.

You might also like