5.Operators

Operators are special symbols which represents computation. They are applied on operand(s), which can be values(constants) or variables. Same operator can behave differently on different data types. Operators when applied on operands form an expression Operators are categorised as

Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bit wise Operators
Membership Operators
Identity Operators

Arithmetic Operators( Mathematical)

Symbol

Description

Example 1

Example 2

+

Addition

55+45
100

 “Good” + “Morning”
GoodMorning


-

 

Subtraction

55-45
10

30-80
-50

 

*

 

Multiplication

55*45

 “Good” * 3
GoodGoodGood

 

/

 

Division

17/5
3
17/5.0
3.4

17.0/5
3.4
28/3
9

 

%

 Remainder/

Modulo

17%5
2

 23%2
1

 

**

 

Exponentiation

2**3
8
16**0.5
4.0

2**8
256

 

//

 Integer

Division

7.0//2
3.0

3/ / 2
1                                      


Comparison (Relational) Operators
These operators compare the values on either side of them and decide the relation among them.
They are also called Relational operators.
Symbol Description Example 1 Example 2
< Less than 7<10
True
Goodbye' < 'Hello'
True
> Greater than7>5
True
'Goodbye' > 'Hello'
False

<=

less than equal to  2<=5
True
"Hello" <= “Goodbye”
False

>=

greater than equal
to
10>=10
True
"Hello" >= “Goodbye”
True

! =

not equal to 10!=11
True
‟Hello”!= “HELLO"
True

==

equal to 10==10
True
"Hello" == “Hello”
True
Note: two values that are of different data type will never be equal to each other.

Assignment Operators
Assignment Operator combines the effect of arithmetic and assignment operator
Symbol DescriptionExample Explanation

 

=

Assigned values from right side
operands to left variable

x=12
y="greetings"

x=12

( we will assume x=12 for all examples)

 

+= 

 

added and assign back the result
to left operand

x+=2

x=x+2
x=14

 

-=

subtracted and assign back the
result to left operand

x-=2

x will become 10

 

*=

multiplied and assign back the
result to left operand

x*=2

x will become 24

 

/=

divided and assign back the

result to left operand

x/=2

x will become 6

 

 

%=

taken modulus using two
operands and assign the result
to left operand

 

x%=2

 

x will become 0

 

 

**=

performed exponential (power)
calculation on operators and
assign value to the left operand

x**=2

x will become 144

 

//=

performed floor division on
operators and assign value to
the left operand

x / /= 2

x will become


Logical Operators 

The following logical operators are supported by Python language. Assume variable A holds True and variable B holds False.
Symbol Description Example
or If any one of the operand is true, then the condition becomes true. A or B is True
and If both the operands are true, then the condition becomes true. A and B is False
not Reverses the state of operand/condition. not A is False

Note:The Python Virtual Machine sometimes knows the value of Boolean expression before it has evaluated all of its operands. For instance, in the expression A and B, if A is false, then so is the expression and there is no need to evaluate B.Likewise in the expression A or B, if A is true, then so is the expression, and again there is no need to evaluate B. This approach, in which evaluation stops as soon as possible is called short-circuit evaluation.

Python Bitwise Operators

Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows −

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

Python's built-in function bin() can be used to obtain binary representation of an integer number.

The following Bitwise operators are supported by Python language 

Operator

Description

Example

& Binary AND

Operator copies a bit, to the result, if it exists in both operands

(a & b) (means 0000 1100)

| Binary OR

It copies a bit, if it exists in either operand.

(a | b) = 61 (means 0011 1101)

^ Binary XOR

It copies the bit, if it is set in one operand but not both.

(a ^ b) = 49 (means 0011 0001)

~ Binary Ones Complement

It is unary and has the effect of 'flipping' bits.

(~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number.

<< Binary Left Shift

The left operand's value is moved left by the number of bits specified by the right operand.

a << 2 = 240 (means 1111 0000)

>> Binary Right Shift

The left operand's value is moved right by the number of bits specified by the right operand.

a >> 2 = 15 (means 0000 1111)


Membership Operators

Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below

Let  S=”Python”  c=’o’

Operator

Description

Example

in

Evaluates to true if it finds a variable in the specified sequence and false otherwise.

c  in S will result True

not in

Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

c not in S will result False


Identity Operators

Identity operators compare the memory locations of two objects. There are two Identity operators as explained below

Let x=10 and y=x

id(x) and id(y) are same in this case

Operator

Description

Example

is

Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

x is y will return True

is not

Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

x is not y will return False

 

Note: Important Operators



Summary

Python provides a wide array of operators that allow for various operations on data, ranging from simple arithmetic and comparison to bitwise operations and identity checks. Understanding these operators is essential for writing efficient and effective Python code.

Comments

Popular posts from this blog

Python For Machine Learning - CST 283 - KTU Minor Notes- Dr Binu V P

46.Classes and Objects in Python- Accessors and mutators

KTU Python for machine learning Sample Question Paper and Answer Key Dec 2020