0% found this document useful (0 votes)
4 views6 pages

24.08.2024-OPERATORS

The document explains various operators in Python, including logical (and, or, not), bitwise, assignment, identity, and membership operators, along with their syntax and examples. It also covers operator precedence and associativity, detailing how operations are prioritized during evaluation. Additionally, it illustrates the behavior of these operators with different data types such as numbers, strings, and lists.

Uploaded by

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

24.08.2024-OPERATORS

The document explains various operators in Python, including logical (and, or, not), bitwise, assignment, identity, and membership operators, along with their syntax and examples. It also covers operator precedence and associativity, detailing how operations are prioritized during evaluation. Additionally, it illustrates the behavior of these operators with different data types such as numbers, strings, and lists.

Uploaded by

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

a.

Relational expressions as operands:

X Y X and Y
False False False
False True False
True False False
True True True

>>> 5>8 and 7>3


False
>>> (4==4) and (7==7)
True

b. numbers or strings or lists as operands:


In an expression X and Y, if first operand has false value, then return first operand X as a
result, otherwise returns Y.
X Y X and Y
>>>0 and 0
0 false false X
>>>0 and 6 false true X
0 true false Y
>>>‘a’ and ‘n’ true true Y
’n’
>>>6>9 and ‘c’+9>5 # and operator will test the second operand only if the first operand
False # is true, otherwise ignores it, even if the second operand is wrong

The or operator: The or operator works in two ways:


a. Relational expressions as operands
b. numbers or strings or lists as operands

a. Relational expressions as operands:

X Y X or Y
False False False
False True True
True False True
True True True

>>> 5>8 or 7>3


True
>>> (4==4) or (7==7)
True
https://pythonschoolkvs.wordpress.com/ Page 21
b. numbers or strings or lists as operands:
In an expression X or Y, if first operand has true value, then return first operand X as a
result, otherwise returns Y.

X Y X or Y
false false Y
false true Y
true false X
true true X

>>>0 or 0
0
>>>0 or 6
6
>>>‘a’ or ‘n’
’a’
>>>6<9 or ‘c’+9>5 # or operator will test the second operand only if the first operand
True # is false, otherwise ignores it, even if the second operand is wrong

The not operator:


>>>not 6
False
>>>not 0
True
>>>not -7
False

Chained Comparison Operators:


>>> 4<5>3 is equivalent to >>> 4<5 and 5>3
True True

iv. Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation.

OPERATOR DESCRIPTION SYNTAX

& Bitwise AND x&y

| Bitwise OR x|y

https://pythonschoolkvs.wordpress.com/ Page 22
~ Bitwise NOT ~x

^ Bitwise XOR x^y

>> Bitwise right shift x>>

<< Bitwise left shift x<<

Examples:
Let Output:
a = 10 0
b=4
14
print(a & b) -11
print(a | b) 14
2
print(~a)
40
print(a ^ b)
print(a >> 2)

print(a << 2)

v. Assignment operators: Assignment operators are used to assign values to the variables.

OPERA
TOR DESCRIPTION SYNTAX

= Assign value of right side of expression to left side operand x=y+z


Add AND: Add right side operand with left side operand and a+=b
+=
then assign to left operand a=a+b
Subtract AND: Subtract right operand from left operand and then a-=b a=a-
-=
assign to left operand b
Multiply AND: Multiply right operand with left operand and then a*=b
*=
assign to left operand a=a*b
https://pythonschoolkvs.wordpress.com/ Page 23
Divide AND: Divide left operand with right operand and then a/=b
/=
assign to left operand a=a/b
Modulus AND: Takes modulus using left and right operands and a%=b
%=
assign result to left operand a=a%b
Divide(floor) AND: Divide left operand with right operand and a//=b
//=
then assign the value(floor) to left operand a=a//b
Exponent AND: Calculate exponent(raise power) value using a**=b
**=
operands and assign value to left operand a=a**b
Performs Bitwise AND on operands and assign value to left a&=b
&=
operand a=a&b
Performs Bitwise OR on operands and assign value to left a|=b
|=
operand a=a|b
Performs Bitwise xOR on operands and assign value to left a^=b
^=
operand a=a^b
Performs Bitwise right shift on operands and assign value to left a>>=b
>>=
operand a=a>>b
Performs Bitwise left shift on operands and assign value to left a <<=b a=
<<=
operand a << b
vi. Other Special operators: There are some special type of operators like-

a. Identity operators- is and is not are the identity operators both are used to check if
two values are located on the same part of the memory. Two variables that are equal
does not imply that they are identical.
is True if the operands are identical
is not True if the operands are not identical

Example:
Let
a1 = 3
b1 = 3
a2 = 'PythonProgramming'
b2 = 'PythonProgramming'
a3 = [1,2,3]
b3 = [1,2,3]

print(a1 is not b1)

https://pythonschoolkvs.wordpress.com/ Page 24
print(a2 is b2) # Output is False, since lists are mutable.
print(a3 is b3)

Output:
False
True
False
Example:
>>>str1= “Hello”
>>>str2=input(“Enter a String :”)
Enter a String : Hello
>>>str1==str2 # compares values of string
True
>>>str1 is str2 # checks if two address refer to the same memory address
False

b. Membership operators- in and not in are the membership operators; used to test
whether a value or variable is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence

Example:
Let
x = 'Digital India'
y = {3:'a',4:'b'}

print('D' in x)

print('digital' not in x)

print('Digital' not in x)

print(3 in y)

print('b' in y)

https://pythonschoolkvs.wordpress.com/ Page 25
Output:
True
True
False
True
False

3.4 Operator Precedence and Associativity:


Operator Precedence: It describes the order in which operations are performed when an
expression is evaluated. Operators with higher precedence perform the operation first.
Operator Associativity: whenever two or more operators have the same precedence, then
associativity defines the order of operations.

Operator Description Associativity Precedence


( ), { } Parentheses (grouping) Left to Right
f(args…) Function call Left to Right
x[index:index] Slicing Left to Right
x[index] Subscription Left to Right
** Exponent Right to Left
~x Bitwise not Left to Right
+x, -x Positive, negative Left to Right
*, /, % Product, division, remainder Left to Right
+, – Addition, subtraction Left to Right
<<, >> Shifts left/right Left to Right
& Bitwise AND Left to Right
^ Bitwise XOR Left to Right
| Bitwise OR Left to Right
<=, <, >, >= Comparisons Left to Right
=, %=, /=, += Assignment
is, is not Identity
in, not in Membership
not Boolean NOT Left to Right
and Boolean AND Left to Right
or Boolean OR Left to Right
lambda Lambda expression Left to Right

https://pythonschoolkvs.wordpress.com/ Page 26

You might also like