# Basic Operators in Python 3

B

In this tutorial, we discuss about the Basic Operators in Python. Operators are the constructs, which can manipulate the value of operands. Consider the expression 4 + 5 = 9. Here, 4 and 5 are called the operands and + is called the operator.

Before the start discussion of Basic Operators in Python, it is always recommended you should know about Python Primitives. Follow the link below

PYTHON PRIMITIVES – VARIABLES, BUILT-IN DATA TYPES, COMMENTS, SYNTAX, AND SEMANTICS

### Basic Operators in Python 3

Python language supports the following Basic operators or Types of Operators

• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators

Let us have a look on all basic operators one by one.

#### Python Arithmetic Operators

• + (Addition): Adds values on either side of the operator.
• – (Subtraction): Subtracts right hand operand from left hand operand.
• * (Multiplication): Multiplies values on either side of the operator
• / (Division): Divides left hand operand by right hand operand
• % (Modulus): Divides left hand operand by right hand operand and returns remainder
• ** (Exponent): Performs exponential (power) calculation on operators
• // (Floor Division): The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity)

#### Python Comparison Operators

These operators compare the values on either side of them and decide the relation among them. They are also called Relational operators.

• ( == ): If the values of two operands are equal, then the condition becomes true.
• ( != ): If values of two operands are not equal, then condition becomes true. (a!= b) is true.
> If the value of left operand is greater than the value of right operand, then condition becomes true.
• ( < ): If the value of left operand is less than the value of right operand, then condition becomes true.
• ( >= ): If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.
• ( <= ): If the value of left operand is less than or equal to the value of right operand, then condition becomes true.

#### Python Assignment Operators

• ( = ): Assigns values from right side operands to left side operand
• ( += ): Add AND It adds right operand to the left operand and assign the result to left operand
• ( -= ): Subtract AND It subtracts right operand from the left operand and assign the result to left operand
• ( *= ): Multiply AND It multiplies right operand with the left operand and assign the result to left operand
• ( /= ): Divide AND It divides left operand with the right operand and assign the result to left operand
• ( %= ): Modulus AND It takes modulus using two operands and assign the result to left operand
• ( **= ): Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand
• ( //= ): Floor Division It performs floor division on operators and assign value to the left operand

#### Python Bitwise Operators

Bitwise operator works on bits and performs bit-by-bit operation. Python’s built-in function bin() can be used to obtain binary representation of an integer number. The following Bitwise operators are supported by Python language

• & Binary AND – Operator copies a bit, to the result, if it exists in both operands
• | Binary OR –  It copies a bit, if it exists in either operand.
• ^ Binary XOR – It copies the bit, if it is set in one operand but not both.
• ~ Binary Ones Complement – It is unary and has the effect of ‘flipping’ bits.
• << Binary Left Shift – The left operand’s value is moved left by the number of bits specified by the right operand.
• >> Binary Right Shift – The left operand’s value is moved right by the number of bits specified by the right operand.

#### Python Logical Operators

The following logical operators are supported by Python language.

• and Logical AND – If both the operands are true then condition becomes true.
• or Logical OR – If any of the two operands are non-zero then condition becomes true.
• not Logical NOT – Used to reverse the logical state of its operand.

#### Python Membership Operators

Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below −

• in – Evaluates to true if it finds a variable in the specified sequence and false otherwise.
• not in – Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

#### Python Identity Operators

Identity operators compare the memory locations of two objects. There are two Identity operators as explained below −

• is – Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
• is not – Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

#### Python Operators Precedence

The following table lists all operators from highest precedence to the lowest.

• ** : Exponentiation (raise to the power)
• ~ + – : complement, unary plus and minus (method names for the last two are +@ and -@)
• * / % // : Multiply, divide, modulo and floor division
• + – : Addition and subtraction
• >> << : Right and left bitwise shift
• & : Bitwise ‘AND’
• ^ | : Bitwise exclusive `OR’ and regular `OR’
• <= < > >= : Comparison operators
• <> == != : Equality operators
• = %= /= //= -= += *= **= : Assignment operators
• is is not : Identity operators
• in not in : Membership operators
• not or and : Logical operators
###### SOME ADDITIONAL USEFUL CONTENT FROM OTHER SOURCES

Download an Android Application for online learning app of Study Warehouse

### About the author ##### Pardeep Patel By Pardeep Patel