05 - Python Operators And Expressions

 

To describe Python operators first we need to refer to something basic from the math, terms called expression, operator and operand. For example, if we have 12 + 18, this is called expression, 12 and 18 are called operands and + sign is called operator.

Now when basic terminology is clear, next relevant thing is to list all type of operators that are supported within the Python:

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Below are described all types of operators along with the examples.

5.1 Arithmetic Operators

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division *

%

Modulus - Returns remainder of division

**

Exponent (power)

//

Floor Division - Returns whole number of division, digits after decimal point are removed

 

* If the type of the operand is integer then only the whole part of a division is returned, digits after the decimal point are removed and operation acts similar to Floor Division

x = 15
y = 7
print x + y        # 22
print x - y         # 8
print x * y        # 105
print x / y        # 2
print x % y       # 1       15/7 = 2 -> 2*7 =14 -> 15-14 = 1, remainder is 1
print x ** y      # 170859375
print x // y       # 2

To understand better how division and floor division works with float data types, check the example below.

x = 15.2
y = 7
print x / y                    # 2.17142857143
print round(x/y, 2)      #2.17   round function rounds result of division to N decimal places
print x % y                   # 1.2    15.2/7 = 2 -> 2*7 =14 -> 15.2 -14 = 1.2, remainder is 1.2
print x // y                   # 2.0    15.2/7 = 2.2 -> after removing digits after decimal point 2.0

5.2 Comparison Operators

Operator

Description

==

Identically Equals - If two operands are equal returns True, otherwise False

!=

If two operands are not equal returns True, otherwise False

<> 

Similar to != operator

Greater than - If value of left operand is greater than value of right operand, returns True, otherwise False

>=

Greater or Equal than - If value of left operand is greater than or equal to value of right operand, returns True, otherwise False

Less than - If value of left operand is less than value of right operand, returns True, otherwise False

<=

Less or Equal than - If value of left operand is less than or equal to value of right operand, returns True, otherwise False

 

x = 15
y = 7
print x == y      # False
print x != y       # True
print x <> y      # True
print x > y        # True
print x >= y      # True
print x < y        # False
print x <= y      # False

 

5.3 Assignment Operators

Operator

Description

=

Assigns value from right side operand to left side operand

+=

Left side operand becomes sum of left side operand and right side operand

-=

Left side operand becomes result of difference left side operand and right side operand

*=

Left side operand becomes the result of multiplication of left side operand with right side operand

/=

Left side operand becomes the result of divergence of left side operand with right side operand

%=

Left side operand becomes the result of modulus of left side operand and right side operand

**=

Left side operand becomes the result of power operation of left side operand with right side operand

//=

Left side operand becomes the result of floor division of left side operand with right side operand

 

If x and y are some operands then:

x += y               becomes          x = x + y
x -= y               becomes          x = x - y
x *= y               becomes          x = x * y
x /= y               becomes          x = x / y
x %= y              becomes          x = x % y
x **= y             becomes          x = x ** y
x //= y              becomes          x = x // y

5.4 Logical Operators

Operator

Description

and

Logical AND - If both operands are True returns True, otherwise False

or

Logical OR - If any of two operands are True returns True, otherwise False

not

Logical NOT - Reverse the logical state. True becomes False, False becomes True

 

x

y

AND

True

True

True

True

False

False

False

True

False

False

False

False


 

  Table 1. Decoding states for AND operation

 

 

 

 

Like us on Facebook