# 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

Arithmetic operators are operators that we usually find for mathematical operations. Arithmetic itself is a branch of mathematics that discusses simple calculations, such as times, dividing, adding, and subtracting.

- 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

The comparison operator is used to compare 2 values, whether the values are the same as big, smaller, bigger, and others. The result of this comparison operator is a Boolean True or False

== 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

Logical operators are operators that are used to make logical inferences from 2 Boolean conditions: true or false. In Python there are 3 logical 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

Bitwise is a special operator for handling logical operations of binary numbers in the form of bits.

The binary number itself is a type of number that only consists of 2 types of numbers, namely 0 and 1. If the original value used is not a binary number, it will be automatically converted to a binary number. For example 7 decimal = 0111 in binary numbers.

In practice, bitwise operators are not used very often, unless you are creating a program that must process computer bits. Apart from that these operators are quite complex and one should have an understanding of the binary number system. In this discussion, I assume you already understand the difference between binary numbers (base 2) and decimal numbers (base 10).

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

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:

- 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

Understanding and Examples of Python Identity Operators

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

Understanding and Examples of Python 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