# Python Basic Operators

### Python Operators

Python operators are the special type of symbols that can be used to perform several operations like arithmetic, comparison, logical, etc.. These are used to manipulate the value of operands. The values or variables that the operators are applied are called the operands. There are different types of operators supported by Python which are discussed below.

#### Python Arithmetic Operators

Arithmetic operators in Python are used to perform arithmetic operations such as addition, subtraction, multiplication, division and so on. Arithmetic operators supported by Python are:

• – (Subtraction): Performs subtraction of right operand from the left operand. Example: x – y
• * (Multiplication): Multiplies two operands. Example: x * y
• / (Division): Divides the operand at the left side by the operand at the right side. Example: x / y
• % (Modulus): Returns the remainder of a division of the left operand by the right operand. Example: x % y
• ** (Exponentiation): Returns the left-hand operand power right-hand operand. Example: x ** y
• // (Floor division): Returns the whole number equal to r just smaller than the quotient. Example: x // y

#### Python Assignment Operators

In python, the operators that are used to assign values to variables are called assignment operators. The major assignment operator is an equal sign (=). However, several arithmetic operators can be used along with the equal sign to perform the arithmetic operation on a variable and assign the result to the same variable. These types of operators are called compound operators. Assignment operators in python are:

• =: Assigns value at the right side to the variable at left. Example: a = 100, assigns 100 to variable a.
• +=: Adds the operand at the right side to the variable at left and assigns the result to the variable at left. Example: a += 1, equivalent to a = a + 1.
• -=: Subtracts the operand at the right side from the variable at left and assigns the result to the variable at left. Example: a -= 1, equivalent to a = a – 1.
• *=: Multiplies the operand at the right side to the variable at left and assigns the result to the variable at left. Example: a *= 2, equivalent to a = a * 2.
• /=: Divides the variable at the left side by the operand at right and assigns the result to the variable at left. Example: a /= 2, equivalent to a = a / 2.
• %=: Performs modulo operation on two operands and assigns the result to the variable at left. Example: a %= 2, equivalent to a = a % 2.
• **=: Performs an exponential operation on the operands and assigns the result to the variable at left. Example: a **= 2, equivalent to a = a ** 2.
• //=: Performs floor division on the operands and assigns the resulting value to the left variable. Example: a //= 2, equivalent to a = a // 2.

#### Python Comparison (Relational) Operators

The comparison or relational operators in python compare the values or variables on either side of them and return True or False based on the relation. The comparison operators allowed in python are:

• == (Equal to): Returns True if the operands at both sides are equal, returns False otherwise. Example: a == b.
• != (Not equal to): Returns True if the operands are not equal, returns False otherwise. Example: a != b.
• > (Greater than): Returns True if the left operand is greater than the operand at right, returns False otherwise. Example: a > b.
• >= (Greater than or equal to): Returns True if the left operand is either greater than or equal to the operand at right, returns False otherwise. Example: a >= b.
• < (Less than): Returns True if the left operand is less than the operand at right, returns False otherwise. Example: a < b.
• <= (Less than or equal to): Returns True if the left operand less than or equal to the operand at right, returns False otherwise. Example: a <= b.

#### Python Logical Operators

The logical operators in Python allow us to combine two or more conditional operators and return True or False based on the results of those conditional operators combined with logical operators. The logical operators in Python are:

• and: Returns True if both the conditions are true otherwise returns False. Example: (a > 10) and (a < 20).
• or: Returns True if one of the conditions is true, otherwise returns False. Example: (a > 10) or (a < 20).
• not: Also called negation, this operator is used to reverse the logical state of its operand. For example: not (a > 10).

#### Python Bitwise Operators

As the name suggests, bitwise operators perform bit operations bit by bit on the operands. They support one or more operands by converting the operands into binary bits or bit patterns and applying bit operations on individual bits instead of as a whole. The bitwise operators in python are:

• &: Performs bitwise AND operation. Example: a & b.
• |: Performs bitwise OR operation. Example: a | b.
• ~: Performs bitwise NOT operation. Example: ~ a.
• ^: Performs bitwise XOR operation. Example: a ^ b.
• >>: Performs bitwise right shift operation. Example: a >> 1.
• <<: Performs bitwise left shift operation. Example: a << 1.

#### Python Membership Operators (in / not in)

The membership operators in python are used to test for membership of values or variables in sequences like strings, lists, tuples, sets, and dictionaries. There are two membership operators available in python as follows:

• in: Returns True if the value or variable is found in the sequence. Example: 10 in a.
• not in: Returns True if the value or variable is not found in the sequence. Example: 10 not in a.

#### Python Identity Operators (is / is not)

Python identity operators are used to compare the memory locations of two objects. The two identity operators supported by python are as follows:

• is: Returns True if the two objects have the same memory locations. Example: a is b.
• is not: Returns True if the two objects do not have the same memory locations. Example: a is not b.