#4 Python Glossary: Operator

Operators in python is symbol that can perform value or variable. Operator can execute math, logic, assignment, comparison et cetera operation and give result. Without operator, variable only, python is only store value. But, python have operators so it can make calculate and imitate human mind.

There are various operators in python.

  • Arithmetic operator
  • comparison/ relational operator
  • assignment operator
  • logical operator
  • membership operator
  • Identity operator
  • presedence operator

Arithmetic Operator

Arithmetic operator takes two value or variable then performs a calculation on them. The math operation is for example : addition (+), subtraction (-), multiplication (*) , division (/), %modulus , exponentiation(**). There are basic mathematical operations.

>>> #this is addition or plus
>>> 3+5
>>> #this is substraction or minus
>>> 10 - 3
>>> #this is multiply two operand (value or variable)  either side of the operator
>>> 4 * 5
>>> #division divide first operand by second operand
>>> 10 / 2
>>> # modulus divides and returns the value of the remainder.
>>> 10%3

Comparison/ Relational Operator

Comparison/ relational operators compare the values on two sides of the value or variable. For example, 4 < 2 (output = False). After we write a comparison between value /variable/operand, then Python program in run mode will tell whether an operand is greater than the other, lesser, equal, or a combination of those. Various comparison operators are ( ==, != , <>, >,<=, etc)

>>> #Equal to (==) check whether first operand equal to the second
>>> 5 == 5
>>> x = 10
>>> x == 11
>>> # Not equal to (!=) will check whether first is not equal to the second
>>> 3 != 1
>>> 5 != 5

Assignment Operator

Assignment operators in Python assign the value to a variable. Assign variable that already has value to a variable is allowed. But we can not assign a variable that has no value to a variable. There are several assignment operators used in Python (=, +=, – = , *=, /= , etc.)

Logical Operator

Logical operators consist 3 operator: or, and, not. That are used for combined conditional statements are true or false. The ‘and’ operator return to True only if two side are True. The ‘or’ operator return to True if one or two side are True. The ‘not’ for True is False. The ‘not’ for False is True.

Membership Operator

Membership operator check whether some value is membership of lists. There are two membership operator in Python: ‘in’ and ‘not in’

Identity Operator

Identity operator check whether two object is same. There are two identity operator. There are ‘is’ and ‘not is’.

Precedence operator

Precedence operator avoids ambiguity. It determines which operators need to be evaluated first. Precedence operator consists of parenthesis or (). Then multiplication has a higher precedence than addition.