Python Operators Explained with Examples, Lecture notes of Semantics of Programming Languages

A comprehensive overview of python operators, including arithmetic, assignment, comparison, logical, identity, and membership test operators. Each operator is explained with examples, demonstrating its function and usage. The document also references the 'operator' module, enhancing understanding of operator implementation in python. It serves as a useful guide for understanding basic python programming concepts, suitable for beginners and intermediate learners. It includes code snippets and explanations.

Typology: Lecture notes

2024/2025

Available from 11/24/2025

prateek-7
prateek-7 🇮🇳

3 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python Operators Explained with Examples and more Lecture notes Semantics of Programming Languages in PDF only on Docsity!

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

Multiplicatio n

  • operator.mul(a,b) >>> x = 5 ; y = 6

x * y 30 import operator operator.mul( 5 , 6 ) 30

Exponentiation: Left operand raised to the power of right ** operator.pow(a,b) >>> x = 2 ; y = 3

x ** y 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 division: equivila nt to math.floor(a/b) // operator.floordiv(a, b) x = 6 ; y = 5 x // y 1 import operator operator.floordiv( 6 , 5 ) 1

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 higher than the right one

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

The logical operators are used to combine two boolean

expressions.

Operator Description Example

and True if both are true >>> x = 5 ; y =

>>> x > 1 and

y < 10 True

or True if at least one is

true

>>> x = 5 ; y =

>>> x > 6 or y

< 10 True

not Returns True if an

expression evalutes to

false and vice-versa

>>> x = 5

>>> not x > 1

False

The identity operators check whether the two objects have the

same id value i.e. both the objects point to the same memory

location.

Operat

or Function Description Example

is operator.is_(a

,b)

True if both

are true

>>> x = 5 ; y = 6

>>> x is y False

>>> import operator

>>> operator.is_(x,y)

False

is not operator.is_no

t(a,b)

True if at

least one is

true

>>> x = 5 ; y = 6

>>> x is not y True

>>> import operator

>>> operator.is_not(x,

y) True