Operators In Python

Arithmetic Operators in Python:

Operator Name

Operator

Description

Example

Addition

+

  • Adds two operands and returns the value.

 

  • Addition operation is commutative. Changing the sequence of operands does not change the resultant sum.

 

  • By commutative property of addition, A + B = B + A

 

  • Addition operation is associative. That is A+(B+C) = (A+B)+C

 

A = 10 + 2

 

The addition operator adds 10 and 2 and returns 12

 

Subtraction

-

  • Subtracts the value of the second operand from the value of the first operand.

 

  •  Subtraction is anti-commutative. Changing the order of the operands in subtraction results in different results.

 

    i.e., A-B ≠ B-A

 

  •  Subtraction is not associative. When subtracting more than two numbers if the sequence of the operation changes the result also changes.

i.e., A-(B-C) ≠ (A-B)-C

 

B = 2 – 4

 

The subtraction operator subtracts 4 from 2 and returns -2.

 

Multiplication

*

  • Multiplies the second operand by the number specified in the first operand.

The operator symbol is an asterisk.

 

  • Multiplication is commutative. Interchanging the first operand and the second operand of the multiplication produces same result.

 

i.e., A* B = B* A

 

  • Multiplication is associative. When multiplying more than two numbers different orders of multiplication will produce the same result.

i.e., A*B*C = C*B*A 

 

 

 

C=4*5

 

The multiplication operator multiplies the number four by five times resulting in the value 20.

Division- Floating point

/

 

  • Divides the first operand by the second operand. The resultant value is a floating-point number even if one of the operators is of type integer.

 

  • The operator symbol is a forward slash.

 

  • Division operation does not commutative and associative properties.

 

D=25/5

 

The denominator divides the numerator 25 by 5 times resulting in a value of 5.0.

Division - Integer

//

  • Divides the first operand by the second operand. The resultant value is an integer value even if one of the operators is a floating-point number.

 

  • The operator symbol is two forward slashes.

 

  • Division operation does not commutative and associative properties.

 

E = 45.0//9

 

The denominator divides the numerator 45.0

by 9 times resulting in a value of 5.

Modulo Division

%

  • Divides the first operand by the second operand and returns the reminder of the division.

 

  • The operator symbol is the percentage sign.

 

 

  • Modulo division does not hold the commutative and associative properties.

Rem = 10%3
 

The denominator divides the numerator by 3 times resulting in a quotient of 3 and a remainder of 1.

 

Relational Operators in Python:

(Also called as Comparision Operators)

Operator Name

Operator

Description

Example

Less Than

Compares whether the left hand side operand value is less than the right hand side operand value. If so, the less than operator returns true. Returns false otherwise.

Count = 10

CurIndex =8

If (CurIndex < Count):

     print(CurIndex)

 

In the above example the relational expression inside the if statement will evaluate to True.

 

Less Than or Equal to

<=

Compares whether the left hand side operand value is less than or equal to the right hand side operand value. If so, the less than or equal to operator returns true. Returns False otherwise.

size =21

currentValue= 21

if(currentValue<=size):

   process(currentValue)

 

In the above example process will be called because the current value is 21 which is equal to the size.

Greater Than

Compares whether the left hand side operand value is greater than the right hand side operand value. If so, the greater than operator returns true. Returns false otherwise.

Length = 20

Position = 21

 

while (length > Position):

     print(position)

In, the above snippet the expression inside the while loop will evaluate to False and the loop will terminate.

Greater Than or Equal to

>=

Compares whether the left hand side operand value is greater than or equal to the right hand side operand value. If so, the greater than or equal to operator returns true. Returns False otherwise.

if(x >=y):

      generate()

 

Here the generate function will be called if y is greater than or equal to the value of x.

Equality or Equal operator

 

==

Compares whether the left hand side operand value is equal to the right hand side operand value.

if(reminder==1) :

     isEven = False

In this example if the reminder is one than expression inside the if statement will return true making isEven as False.

Not Equal to operator

!=

Checks whether the left hand side operand value is not equal to the right hand side operand value. Returns true if the operands are not equal. Returns False if the operands are equal.

isValidToken = (Token != None)

In the above example is ValidToken becomes True  only if the identifier Token is not equal to None.

 

Logical Operators in Python:

 

Operator Name

Operator

Description

Example

And

And

The And operator is the implementation of Logical AND operation.

The And operator returns True only if both the operands evaluate to True. Otherwise it returns False.

if (validUserName == True) and (nameAvailable==True):

        GetPassword()

 

In the above example  Python will invoke GetPassword() only if the user name is valid and it is available.

Or

Or

The Or operator is the implementation of logical OR operation. The Or operator returns True if any or both of the operands evaluate to True. It returns False only when both the operators return False.

if HoldsSSN or HoldsStateID:

   permit()

 

Here the permit() is called if either SSN or State ID is held. permit() is not called only when both SSN and StateID is not available.

Not

Not

Not operator returns True when negation of a truth-value evaluates to True. Returns False otherwise.

While char is Not None:

      emit(char)

 

In the above example, A character is emitted only when it is not None.

 

Bitwise Operators in Python:

Operator Name

Operator

Description

Example

Left shift operator

<< 

  • The left shift operator (<<) shifts the bits of left hand side operand to the left by number of bits specified in the right hand side operand.
  • x << y equals to multiplying x by pow(x,y).

8<<2 results in 32

Right shift operator

>> 

  • The right shift operator (<<) shifts the bits of

left hand side operand to the right by number of bits specified in the right hand side operand.

  • x >> y equals to dividing x using the // operator pow(2,x).

32>>4 results in 2

Bitwise AND operator

&

Each operand is considered as a binary bit pattern and a bitwise AND is performed.

 

Each bit of operand one is done an AND operation with the corresponding bit of operand two.

 

The bit patterns for number six and number one are

Given below:

110

001

 

Bitwise AND on these binary bit patterns will result in 0.

 

 

Bitwise OR operator

|

Each operand is considered as a binary bit pattern and a bitwise OR is performed.

 

Each bit of operand one is done an OR operation with the corresponding bit of operand two.

 

The bit patterns for number eight and number three are given below:

1000

0011

 

Bitwise OR on these binary bit patterns will result in

1011, which is equal to number eleven in decimal system.

Binary XOR

^

Each operand is considered as a binary bit pattern and a bitwise XOR is performed.

 

Each bit of operand one is done an XOR operation with the corresponding bit of operand two.

 

The bit patterns for the numbers seven and four are given here:

111

100

 

Bitwise XOR on these binary bit patterns will result in

011. Recall that XOR results in 1 if only one bit is set and other bit is not set. If both bit or set or not set XOR results in 0.

Ones Complement

~

Returns the ones complement of the number.

Remember that unlike other bitwise operators this is a Unary operator.

In a bit pattern, ones complement is equivalent to flipping all ones by zero and all zeros by one.

 

e.g., 2 corresponds to a bit pattern of 00000010

and taking ones complement results in 11111101.

 


Copyright 2018 © pythontic.com