Operators in Python

Previous
Next

Operator:

  • Operator is a symbol, to operate operands.
  • Operator performs operations on one or more operands.
  • Operands like variables, constants and functions

Python divides the operators in the following way:

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators
  • Ternary Operator

Arithmetic Operators:

  • Used to perform all arithmetic operations
  • Operators are +, – , * , / , % , // , **

Note: In other programming languages, arithmetic operations perform depends on data types.

Python doesn’t support data types concept.

  • Operator(/) returns quotient after division
  • Operator(//) is called floor division – returns floor value of quotient after division.
  • Operator(%) is called mod operator – returns remainder after division.
  • Operator(**) is called exponent operator – returns power value.
    • 2**3 -> 2^3 -> 8
    • 3**2 -> 3^2 -> 9
a=10
b=5
print(a/b)
print(a//b)
print(a%b)
a=7
b=2
print(a/b)
print(a//b)
print(a%b)

Python arithmetic operators follow BODMAS rule:

print("BODMAS rule")
print("5+3-2 :",5+3-2)
print("5+3*2 :",5+3*2)
print("5/3*2 :",5/3*2)
print("(5+3)*2 :",(5+3)*2)

We can assign multiple values to variables as follows:

a,b,c=3,8,2
d=a+b-c
e=a*b/c
f=a**c+b
print(d,e,f)

Analyze the priority of operators using following code:

a,b,c=3,8,2
d=b%c+a
e=b//c*a
print(d,e)

Comparison (Relational) Operators

  • Relational Operators: Relational operators are comparing values.
  • It returns True or False depending to the condition.
OperatorMeaning
Greater than
Less than
>=Greater than or equals to
<=Less than or equals to
==Equal to
!=Not equal to
print("Relational operators : ")
print("5>3 :",5>3)
print("5<3 :",5<3)
print("5>=3 :",5>=3)
print("5<=3 :",5<=3)
print("5==3 :",5==3)
print("5!=3 :",5!=3)

Comparing strings:

a="abc"
b="xyz"
print(a>b)

Analyze the code:

print("a>A :", 'a'>'A')
print("1>0 :", '1'>'0')
print("abc>a :", 'abc'>'a')

Assignment Operators

OperatorDescriptionExample
=Assigns value to left side variablec = a + b
+=ADD AND operator.c += a equals to c = c + a
-=Subtract AND operatorc -= a equals to c = c – a
*=Multiply AND operatorc *= a equals to c = c * a
/=Divide AND operatorc /= a equals to c = c / a
%=Modulus AND operatorc %= a equals to c = c % a
**=Exponent AND operatorc **= a equals to c = c ** a
//=FLOOR DIVISION AND operatorc //= a equals to c = c // a
print("Python assignment operators :")
a,b = 5,3
print("a value :",a)
print("b value :",b)
a+=b
print("a+=b : ", a)
a,b = 5,3
a-=b
print("a-=b : ", a)
a,b = 5,3
a*=b
print("a*=b : ", a)

Logical Operators:

  • Evaluate more than one expression and returns a boolean value.
  • Operator are and, or and not.
OperatorMeaningExample
AndIf condition is Ture both the operands are truex and y
OrIf the condition  is Ture  one of the operands is truex or y
notIf condition is Ture operand is false (complements the operand)not x
x = True
y = False
print('x and y is : ',x and y)
print('x or y is : ',x or y)
print('not x is : ',not x)

Output it:

print(True and True)
print(6>4 and 5>3)
print( True and False)
print(False and True)
print(False and False)
print(6>3 and 4!=4)

Bitwise Operators

  • Bitwise operators: To perform the bitwise operators are bit by bit operation.
  • In python bitwise shift operators are using left shift operands and right shift operands
  • Left side for the given number of times in the right operand.
  • Moving binary bits in the memory change the value of variable.
  • These operators return the result in decimal format only.
  • Operators are Right shift (>>) and Left shift (<<)
x=8
print(x>>2)
print(x<<2)

Right shift: n/2^s -> 8/2^2 -> 8/4 -> 2

Left shift : n*2^s -> 8*2^2 -> 8*4 -> 32

OPERATORDESCRIPTIONSYNTAX
&Bitwise ANDX&Y
|Bitwise ORX|Y
~Bitwise NOT~X
^Bitwise XORX^Y
>> Bitwise right shiftX>>
<< Bitwise left shiftX<<
a=20
b=5
print(a&b)
print(a|b)
print(~a)
print(a^b)
print(a>>4)
print(a<<4)

Membership Operators

  • in and not in are the membership operators
  • To test whether a value or variable is in a sequence used
    • in: True if value is found in the sequence
    • not in: True if value is not found in the sequence

Example of Membership Operator

x = 'Tutipy tutorials'
y = {3:'a',4:'b'}
print('a' in x) 
print('tutip' not in x) 
print('tutorials' not in x) 
print(3 in y)
print('b' in y)
s = 'Python is Programming language'
print('y' in s)
print('py' in s)
print('th' in s)

Identity Operators:

  • Is and is not are the identity operators both are used to check if two values are located on the same part of the memory.
  • Two variables that are equal do not imply that they are identical.
    • is  : True if the operands are identical
    • is not  :  True if the operands are not identical
a1 = 3
b1 = 3
a2 = 'conix Technologies'
b2 = 'Tutipy tutorials'
a3 = [1,2,3] 
b3 = [1,2,3]
print(a1 is not b1) 
print(a2 is b2)

Conditional expression(Ternary):

  • The conditional expressions are called Ternary operators and it evaluates based on the condition true or false.
  • It is test condition in a single line and replacing the multiline if else condition.

Syntax:

[on_true] if [expression] else [on_false]

Example :

a, b = 20, 30
min = a if a < b else b 
print(min)

Precedence of operators:

  • The combination of values, variables, operators, and functions are called expression. The Python interpreter can execute a valid expression.
OperatorsMeaning
()Parentheses
**Exponent
+x , -x, ~xUnary plus, Unary minus, Bitwise NOT
*, /, //, %Multiplication, Division, Floor division, Modulus
+, –Addition, Subtraction
<<, >>Bitwise shift operators
&Bitwise AND
^Bitwise XOR
|Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not inComparisons, Identity, Membership operators
notLogical NOT
andLogical AND
orLogical OR
Previous
Next

Add Comment

Courses Enquiry Form