Python Operators

 Python Operators



Python Operators:

Operators are symbols used to perform a specific operations on variables and values.

For Example

+, -, *,  / ,  %  these are symbols which are used to perform arithmetic operations.

 Following are some operators in Python,

1. Arithmetic Operators:

To perform common mathematical operations arithmetic operators are used.

Addition    A+B   eg.  A=10; B=10  print( A+B)

- Subtraction A-B eg. A=10; B=10  print( A-B)

* Multiplication A*B eg. A=10; B=10  print( A*B)

/ Division A/B e.g. A=10; B=10  print( A/B)

% Modulus A%B eg. A=10; B=10  print( A%B)

** Exponentiation A**B eg. A=2; B=15  print( A**B)  #same as 2*2*2*2*2

// Floor Division A//B A=15; B=2 print(A//B)#round the result  to nearest whole number


2. Assignment Operators:

Assignment operators are used to assign values to the variables.

= A=10

+= A+=10 same as A=A+10 

-= A-=10 same as A=A-10

*= A*=10 same as  A=A*10

/= A/=10 same as A=A/10

%= A%=10 same as  A=A%10

//= A//=10 same as A=A//10

**= A**=10 same as A=A**10

&= A&=10 same as A=A&10

|= A|=10 same as A=A|10

^= A^=10 same as A=A^10

>>= A>>=10 same as A=A>>10

<<= A<<=3 same as A=A<<10


3. Python Comparison Operator:

Comparison operators are used to compare two or more than two values.

== Exactly equal to A==B

!= Not equal to A!=B

> Greator than A>B

< Less than A<B

>= Greater than or equal to A>=B

<= Less than or equal to A<=B


4. Python Logical Operators:

Logical operators are used to combine two or more conditional statements.

  • and : Returns True if both statements are true. A<10 and A<15 A=15

print(A>12 and A<20)

it will return true , because A is greater than 12 and less than 20

  • or Returns true if one statement          is true  A < 5 or A<4

    A=15

print(A>12 or  A<10)

it will return true , because A is greater than 12 but A is not less than 10. So one condition satisfy.

  • not      Reverse the result, Returns False if the result is true. not(A<5 and A<10)

A=10

print(not(A>8 and A<15)

Returns False because not is used to reverse the result.


5. Python Identity Operators:

Identity operators are used to compare two objects.  They are not only equal but both objects have same memory locations.

  • Is Returns True if both variables have same objects

A is B

A=[“apple’, “mango”]

B=[“apple”, “mango”]

C=A

print( A is C) #Return True

print(A is B)         # Return False

print(A ==B) # Returns True

  • is not Returns True if both the variables are not the same objects.

X is not Y

X= [“ Pen”, “Pencil”]

Y=[“Pen”, “Pencil”]

Z=X

print(X is not Z) # Returns False

print(X is not Y) # Returns True

print(X!=Y) # Returns False 

6. Python Membership Operators:

Membership operators are used to test if the sequence is presented in an object.

  • in  Returns True if sequence with specified values is present in object.

A in B

A=[“Pen”,”Pencil”]

print(“Pencil” in A)   Returns True because Pencil is present in list A.

  • not in Returns True if a sequence with specified values is not in the object

X not in Y

X= [“Pen”,” Pencil”]

print(“Rubber” not in X)

Returns True because Rubber is not present in X list.

 

7. Python Bitwise Operator:

Bitwise operators are used to compare binary numbers.

  • & AND Sets each bit to 1 if both bits are 1
  • | OR Sets each bit to 1 if one of two bits is 1
  • ^ XOR Sets each bit to 1 if only one of two bits is 1
  • ~ NOT Inverts all the bits
  • << Zero fill left shift Shift left by pushing zeros in form of right and let the leftmost bits fall off
  • >> Signed right shift Shift right by pushing copies of the leftmost bit in form the left and the rightmost bits fall off.

  

टिप्पणी पोस्ट करा

0 टिप्पण्या