Monday 29 April 2019

6. Operators

6.   Operators    -    15 mins read

  • What is an operator?
  • Different type of operators
  • Arithmetic Operators
  • Assignment operator
  • Unary minus operator
  • Relational operators
  • Logical operators
  • Membership operators
  • Identity operators
  • Programs    

6. Operators

What is an operator?

  • An operator is a symbol that performs an operation.
  • An operator acts on some variables, those variables are called operands.
  • If an operator acts on a single operand, then it is called a unary operator
  • If an operator acts on 2 operands, then it is called a binary operator.
  • If an operator acts on 3 operands, then it is called a ternary operator.
Program      operator and operands
Name          demo1.py

                  a = 10
b = 20
                  c = a + b
                  print(c)

output
                  30
  • Here a, b and c are called as operands,
  • + is called as operator.
Different type of operators

  • Arithmetic Operators:      (+, -, *, /, %, **, //)
  • Assignment Operator       (=)
  • Unary minus Operator     (-)
  • Relational Operator         (>, <, >=, <=, ==, ! =)
  • Logical Operators            (and, or, not)
  • Membership operators     (in, not in)
  • Identity operators           (is, is not)
Make a note:

  • Python does not have increment and decrement operators.
1. Arithmetic Operators: (+, -, *, /, %, **, //)

  • These operators do their usual job, so please don’t expect any surprises.
Assume that,
         a = 13
         b = 5


Operator


Meaning

Example

Result


+


Addition

a+b

18

-


Subtraction

a-b

8

*


Multiplication

a*b

65

/


Division

a/b

2.6

%


Modulus (Remainder of division)


a%b

3

**

Exponent operator (exponential power value)


a**b

371293

//

Integer division (gives only integer quotient)


a//b

2

Make a note

  • Division operator / always performs floating point arithmetic, so it returns float values.
  • Floor division (//) can perform both floating point and integral as well,
    • If values are int type, then the result is int type.
    • If at least one value is float type, the result is float type.
Program      Arithmetic Operators
Name          demo2.py

a = 13
b = 5
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a%b)
print(a**b)
print(a//b)

Output
                  18
                  8
                  65
                  2.6
                  3
                  371293
                  2

2. Assignment operator: (=, +=, -=, *=, /=, %=, **=, //=)
ü  By using these operators, we can assign values to variables.
Assume that,
         a = 13
         b = 5


Operator


Example

Equals to

Result


=


x=a+b

x = a + b

18

+=


a+=5

a = a+5

18

-=


a-=5

a = a-5

8

Program       Assignment operator
Name          demo3.py
        
                  a=13
                  print(a)
                  a+=5
                  print(a)

Output
                  13
                  18  

3. Unary minus operator: (-)

  • Symbol of unary minus operator is
  • When this operator before a single variable then it brings the corresponding results
Program       Unary minus operator
Name          demo4.py

                  a=10
                  print(a)
                  print(-a)

Output
                  10
                 -10

4.Relational operators (>, >=, <, <=, ==, !=)

  • These operators are used to compare two values.
  • These operators bring boolean result as True and False while comparing the values.
  • By using these operators, we can construct simple conditions.
Assume that,
         a = 13
         b = 5


Operator


Example

Result

> 


a>b

True

>=


a>=b

True

< 


a<b

False

<=


a<=b

False

==


a==b

False

!=


a!=b

True

Program       Relational operators
Name          demo5.py

a = 13
b = 5
print(a>b)
print(a>=b)
print(a<b)
print(a<=b)
print(a==b)
print(a!=b)          
Output
True
True
False
False
False
True

5. Logical operators (and, or, not)

  • In python there are three logical operators those are,
    • and
    •  or
    •  not
  • Logical operators are useful to construct compound conditions.
  • Compound condition is a combination of more than one simple conditions.
  • Each simple condition brings the boolean result finally the total compound condition evaluates either True or False.
Make a note

  • In case of logical operators,
    •  False indicates 0
    • True indicates any other number.
For boolean types behaviour
ü  and
o   If both arguments are True, then only result is True
ü  or
o   If at least one argument is True, then result is True
ü  not
o   complement

Program       Logical operators on boolean data types
Name          demo6.py

a = True
b = False
print(a and a)
print(a or b)
print(not a)
        
Output
True
True
False

Logical operators (and, or, not) on non-boolean data types behavior
Suggestion: Please understand these below statements carefully,
and operator
ü  A and B returns A, if A is False.
ü  A and B returns B, if A is not False.

Info
ü  0 means False
ü  Any other number means True


Example 1
ü  0 and 4 returns 0
ü  5 and 7 returns 7

Example 2
ü  21 and 0 returns 0
ü  15 and 8 returns 8

Program       Logical operators on non-boolean types
Name          demo7.py

                  print(0 and 4)
                  print(5 and 7)

Output
                  0
                  7

Program       Logical operators on non-boolean types
Name          demo8.py

                  print(21 and 0)
                  print(15 and 8)

Output       
                  0
                  8

Program       Logical operators on non-boolean types
Name          demo9.py

                  x = 0
                  y = 4
                 
                  a = 4
                  b = 7
                 
print(x and y)
                  print(a and b)

Output           
                  0
                  7

Program       Logical operators on non-boolean types
Name          demo10.py


                  x=21
                  y=0
                 
                  a=15
                  b=8
                 
                  print(x and y)
                  print(a and b)

Output    
                  0
                  8

Conclusion


Operator


Example

Meaning

and

x and y

ü  If x is False, it returns x, otherwise it returns y


or operator
ü  A or B returns A, if A is True.
ü  A or B returns B, if A is not True.

Info
ü  0 means False
ü  Any other number means True

Example 1

ü  0 or 4 returns 0
ü  5 or 7 returns 7

Example 2
ü  21 or 0 returns 21
ü  15 or 8 returns 8

Program       Logical operators on non-boolean types
Name          demo11.py

                  print(0 or 4)
                  print(5 or 7)

Output
                  4
                  5

Program       Logical operators on non-boolean types
Name          demo12.py

                  print(21 or 0)
                  print(15 or 8)

Output                 
                  21
                  15

Program       Logical operators on non-boolean types
Name          demo13.py

                  x = 0
                  y = 4
                 
                  a = 5
                  b = 7
                 
print(x or y)
                  print(a or b)

Output                 
                  4
                  5

Program       Logical operators on non-boolean types
Name          demo14.py

                  x=21
                  y=0
                 
                  a=15
                  b=8
                 
                  print(x or y)
                  print(a or b)

Output                 
                  21
                  15

Conclusion


Operator


Example

Meaning

or


x or y

ü  If x is False, it returns y, otherwise it returns x



not operator
ü  not A returns False, if A is True
ü  not A returns True, if A is False

Info
ü  0 means False
ü  Any other number means True

Example 1
ü  not 5 returns False
ü  not 0 returns True

Program       not operator on non-boolean types
Name          demo15.py

                  print(not 5)
                  print(not 0)

output
                  False
                  True

Program       not operator on non-boolean types
Name          demo16.py

                  x = 5
                  y = 0

                  print(not x)
                  print(not y)

output
                  False
                  True

Conclusion


Operator


Example

Meaning

not


not x

ü  If x is False, it returns True, otherwise False


Logical operators (and, or, not)


Operator


Example

Meaning

and

x and y

ü  If x is False, it returns x, otherwise it returns y


or


x or y

ü  If x is False, it returns y, otherwise it returns x


not


not x

ü  If x is False, it returns True, otherwise False



 7. Membership operators 
ü  Membership operators are useful to check whether the given object is available in collection (sequence) or not. (It may be string, list, set, tuple and dict)
ü  There are two membership operators,
o   in
o   not in

The in operator
ü  in operator returns True, if element is found in the collection or sequences.
ü  in operator returns False, if element is not found in the collection or sequences.

The not in operator
ü  This work in reverse manner for ‘in’ operator.
ü  not in operator returns True, if an element is not found in the sequence.
ü  not in operator returns False, if an element is found in the sequence.

Program       example by using in and not in operator       
Name          demo17.py

                  text = “Welcome to python programming”

                  print(“Welcome” in text)
                  print(“welcome” in text)
                  print(“nireekshan” in text)
                  print(“Hari”  not in text)

output
                  True
                  False
                  False
                  True

Program       example by using in and not in operator       
Name          demo18.py

                  names = [“Ramesh”, “Nireekshan”, “Arjun”, “Prasad”]
                  print(“Nireekshan” in names)
                  print(“Hari” in names)
                  print(“Hema” not in names)

output
                  True
                  False
                  True

8. Identity operators       
ü  This operator compares the memory locations (address) of two objects.
ü  Hence, it is possible to know whether the two objects are pointing to same object or not.
ü  The memory locations of an object can be seen by using id() function.

Program       Checking two variables address by using id() function
Name          demo19.py

a=25
b=25

print(a)
print(b)

print(id(a))
print(id(b))

Output
                  25
                  25

                  1570989024
                  1570989024

 There are two identity operators  
         1. is
         2. is not
ü  We can use identity operators for address comparison.
ü  We can use id() predefined function to check the address of every element.

is operator
ü  A is B returns True, if both A and B are pointing to the same object.
ü  A is B returns False, if both A and B are not pointing to the same object.

is not operator
ü  A is not B returns True, if both A and B are not pointing to the same object.
ü  A is not B returns False, if both A and B are pointing to the same object.

Make a note
ü  The ‘is’ and ‘is not’ operators are not comparing the values of the objects.
ü  They compare the memory locations (address) of the objects.
ü  If we want to compare the value of the objects, we should use equality operators (==).

Program       example by using is operator
Name          demo20.py

                  a = 25
                  b = 25
                 
                  print(a is b)
                  print(id(a))
                  print(id(b))
        
Output
                  True
                  1416520672
                  1416520672

Program       example by using is operator
Name          demo21.py

                  a = 25
                  b = 30
                 
                  print(a is b)
                  print(id(a))
                  print(id(b))
         
Output
                  False 
                  1420977120
                  1420977280

Program       example by using == operator
Name          demo22.py

                  a = 25
                  b = 25
                 
                  print(a == b)
        
output
                  True

Program       example by using == operator
Name          demo23.py

                  a = 25
                  b = 30
                 
                  print(a == b)

output
                  False 

Program       example by using is not operator
Name          demo24.py

                  a = 25
                  b = 25
                 
                  print(a is not b)
        
output
                  False