## BASIC OPERATORS IN PYTHON:-

Operators
are special symbols used in python to compute arithmetic and logical
operations. The values on which operators work are known as

**operands.**
Consider
an eg: Expression 10 +5, here 10 & 5 are operands and “+” is the operator

Types of
Operators:-

- Simple Mathematical Operators
- Comparison Operators
- Bitwise Operators
- Boolean Operators
- Identity Operators

#### Simple Mathematical Operators:-

Some common operations like addition and subtraction, ﬂoat
division, multiplication, and exponentiation are done with python on its own.
The math module oﬀers expanded functionality like trigonometric functions, root
operations, logarithms.

**1.Division :-**

Python
used to do integer division when both the no. are integers, and float division
when either one of the no. is floating type.

Eg –

a, b, c, d, e = 5, 2, 2.0, -7, 1

a / b
# = 2

a / c
# = 2.5

d / b
# = -3

b / a
# = 0

d / e
# = -7
Note that because both a and b are ints, the result is an
int.

**2.Addition :-**

Adds
values on either side of the operator.

a, b = 6, 2

# Using the "+" operator: a + b # = 8

# Using the "in-place" "+=" operator to
add and assign:

a += b # a = 8 (equivalent to a = a +
b)

import operator

# The "+=" operator is equivalent to:

a = operator.iadd(a, b)
# a = 8

Possible combinations (builtin types):

· int and int (gives an int)

· int and float (gives a float)

· int and complex (gives a complex)

· float and float (gives a float)

· float and complex (gives a complex)

· complex and complex (gives a complex)

· int and float (gives a float)

· int and complex (gives a complex)

· float and float (gives a float)

· float and complex (gives a complex)

· complex and complex (gives a complex)

**3.Exponentiation:-**

a, b = 3,4

(a ** b)
# = 81

pow(a, b) # = 81

import math math.pow(a, b) # = 81.0 (always float; does not allow
complex results)

import operator operator.pow(a, b) # = 81

*The function math.sqrt(x) calculates the square root of x.

c = 9

math.sqrt(c)
# = 3.0 (always float; does not allow complex results)

cmath.sqrt(c) # = (3+0j) (always complex)

**4.Subtraction :-**

a, b = 3, 7

# Using the "-" operator: b - a # = 4

import operator # contains 2 argument arithmetic functions

operator.sub(b, a) # = 4

**5.Multiplication:-**

Multiplies values on either side of the operator

a, b = 5, 3

a * b = 15

import operator operator.mul(a, b) # = 15

Possible combinations (builtin types):

1.int and int (gives an int)

2.int and float (gives a float)

3. int and complex (gives a complex)

4.float and float (gives a float)

5.float and complex (gives a complex)

6.complex and complex (gives a complex)

1.int and int (gives an int)

2.int and float (gives a float)

3. int and complex (gives a complex)

4.float and float (gives a float)

5.float and complex (gives a complex)

6.complex and complex (gives a complex)

·

**6.Modulus:-**
Divides left-hand operand by right-hand operand and returns
remainder.

·

3 % 4
# 3

·
10 % 2
# 0

· 6 % 4
# 2

by using the operator module:

import operator

·
operator.mod(3 , 4) # 3

·
operator.mod(10 , 2) # 0

·
operator.mod(6 , 4) # 2

**Comparison Operators:-**

Comparison Operators are used in the place where there is a
need for comparing two numbers.

**Bitwise Operators:-**

Bitwise operator works on bit-level strings, and it is
operated bit by bit. This section provides useful knowledge and examples of
Python's bitwise operators.

**1. Bitwise NOT:-**

The ~ operator is used to flip the bits of the number. Since
computers use signed numbers notation, the two's complement notation to encode
negative binary numbers where negative numbers are written with a leading one
(1) instead of a leading zero (0).

In this way, negative numbers range down to -128 (1000
0000). Zero (0) is represented as 0000 0000, and minus one (-1) as 1111 1111.

In general, though, this means ~n = -n - 1.

·
0 = 0b0000 0000 ~0

·
Out: -1 # -1 = 0b1111 1111

·
1 = 0b0000 0001 ~1

·
Out: -2 # -2 = 1111 1110

**Bitwise XOR (Exclusive OR):-**

The ^ operator performs the XOR on given bits. It copies
the bit if it is set in one operand but not both.

Truth Table :-

·
0 ^ 0 = 0

·
0 ^ 1 = 1

·
1 ^ 0 = 1

·
1 ^ 1 = 0

60 = 0011 1100

30 = 0001 1110

60 ^ 30 Out: 34

34 = 0010 0010

**Bitwise AND:-**

Performs AND operation on the provided bits. Operator
copies a bit to the result if it exists in both operands.

Truth Table :-

·
0 & 0 = 0

·
0 & 1 = 0

·
1 & 0 = 0

·
1 & 1 = 1

Eg -

60 = 0011 1100

30 = 0001 1110

60 & 30 Out: 28

28 = 0001 1100

**Bitwise OR :-**

It will perform Or operation on each and every bit. It copies a bit if it exists in either operand.

Truth Table :-

·
0 | 0 = 0

·
0 | 1 = 1

·
1 | 0 = 1

·
1 | 1 = 1

Eg-

60 = 0011 1100

30 = 0001 1110

60 | 30 Out: 62

62 = 0011 1110

B

**itwise Left-Shift -**
The << operator will perform a bitwise "left
shift," where the left operand's value is moved left by the number of bits
given by the right operand.

Eg- a = 0011 1100

a << 2 = 240 (means will become
1111 0000)

**Bitwise Right-Shift -**

The << operator will perform a bitwise
"left shift," where the left operand's value is moved left by the number of bits given by the right operand.

Eg – a= 0011 1100

a >> 2 = 15 (means 0000 1111)

**Boolean Operators:-**

When you use or, it will either return the ﬁrst
value in the expression if it's true, else it will blindly return the second
value. When you use or, it will either return the ﬁrst
value in the expression if it's true, else it will blindly return the second
value.

**AND:-**

“AND “operator evaluates the second argument if and only if both of the statements
are truthy. If both the operands are true, then the condition becomes true.

- x = True
- y = True
- z = x and y
- z = True

- x = True
- y = False
- z = x and y
- z = False

- x = False
- y = True
- z = x and y
- z = False

- x = False
- y = False
- z = x and y
- z = False

**OR:-**

“OR”
operator evaluates the ﬁrst truthy argument if either one of the arguments is
truthy. If both arguments are false, evaluates to the second argument.

x = True y = True |z = x or y

z = True

x = True y = False |z = x or y

z = True

x = False y = True |z = x or y

z = True

x = False y = False |z = x or y

z = False

z = True

x = True y = False |z = x or y

z = True

x = False y = True |z = x or y

z = True

x = False y = False |z = x or y

z = False

**NOT:-**

It returns the opposite of the following
statement:

- x = True | y = not x
- y = False
- x = False | y = not x
- y = True

#### Identity Operators:-

Identity operators are used to comparing the memory
locations of two objects. Generally, two identity operators are used- 1. Is 2. Is not

Is –

evaluates true when either side of element points
to the same object.

In Not-

Evaluates false when either side of the element
points to the same object.

**Operator Precedence:-**

Python operators have a set order of precedence,
which determines what operators are evaluated ﬁrst in a potentially ambiguous
expression. For instance, in the expression 4 * 5 + 8, ﬁrst 4 is multiplied by
5, and then the result is added to 8, yielding 28.

Well explained, guys it's beneficial for beginners.

ReplyDeleteVery helpful bro, thanks

ReplyDeleteVery good explanation...

ReplyDeletewell explained.

ReplyDeleteThanks... Very easy to understand for beginners 👍

ReplyDeleteVery well explained and its very easy to learn.

ReplyDeleteThis comment has been removed by the author.

ReplyDelete## Post a Comment