Basic Operators in Python


Operators  in Python
     ·       Arithmetic operators
     ·       Relational operators
     ·       Logical operators
     ·       Bitwise operators
     ·       Assignment  operators
     ·       Special operators
Arithmetic operators:

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.

Example:
x = 10
y = 4
# Addition of numbers
add = x + y
# Subtraction of numbers
sub = x - y
# Multiplication of number
mul = x * y
# Division(float) of number
div1 = x / y
# Division(floor) of number
div2 = x // y
# Modulo of both number
mod = x % y

# print results
print(add)

print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
output:
14
6
40
2.5
2
2

Relational Operators

It compares the values. It either returns True or False according to the given condition.

Example:
a = 55
b = 33
# a > b is True
print(a > b)
# a < b is false
print(a < b)
# a == b is False
print(a == b) 
# a != b is True
print(a != b)
# a >= b is True
print(a >= b)
# a <= b is false
print(a <= b)

Output:
True
False
False
True
True
False

Logical operators:

Logical operators perform Logical ANDLogical OR and Logical NOT operations.

Example:
a = True #Operands
b = False #Operands
# Print a and b is False
#Logical And True if both operands (a and b) are true
print(a and b)
# Print a or b is True
# Logical OR True if either the operands are true
print(a or b)
# Print not a is False
#Logical Not True if operand is false
print(not a)

Output:
False
True
False

Bitwise Operator:

 Bitwise operators acts on bits and performs bit by bit operation.

Example:
a = 100
b = 4
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)
# Print bitwise NOT operation
print(~a)
# print bitwise XOR operation
print(a ^ b)
# print bitwise right shift operation
print(a >> 2)
# print bitwise left shift operation
print(a << 2)

Output:
4
100
-101
96
25
400


 Assignment operators:

 Assignment operators are used to assign values to the variables.

Example:
a = 21
b = 10
c = 0

c = a + b
print ("Line 1 - Value of c is ", c)
c += a
print ("Line 2 - Value of c is ", c)
c *= a
print ("Line 3 - Value of c is ", c)
c /= a
print ("Line 4 - Value of c is ", c)
c  = 2
c %= a
print ("Line 5 - Value of c is ", c)
c **= a
print ("Line 6 - Value of c is ", c)
c //= a
print ("Line 7 - Value of c is ", c)

output:
Line 1 - Value of c is  31
Line 2 - Value of c is  52
Line 3 - Value of c is  1092
Line 4 - Value of c is  52.0
Line 5 - Value of c is  2
Line 6 - Value of c is  2097152
Line 7 - Value of c is  99864

Special Operators:

There are some special type of operators like

    ·       Membership Operators:

    in and not in are the membership operators; used to test whether a value or variable is in a sequence.


in            True if value is found in the sequence
not in        True if value is not found in the sequence
 

Example:
# Examples of Membership operator
x = 'earth is round'
y = {3:'a',4:'b'}
print('G' in x)
print('earth' not in x)
print('Earth' not in x)
print(3 in y)
print('b' in y)

Output:
False
False
True
True
False





Comments

Popular posts from this blog

Python Conditions