Python Basic Rules #3

Python Programming Language Operators

The following are the types of operators in the Python programming language :

  • Arithmetic Operators
  • Comparative / Relational Operators
  • Logical / Boolean Operators
  • Bitwise Operators
  • Assignment Operators
  • Identity operator
  • Membership Operators

Arithmetic

  • Addition (+) Example 6+5, result 11
  • Subtraction (-) Example 7–4, result 3
  • Multiplication (*) Example 2*3, result 6
  • Division (real / fraction) (/) Example 6/2, result 3
  • Division (rounded down) (//) Example 9/5, result 4
  • Modulus (remaining quotient) (%) Example 10/3, result 1
  • Appointment (**) Example 6**6, result 36

Comparative / Relational Operators

== Equal, Example 4==4, result True
Not the same as, Example 4!=4, result False
Greater than, Example 6 > 3, result True
Smaller, Example 6 < 3, result False
Greater than or equal to, Example 6 >= 3, result True
Less than or equal to, Example 6 >= 6, result False

Logical / Boolean Operators

  • and, True if both an operands are True, Example True and False, result False
  • or, True if one of an operands is True, Example True or True, result True
  • not, True if an operand is False, Example not False, result True

Bitwise Operators

Python language supports 6 types of bitwise operators:

  • &, And, Example 10 & 12, Binary 1010 & 1100, result binary 1000, result decimal 8
  • |, Or, Example 10 | 12, Binary 1010 | 1100, result binary 1110, result decimal 14
  • ^, X or , Example 10 ^ 12, Binary 1010 ^ 1100, result binary 0110 result decimal 6
  • ~, Not, Example ~ 10, Binary ~1010, result Binary 0101, decimal –11 (two complement)
  • <<, Left shift, Example 10 << 1, Binary 1010 << 1, result binary 10100, result decimal 20
  • >>, Right shift, Example 10 >> 1, Binary 1010 >> 1, result binary 101, result binary 5

Examples of coding

result

Assignment Operators

These operators are of 2 types:

  • is, Value True if both operands refer to the same object and contain the same value
  • not, Value True if both operands refer to unequal objects

Identity operator

The identity operator is an operator that can be used to check whether the value of a variable is in the same place (in memory) or not. These operators are also known as identity operators.

These operators are of 2 types :

Operator Explanation
is Value True if both operands refer to the same object and contain the same value
is not Value True if both operands refer to unequal objects

Membership Operators

A membership operator is an operator that is used to check whether a value is in a set or not. The set in question consists of data types “in the form of an array” such as strings, lists, tuples, sets, and dictionaries. These operators are also known as membership operators.

These operators are of 2 types:

Operator Explanation
in Value True if the value you are looking for is in the set
not in Value True if the value you are looking for is not in the set

i am good girl