0% found this document useful (0 votes)
45 views

Python Module 3 AFV Operators-1

Operators are special symbols that tell the compiler to perform mathematical, logical, or relational operations. There are different types of operators including arithmetic operators (+, -, *, /, %, //, **), assignment operators (=, +=, -=, etc.), relational operators (> ,<, ==, etc.), logical operators (and, or, not), bitwise operators (&, |, ^, <<, >>), and membership operators (in, not in). Operators allow programmers to manipulate variables and values to perform useful calculations and logic in their code.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Python Module 3 AFV Operators-1

Operators are special symbols that tell the compiler to perform mathematical, logical, or relational operations. There are different types of operators including arithmetic operators (+, -, *, /, %, //, **), assignment operators (=, +=, -=, etc.), relational operators (> ,<, ==, etc.), logical operators (and, or, not), bitwise operators (&, |, ^, <<, >>), and membership operators (in, not in). Operators allow programmers to manipulate variables and values to perform useful calculations and logic in their code.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

03

.
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

Arithmetic Assignment Logical Identity


Operators Operators Operators Operators
Arithmetic Operators
wHat are arithmetic
Operators?

An arithmetic operator is a mathematical


function that takes two operands and performs a
calculation on them.

Example : 2+5=7
Arithmetic Operators
a = 10, b = 5 are operands in this case

Numerical
Operator Meaning Variable Example
Example

+ Add two operands or unary plus c = a+b 10 + 5 = 15

Subtract right operand from the left or 10 - 5 = 5


- unary minus
c = a-b

* Multiply two operands c = a*b 10 * 5= 50

Divide left operand by the right one c = a/b 10 / 5 = 2


/ (always results into float)
Arithmetic Operators
a = 10, b = 5 are operands in this case

Numerical
Operator Meaning Variable Example
Example

Modulus - remainder of the division of 10 % 5 = 0


% left operand by the right
c = a%b

Floor division - division that results into


10 // 5 = 2
// whole number adjusted to the left in the x // y
number line

Exponent - left operand raised to the 10 **5 = 100000


** power of right
x**y
Arithmetic Operators
Difference between / and %
operators
● The division operator (/) computes the
Quotient quotient (both for float and integer
variables).
● The modulus operator (%) computes the
remainder when one integer is divided by
another.
Remainder ● In Python, modulus operator supports both
integer and float type variables.
Arithmetic Operators
Difference Between Modulus (%) & Floor Division (//)

Floor Division: Modulus Operation:


13 // 4 = 3 13 % 4 = 1

4 occurs three times in If you divide 13 by 4


13. you will get 1 as
remainder.

// %
Division Operator for mixed data
type
Numerical
example
Variables with Float and Integer Data Type

a=5.0 a/b = 2.5


b=2.0 a/d = 2.5
c=5 c/b = 2.5
d=2 c/d = 2.5

Python works well with mixed data types!!!


Lms activity
Assignment Operators
What are
assignment
operators?
● The assignment operators are used to
assign the right hand side value
(Rvalue) to the left hand side variable
(Lvalue).

● The assignment operator is used in


different variations along with
arithmetic operators.
Assignment Operators
Part-1
Numerical
Operator Meaning
Example

= 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

Greater Less than Equal to


than (<) Or equal to (>=) (!=)
Relational or comparison
Operators What are
relational
operators?
● Relational operators are used to compare two
values.
● They are used to check the relationship
between two values.
● Every relational operator has two results
TRUE or FALSE.
Example : Your marks are greater Sam’s
9>6
Relational or comparison
Operators
Part-1

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

Less than or equal to - True if left operand is less than or equal to


<= the right or else False.
A <= B
Relational or comparison
Operators
Part-2

Numerical
Operator Meaning
Example

Greater than or equal to - True if left operand is greater than or A >= B


>= equal to the right or else False.

== Equal to - True if both operands are equal or else False. A == B

!= Not equal to - True if operands are not equal or else False. A != B


Lms activity
Logical Operators
What are logical
operators?

Logical operators are used on conditional


statements (either True or False).

The logical operators are the symbols


that are used to combine multiple
conditions or expressions into single
condition.
Logical Operators
logical And

Logical AND - Returns TRUE if all


conditions are TRUE otherwise
returns FALSE.
Example

(10 < 5 and 12 > 10)


False and True
is FALSE
Logical Operators
logical or

Logical OR - Returns FALSE if all


conditions are FALSE otherwise
returns TRUE.
Example

(10 < 5 or 12 > 10)


False or True
is TRUE
Logical Operators
logical not

Logical NOT - Returns TRUE if


condition is FALSE and returns FALSE
if it is TRUE
Example

not(10 < 5 and 12 > 10)


is TRUE
Lms activity
membership Operators
What are
membership
operators?
The membership operators in Python are
used to test whether a value is found
within a sequence.

Example :
‘Hello’ in ‘Hello World’
Membership Operators
in

True if value/variable is found in the


sequence.

Example

x = [1, 2, 3, 4, 5, 6]
print(5 in x)

Output: True
Membership Operators
Not in

True if value/variable is not found in the


sequence.

Example

x = [1, 2, 3, 4, 5, 6]
print(5 not in x)

Output: False
Membership Operators
examples

a = “Welcome to Python”

print("Greetings" not in a) print("Greetings" in a)


Output: True Output: True
Lms activity
identity Operators
What are identity
operators?

Identity operators are used to


determine whether a value is of a
certain class or type. They are usually
used to determine the type of data a
certain variable contains.

There are two types of identity operators.


is - will return output as True if the operands are identical or refer to the same object.
Such as x is true.
is not - will return output as True if the operands are not identical or do not refer to the same
object.
identity Operators
is

True if the operands are identical (refer


to the same object).

Example

x = [1, 2, 3, 4, 5, 6]
print(x is x)

Output: True
identity Operators
Is not

True if the operands are not identical


(do not refer to the same object)

Example

x = [1, 2, 3, 4, 5, 6]
print(x is not x)

Output: False
Type function
What is type
function?

The type() function returns the type of


the specified object.

Python is truly object oriented


language, where every variable is an
object, and every piece of code is a
method.
Type function
example
#Assign the values to s
a, b, c, d objects
a = 10
b = "Hello World"
c = 33.33 The Output:
d= 2+3j <class 'int'>
<class 'str'>
<class 'float'>
#use type() function to understand the type of #the <class 'complex'>
data stored in the variable or referred by #the object
print(type(a))
print(type(b))
print(type(c))
print(type(d))
Identity Operators
examples

# Python program to illustrate


# Python program to illustrate the use
the
# of 'is' identity operator
# use of 'is not' identity
operator x=5
x = 51.2 if (type(x) is int):
if (type(x) is not int): Output - True print("true")
print("true")
else:
else:
print("false") print("false")
Lms activity
Bitwise Operators
wHat are bitwise
operators?

The bitwise operators are used to perform


bit level operations.
When we use bitwise operators, the
operations are performed based on the
binary values.
msb & lsb
wHat are MSB &
LSB?
LSB: Least Significant Bit
MSB: Most Significant Bit
The Most Significant Bit (MSB) is the bit in a
multiple-bit binary number with the largest value.
This is usually the bit farthest to the left, or the bit
transmitted first in a sequence.

The Least Significant Bit (LSB) is the bit in a binary


number which is of the lowest numerical value.

In this example, MSB is 1 and LSB is 0.


number conversion: binary to
HowDecimal
to convert binary to
Decimal?
The base of the binary number system is 2, and
it is increased by the factor of two. The first
digit has 20 weights, the second has 21 weights,
the weight of the third digit is 22 and so on.

In binary to decimal conversion when there is


one in a digit position of a binary number, then
the weight of the position is added. But when
there is the zero in a binary position the weight
of the position is disregarded.
number conversion: Decimal to
binary
How to convert
Decimal to binary?

An easy method of converting decimal


to binary number equivalents is to write
down the decimal number and to
continually divide-by-2 (two) to give a
result and a remainder of either a “1”
or a “0” until the final result equals
zero.
Bitwise Operators
Variables: A = 25 (11001) and B = 20 (10100)

Operator Meaning Numerical Example

The result of Bitwise AND is 1 if all the bits are 1 A&B


& otherwise it is 0. ⇒ 16 (10000)

The result of Bitwise OR is 0 if all the bits are 0 A| B


| (Pipe) otherwise it is 1. ⇒ 29 (11101)

The result of Bitwise XOR is 0 if all the bits are same A^ B


^ (Cap) otherwise it is 1. ⇒ 13 (01101)

The result of Bitwise Not 1’s complement is negation ~A


~ (Tilde) of the bit (Flipping). ⇒ 6 (00110)
bitwise Operators
Truth
Table
AND (&) OR (|) XOR (^)
1 0 1 1 0 1
1 1 1 0 1 0

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)

Operator Meaning Numerical Example

The Bitwise left shift operator shifts all the bits to the A<<2


<< left by specified number of positions. ⇒ 100 (1100100)

The Bitwise right shift operator shifts all the bits to A>>2


>> the right by specified number of positions. ⇒ 6 (00110)
bitwise Operators
Left Shift Right Shift

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

You might also like