Python Module 3 AFV Operators-1
Python Module 3 AFV Operators-1
.
Operators in
Python ^ || ~ >>
Operators
wHat are
(=)
and, or operators?
A symbol used to perform mathematical and
>= logical operations in a program.
**=
Operator is a special symbol that tells to the
<< compiler to perform mathematical or logical
|| operations.
Types of Operators
Relational
Bitwise Operators Membership
Operators Operators
Example : 2+5=7
Arithmetic Operators
a = 10, b = 5 are operands in this case
Numerical
Operator Meaning Variable Example
Example
Numerical
Operator Meaning Variable Example
Example
// %
Division Operator for mixed data
type
Numerical
example
Variables with Float and Integer Data Type
= Assign the right hand side value to left hand side variable. A = 15
Add both left and right hand side values and store the result into left A += 10
+= hand side variable. ⇒ A=A+10
Subtract right hand side value from left hand side variable value and A -= B
-= store the result into left hand side variable. ⇒ A=A-B
A *= B
Multiply right hand side value with left hand side variable value and
*= store the result into left hand side variable.
⇒ A=A*B
Assignment Operators
Part-2
Numerical
Operator Meaning
Example
Divide left hand side variable value with right hand side variable value A /= B
/= and store the result into left hand side variable. ⇒ A=A/B
Divide left hand side variable value with right hand side variable value A %= B
%= and store the remainder into left hand side variable. ⇒ A=A%B
Floor Division: Divide left operand with right operand and then assign
//= the value(floor) to left operand.
A// = 5
A **= 5
**= Exponent - left operand raised to the power of right ⇒ A=A**5
Assignment Operators
Part-3
Numerical
Operator Meaning
Example
A &= B
&= The result of Bitwise AND is 1 if all the bits are 1 otherwise it is 0.
⇒ A=A&B
A |= B
|= The result of Bitwise OR is 0 if all the bits are 0 otherwise it is 1
⇒ A=A|B
A ^= B
^= The result of Bitwise XOR is 0 if all the bits are same otherwise it is 1.
⇒ A=A^B
A >>= B
The Bitwise left shift operator shifts all the bits to the left by specified number
>>= ⇒ A=A>>B
of positions
The Bitwise right shift operator shifts all the bits to the right by specified A <<= B
<<=
number of positions ⇒ A=A<<B
Lms activity
Relational or comparison
Operators
(>) (<=) Greater than (=)
Less than Not equal to
Or equal to
1 2 3 4 5 6
Numerical
Operator Meaning
Example
> Less than - True if left operand is less than the right or else False. A> B
Greater than - True if left operand is greater than the right or else
< False.
A< B
Numerical
Operator Meaning
Example
Example :
‘Hello’ in ‘Hello World’
Membership Operators
in
Example
x = [1, 2, 3, 4, 5, 6]
print(5 in x)
Output: True
Membership Operators
Not in
Example
x = [1, 2, 3, 4, 5, 6]
print(5 not in x)
Output: False
Membership Operators
examples
a = “Welcome to Python”
Example
x = [1, 2, 3, 4, 5, 6]
print(x is x)
Output: True
identity Operators
Is not
Example
x = [1, 2, 3, 4, 5, 6]
print(x is not x)
Output: False
Type function
What is type
function?
1 1 1 1 1 1
Input Output Input Output Input Output
1&1= 1 1|1= 1 1^1= 0
1&0= 0 1|0= 1 1^0= 1
0&1= 0 0|1= 1 0^1= 1
0&0= 0 0|0= 0 0^0= 0
bitwise Operators
Truth table
Not (~)
0 1
1 0
Input Output
0 = 1
1 = 0
Bitwise Operators
Variables: A = 25 (11001) and B = 20 (10100)
Add Delete
1 0 0 1 1 1 0 1 0 0 1 1 1 0
Shifts the bits of the number to the left and Shifts the bits of the number to the right and
fills 0 on voids left as a result. Similar effect fills 0 on voids left as a result. Similar effect
as of multiplying the number with some as of dividing the number with some power
power of two. of two.
Bitwise Operator
examples
Right Shift Operator >> Left Shift Operator <<
Example 1:
Example 1:
a = 5 = 0000 0101
a = 10
a << 1 = 0000 1010 = 10
a >> 1 = 5
a << 2 = 0001 0100 = 20
Example 2: Example 2:
b = -10 = 1111 0110 a = -10
b << 1 = 0000 1010 = -20
a >> 1 = -5
b << 2 = 0001 0100 = -40
Lms activity