[go: up one dir, main page]

0% found this document useful (0 votes)
61 views16 pages

Operators: Team Emertxe

Operators allow us to perform operations on values and variables in Python. There are several types of operators including arithmetic, assignment, comparison, logical, and bitwise operators. Operators follow specific precedence rules and are evaluated either from left to right or right to left. Common mathematical functions like square root are contained in Python's math module.

Uploaded by

aarthi dev
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)
61 views16 pages

Operators: Team Emertxe

Operators allow us to perform operations on values and variables in Python. There are several types of operators including arithmetic, assignment, comparison, logical, and bitwise operators. Operators follow specific precedence rules and are evaluated either from left to right or right to left. Common mathematical functions like square root are contained in Python's math module.

Uploaded by

aarthi dev
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/ 16

Operators

Team Emertxe
Arithmetic
OPERATORS
Arithmetic
Operator Example Result

+ a+b 18

- a-b 8

* a*b 65

/ a/b 2.6

% a%b 3

** a ** b 371293

// a // b 2

The results are obtained for the values of:


a = 13
b = 5
OPERATORS
Assignment
Operators Example-1:

= a = b = 1

+=

-+ Example-2:

*+ a = 1; b = 1

/=

%= Example-3:

**=
a, b = 1, 2

//=

Python does not have ++ AND -- operators


OPERATORS
Unary Minus
Example-1:

n = 10
print(-n)

Example-2:
num = -10
num = -num

print(num)
OPERATORS
Relational
Operator Example Result

> a > b False

>= a >= b False

< a < b True

<= a <= b True

== a == b False

!= a != b True

The results are obtained for the values of:


a = 1
b = 2
OPERATORS
Relational: Chaining
Example-1:
x = 15
print(10< x < 20)

Example-2:

print(1 < 2 < 3 < 4)


OPERATORS
Logical
If a = 100, b = 200
Operator Example Result

and a and b 2

or a or b 1

not not a False

Example-1: Example-2:
if (a < b and b < c): if (a > b or b < c):
print("Yes") print("Yes")
else: else:
print("No") print("No")

Short Circuit evaluation implies to Logical Operators


OPERATORS
Boolean
If a = True, b = False
Operator Example Result

and a and b False

or a or b True

not not a False

Example-1:
print(a and b)
print(a or b)
print(not a)
OPERATORS
Bitwise
If a = 10(0000 1010), b = 11(0000 1011)
Operator Example Result

~ ~a 1111 0101(-11)

& a & b 0000 1010(10)

| a | b 0000 1011(11)

^ a ^ b 0000 0001(1)

<< a << 2 0010 1000(40)

>> a >> 2 0000 0010(2)

In case of >> shifting, it preserves the sign of the number.


OPERATORS
Membership
Operator Description

in Returns True, if an item is found in the specified sequence

not in Returns True, if an item is not found in the specified sequence

Example-1:
names = ["Ram", "Hari", "Thomas"]

for i in names:
print(i)

Example-2:
postal = {"Delhi": 110001, "Chennai": 600001, "Bangalore": 560001}

for city in postal:


print(city, postal[city])
OPERATORS
Identity

Use to comapre the memory locations of two objects

id(): Is used to get the memory location ID
Example-1:
a = 25
b = 25
if (a is b): #This compares only the locations
print("a and b are same")

Operator Description

is Returns True, if ID of two objects are same

is not Returns True, if ID of two objects are not same


OPERATORS
Identity

To compare two objects, use ‘==’ operator

Example-1:
a = [1, 2, 3, 4]
b = [1, 2, 3, 4]

if (a == b):
print("Objects are same")
else:
print("Objects are
not same")
OPERATORS
Precedence & Associativity
Operator Name
(expressions...), [expressions...], {key: Binding or tuple display, list display, dictionary
value...}, {expressions...} display, set display
x[index], x[index:index], x(arguments...), Subscription, slicing, call, attribute reference
x.attribute
** Exponentiation

+, -, ~ Positive, negative, bitwise NOT

*, @, /, //, % Multiplication, matrix multiplication, division,


floor division, remainder
+, - Addition, Subraction

<<, >> Bitwise Left, Right shift

& Bitwise AND

^ Bitwise XOR

| Bitwise OR

in, not in, is, is not, <, <=, >, >=, !=, == Comparisons, including membership tests and identity
tests
not Boolean not

and Boolean and

or Boolean or

if-else Conditional Expression

lambda Lambda Expression

All operators follow, Left – Right associativity, except ** which


follows Right - Left
Mathematical Functions
Example-1:
import math
x = math.sqrt(16)

Example-2:
import math as m
x = m.sqrt(16)

Example-3:
from math import sqrt
x = sqrt(16)

Example-4:
from math import sqrt, factorial
x = sqrt(16)
y = factorial(5)
THANK YOU

You might also like