subjects

Dropdown Menu

Monday 15 February 2021

Operators

 An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables.

Ex:

          c = a + b

In this statement      +, = are operators and a, b, c are operands (variables).in this instruction the sum of two variables a, b is carried out and the result is stored in variable c.

 

Operators in Python

 

1. Arithmetic Operators

2. Relational Operators (Comparison Operators)

3. Logical Operators

4. Bitwise operators

5. Assignment Operators

                  6. Special Operators   

Arithmetic Operators:  All the arithmetic operators will perform basic calculations. The operators +,-,*, /, //, % all work the same way as in other languages.

Python Program Demonstrating the use of arithmetic operators                       

a = int (input ("Enter First Value"))

b = int (input ("Enter Second Value"))

print("sum", a+b)

print("difference", a-b)

print("product", a*b)

print("division", a/b)

print("modulus", a%b)

print("floor division", a//b)

print("exponent", a**2)

 

                                                                  Arith.py

 



Relational Operators:  Relational Operators used in comparing the quantities and to formulate some conditions. The value of a relational expression is either true or false.

  Ex:   10 < 100 is true

          100 < 10 is false

Operator

Meaning

     <

     <=

     >

     >=

     ==

     !=

     Is less than

     Is less than or equal to

     Is greater than

     Is greater than or equal to

     Is equal to

     Is not equal to


                      Table: Relational Operators


Python Program Demonstrating the use of Relational operators                        

a = int (input ("Enter First Value"))

b = int (input ("Enter Second Value"))

print("less than: ",a<b)

print("less than or equal to: ",a<=b)

print("greater than: ",a>b)

print("greater than or equal to: ",a>=b)

print("equal to:" ,a==b)

 

 

                Relational.py

output



Logical Operators: In order to evaluate more than one condition we use logical operators.in python we use and, or, not as logical operators

Operator

                 Meaning

       and

       or

       not

 

             logical And

             logical Or

             logical Not

                                            Table: Logical Operators

Python Program Demonstrating the use of Logical operators                       

a = True

b = False

print(a and b)

print(a or b)

print(not a)

                                                Logical.py

Output:



Bitwise operators: we use bitwise operators to manipulate data at bit level.

 Operator

    Meaning

     &

      |

      ^

     <<

     >>

      ~

    Bitwise AND

    Bitwise Or

    Bitwise Exclusive Or

    Shift left

    Shift Right

    Bitwise Inverse

                                      Table: Bitwise operators

Python Program Demonstrating the use of Bitwise operators                       

a = int(input ("Enter First Value"))

b = int(input ("Enter Second Value"))

print("Bitwise AND :",a&b)

print("Bitwise Or :",a|b)

print("Bitwise Exclusive Or :",a^b)

print("Bitwise Inverse :",~a)

print("Shift left :",a<<b)

print("Shift Right :",a>>b)

                                         Bitwise.py

 Output:



ASSIGNMENT Operators: Assignment operator used to store some value        (right side)into   variable(left side). Compound-assignment operators provide a        shorter syntax for assigning the result of an arithmetic or bitwise operator. They    perform the operation on the two operands before assigning the result to the first   operand.

Simple assignment statement

Short Hand Operator

      a = a+1

      a = a-1

      a = a* 1

      a = a / 1

      a = a % b

      a = a&b

      a = a | b

      a = a ^ b

      a = a << b

      a = a >> b

 

       a += 1

       a -=1

       a *=1

       a /=1

       a %= b

       a &= b

       a |= b

       a ^=  b

       a <<= b

       a >>= b

                                                 Table: Assignment Operators

Python Program Demonstrating the use of Assignment operators                       

a = int(input ("Enter First Value"))

b = int(input ("Enter First Value"))

a += b

print("+=",a)

a -= b

print("-=",a)

a *= b

print("*=",a)

a /= b

print("/=",a)

a %= b

print("%=",a)

a //=b

print("//=",a)

a **=b

print("**=",a)

a &= b

print("&=",a)

a |= b

print("|=",a)

a ^=  b

print("^=",a)

a <<= b

print("<<=",a)

a >>= b

print(">>=",a)

 

                                                     Assignment.py

 

Output:



Special Operators:

Membership Operators: in and not in are membership operators in Python

 

            Operator

         Meaning

               in

               not in     

         Member of sequence

    Not a member of sequence

Table:Membership Operators

Python Program Demonstrating the use of membership operators                

list1 = [1,2,3,'Ram','Rani',3.2]

print(1 in list1)

print(1 not in list1)

print('Ram' in list1)

print(5.5 not in list1)


Output:

                           t

Identity operators: “is” and “is not” are the identity operators in Python. They are used to               check if two values (or variables) are located on the same part of the memory. Two                                    variables      that      are equal does not imply that they are identical.

              Operator

            Meaning

               is

               is not  

 Same memory location

Not same memory location



                                                                   Table: Identity Operators                                                                                                                           

Python Program Demonstrating the use of Identity operators                

#for integer values

a = int(input ("Enter First Value"))

b = int(input ("Enter Second Value"))

print("IS",a is b)

print("iS NOT :",a is not b)

#for string values

s1 = 'Hello'

s2 = 'Hello'

print(s1 is s2)

#for list

L1 = [1,2,3]

L2 = [1,2,3]

Print(L1 is L2)

 Output:



No comments:

Post a Comment