Basic operators in Python | Types of operators in Python

Basic operators in Python | Types of operators in Python

Operator

It is the symbol used to perform some calculation or manipulation on variables , literals or expressions.

Operand

It is the variable, literal or expression on which an operator performs some operation.

Example:

9 + 8

In above expression, 9 and 8 are operands whereas + is an operator.



Types of Operators

Operators are of various types:

1. Unary Operators in Python

These are the operators which can have single operand. They are of following types:-

Unary Plus (+)

It is used to specify that the number is positive. It is an optional sign as the number without any sign is automatically considered positive.

Examples:

+10 , 3.56

Unary Minus (-)

It is used to specify that the number is negative. Minus(-) must be written before a number to specify that it is a negative number.

Examples:

-10, -3.56



2. Binary Operators in Python

These are the operators which can have two operands. Various categories of binary Operators in Python are:-

a. Arithmetic Operators

b. Relational Operators

Relational operators are also known as comparison operators.

They are used to perform comparison between values in a program.

The result of relational expression is always True or False. Various relational Operators in Python are:

Operator Purpose Example
<

(Less Than)

To check whether a value is smaller than another value. 3<10

returns True

10<3

returns False

<=

(Less than equal to)

To check whether a value is smaller than or equal another value. 3<=10

returns True

10<=3

returns False

3<=3

returns True

>

(Greater than)

To check whether a value is larger than another value. 10>3

returns True

3>10

returns False

>=

(Greater than equal to)

To check whether a value is larger than or equal to another value. 10>=3

returns True

3>10

returns False

3>=3

returns True

= =

(Equal to)

To check whether a value is equal to another value. 10==3

returns False

3==3

returns True

!=

(Not equal to)

To check whether a value is not equal to another value. 10!=3

returns True

10!=10

returns False

 

c. Logical Operators

Logical operators in Python are used to combine expressions containing comparison operators. The output of a logical expression is always True or False.

Various Logical Operators in Python are:

  • Logical AND
  • Logical OR
  • Logical NOT

(i) Logical AND (and):

Logical AND operator is represented as word and. It is used to combine two expressions containing comparison operators.

If any input of this operator is False, output would be False. If all the inputs of this operator are True, output would be True.

Truth table for Logical AND operator is

Input1 Input2 Output
False False False
False True False
True False False
True True True

Examples:

(i) 10 >3 and 10>5 = True

because 10>3 returns True , 10>5 also returns True, so True and True returns True .

(ii)10 >3 and 10<5 = False

because 10>3 returns True , 10<5 also returns False so True and False returns False .

(iii) 10 <3 and 10<5 = False

Because 10<3 returns False , 10<5 also returns False, so False and False returns True .

(ii) Logical OR (or):

Logical OR operator is represented as word or. It is used to combine two expressions containing comparison operators.

If any input of this operator is True, output would be True. If all the inputs of this operator are False, output would be False.

Truth table for Logical OR operator is

Input1 Input2 Output
False False False
False True True
True False True
True True True

Examples:

(i) 10 >3 or 10<5 = True

Because 10>3 returns True , 10<5 returns False, so True or False returns True.

(ii)10 >3 or 10>5 = True

As10>3 returns True , 10>5 also returns True, so True or True returns True .

(iii) 10 <3 or 10<5 = False

As10<3 returns False , 10<5 also returns False, so False or False returns False

(iii) Logical NOT(not)

Logical NOT operator is represented by word not in Python . It is used to reverse the output of a relational expression or logical expression.

If input of this operator is True, output would be False. If input of this operator is False, output would be True.

Truth table for Logical NOT operator is

Input Output
False True
True False

Examples:

(i) not (10 >3) = False

because 10>3 returns True , Not (True) returns False.

(ii) not (10 <3 and 10<5) = True

because 10 <3 and 10<5 returns False , not(False) returns True.

 d. Augmented/Assignment operators

 

e. Bitwise operators in Python

Bitwise operators in Python are used to manipulate individual bits of numeric values. There are six bitwise operators provided by Python.

  • Bitwise AND(&)
  • Bitwise OR (|)
  • Bitwise XOR(^)
  • Bitwise Left shift(<<)
  • Bitwise Right shift(>>)
  • Bitwise complement(~)

Note: In all examples, bitwise form of number has been obtained by adopting the method of converting a decimal number into binary number.

(i) Bitwise AND (&):

Bitwise AND operator is represented as single ampersand sign (&).

If any of the input to this operator is 0, output would be 0. If all the inputs of this operator are 1, output would be 1.

Numeric values are converted into their bitwise form and then their individual bits are applied bitwise AND operation.

Truth table for Bitwise AND operator is

Input1 Input2 Output
0 0 0
0 1 0
1 0 0
1 1 1

Example:

a=10
b=7
a=10 is represented in bitwise form as 00001010
b=7 is represented in bitwise form as 00000111
a&b will generate 00000010

After converting 00000010 into decimal form, we get 2, so value of a&b would be 2.

(ii) Bitwise OR (|)

Bitwise OR operator is represented as single pipe sign (|).

If any of the input to this operator is 1, output would be 1. If all the inputs of this operator are 0, output would be 0.

Numeric values are converted into bitwise form and then their individual bits are applied bitwise OR operation.

Truth table for Bitwise OR operator is

Input1 Input2 Output
0 0 0
0 1 1
1 0 1
1 1 1

Example:

a=10
b=7
a=10 is represented in bitwise form as 00001010
b=7 is represented in bitwise form as 00000111
a|b will generate 00001111

After converting 00001111 into decimal form, we get 15, so value of a|b is 15.

(iii) Bitwise XOR (^)

Bitwise XOR operator is represented as caret sign (^).

If odd number of inputs to this operator are 1 output would be 1.

Numeric values are converted into bitwise form and then their individual bits are applied bitwise XOR operation.

Truth table for Bitwise XOR operator is

Input1 Input2 Output
0 0 0
0 1 1
1 0 1
1 1 0

Example:

a=10
b=7
a=10 is represented in bitwise form as 00001010
b=7 is represented in bitwise form as 00000111
a^b will generate 00001101

After converting 00001101 into decimal form, we get 13, so value of a^b is 13.

(iv) Bitwise Left Shift(<<)

Bitwise Left Shift operator is represented as double less than sign(<<).

It is used to shift individual bits of a numeric value towards left by specified number of digits.

Example:

a= 10
b=a<<1
a=10 is represented in bitwise form as 00001010
a<<1 generates 00010100

Left shift operation shifts position of each bit towards left by one place.

So bitwise value of variable a 00001010 becomes 00010100.

After converting this value into decimal form, we get 20. So value of a<<1 is 20.

(v) Bitwise Right Shift(>>)

Bitwise Right Shift operator is represented as double greater than sign(>>).

It is used to shift individual bits of a numeric value towards right by specified number of digits.

Example:

a= 10
b=a>>1
a=10 is represented in bitwise form as 00001010
a>>1 generates 00000101

Right shift operation shifts position of each bit towards right by one place.

So bitwise value of variable a 00001010 becomes 00000101.

After converting this value into decimal form, we get 5. So value of a>>1 is 5.

(vi) Bitwise complement (~)

Bitwise complement operator is represented as tild sign (~). It is used to reverse the input. If input of this operator is 1, output would be 0. If input of this operator is 0, output would be 1.

Numeric value is converted into bitwise form and then individual bits are applied bitwise complement operation. After applying this operation all 0s become 1s and all 1s become 0s.

Truth table for Bitwise Complement operator

Input1 Output
0 1
1 0

Example:

a= 10
b=~a
a=10 is represented in bitwise form as 00001010
~a generates 11110101

# Program to demonstrate Bitwise Operators

a=10
b=7
c=a&b
print(c)

#Output is 2

c=a|b
print(c)

#Output is 15

c=a^b
print(c)

#Output is 13

b=a<<1
print(b)

#Output is 20

b=a>>1
print(b)

#Output is 5

b=~a
print(b)

#Output is -11



3. Identity Operators in Python

4. Membership Operators in Python

Operator Precedence Python

Precedence specifies the priority of operators in an expression.

It specifies the order in which operators will be evaluated left to right in an expression. Operators precedence table in Python :

S. No Operators
1 **
2 ~ ,+, – (Unary operators)
3 *, /, % , //
4 +, –
5 >> ,<<
6 &
7 ^, |
8 <=, <, >, >=
9 ==, !=
10 =, %=, /=, //=, -=, +=, *=, **=
11 is, is not
12 in, not in
13 Not, or, and

 



Spread the love
Lesson tags: |Types of operators in Python, Basic operators in Python
Back to: Python Programming Tutorial
Spread the love