OPERATORS IN PYTHON

In previous tutorial we have seen the basics of python that is the playing with numbers ,literals,constants etc. but now in this tutorial we will learn about what are operators , what are their types ,how they are operated in python .

What do you mean by operator :

In Python, we can defined operators as the  special symbols that designate  some sort of computation should be performed. The values that an operator acts on are called operands.

>>> a =1

>>> b = 2

>>> a + b

 >>>3

Arithmetic operators

As it is cleared by its name that , Arithmetic Operators are used for Arithmetic operations..

>>>x = 5

>>>y = 2

>>>x+y

>>>7

here addition (+) or unary plus is used for arithmetic addition.

>>>x-y

>>>3

here subtraction (-) subtract right operand from left

>>>x*y

>>>10

here multiplication (*)  multiply two numbers

>>>x/y

>>>2 .5

here Division (/) Divides left operand by the right one>

>>x%y

>>>1

here modulus (%) takes the remainder

>>> x//y

> >>2

here Floor division (//) Division that results into the whole number truncating digits after decimal point?>>>x**y

>>>25

here  Exponent (**) – left operand raised to the power of right

Relational Operator or Comparison Operator

Relational or Comparison Operators are used to compare the operands on the either side of them.

>>>x=4

>>>y=6

  • Greater than (>):

Example : x > y will return true if x is greater than y

>>>x>y

>>>false

  • Less than (<):

Example : x < y will return true if x is less than y

>>>x<y

>>>true

  • Equal to (==):

Example: x == y will return true if x is equal to y

>>>x==y

>>>false

  • Not equal to (!=):

Example   x != y will return true if x is not equal to y

>>>x!=y

>>>true

  •  Greater Than or Equal to (>=) :

Example : x >= y will return true if x is greater than or equal to y

>>>x>=y

>>>false

  •  Less Than or Equal to (<=):

Example : x <= y will return true if x is less than or equal to y

>>>x<=y

>>>true

Logical Operators

In python , there are three  logical operators:

  • and
  • or
  • not

 

  • and : (x and y) will return true if x and y both are true

>>>x=1

>>>y=2

>>>(x>2 and y>1)

>>>false

  • or :(x or y) will return true if either of x or y is true

>>>  (x<2 or y<1)

>>>true

  • not : (not x) will return true if x is false or will return false if x is true.  not  reverses or complements the operands

>>>not (x<2)

>>>false

 Assignment Operators

As simple as it sounds assignment operators are used for assigning values to variables.

In python we have many assignment operators such as =,+=,-=,,/=,%=,*=, **=,//=

>>>x=1

>>>x+=1

>>>print (x)

>>>2

>>>x-=1

>>>print (X)

>>>0

>>>x=9

>>>x/=2

>>>print (X)

>>>4.5

>>>x%=2

>>>print(X)

>>>0.5

>>>x*=2

>>>print(x)

>>>1.0

>>>x**=2

>>>print(X)

>>>1.0

>>>x//=2

>>>print(X)

>>>0.0

 Bitwise Operators

Bitwise  take binary digits as operands and perform bit by bit operations.

Following is the list of bitwise supported in Python.

  • >>>a =  0101
  • >>>b =  0111

there are so many bitwise here

  • & (Binary AND)
  • | (Binary OR)
  • ^ (Binary XOR)
  • ~ (Binary NOT)
  • >> (Binary right shift)
  • << (Binary left shift)

>>>print(a&b)

>>>0101

>>>print (a|b)

>>>0111

>>> print (a ^ b)

>>>0010

>>>print(~ a)

>>>1010

>> >print(a >> 1)

>>>0010

>>>print(b << 1)

>>>1110

Membership Operators

In Python membership operator is used to check whether a variable is a member of a sequence such as String, List, Tuple and Dictionary. In the Python dictionary, we can check the membership of key only, not the value.

Here  in Python  we have in , not in

  • in :- True if x is the member of y
  • not in :- True if x is not the member of y

>>>x =’yolo’

>>>y=’o’

>>>print(y in x)

>>>false

>>>print(x  not in y)

>>>true

 Identity Operators

Python identity operators are used to check if the operands have identical memory location. In simple language, it compares the memory location of two objects and returns True if both objects have identical or same memory location.

Here we have in Python  is , not is

  • is :- True if both variables point to same memory location .
  • not is :-True if both variable point separate memory location .

>>>x=1

>>>y=1

>>>z=2

>>>print (x is y)

>>>true

>>>print ( x is not z)

>>>true

Precedence 

Here is the list of Python Operators in descending order, listing from higher precedence to lower precedence.

  • ( ) :- parenthesis
  • ** :- exponents
  • +, -, ~ :- Unary plus, Unary minus and Bitwise NOT
  • *, /, %, // :- Multiplication, Division, Modulus and Floor Division
  • +, –  :- Addition and Subtraction
  • >>, << :- bitwise right shift ant left shift
  • & :- bitwise and
  • ^, | :- :- bitwise xor and or
  • <=, <, >, >= :- comparison operators
  • ==, !=  :- Equality Operators
  • =, %=, /=, //=, -=, +=, *=, **= :-  Assignment Operators
  • is, is not :- Identity Operators
  • in, not in :- Membership Operators
  • not, or, and :- Logical Operators

https://docs.python.org

 

%d bloggers like this: