OverView
Operators in Python are symbols or keywords that perform operations on operands. Operands can be variables, literals, or expressions. Python supports a variety of operators, categorized into different types. This documentation provides an overview of the main types of operators in Python.
Arithmetic operators perform basic mathematical operations :
+ (Addition): Adds two operands.- (Subtraction): Subtracts the right operand from the left operand.* (Multiplication): Multiplies two operands./ (Division): Divides the left operand by the right operand.% (Modulus): Returns the remainder of the division.// (Floor Division): Returns the floor value after division.** (Exponentiation): Raises the left operand to the power of the right operand.Arithmetic Operators Example
Operator Name Example +Addition a + b-Subtraction a - b*Multiplication a * b/Division a / b%Modulus a % b**Exponentiation a ** b//Floor division a // b
2. Comparison Operators
Comparison operators compare two values and return a Boolean result :
== (Equal): True if the operands are equal.!= (Not Equal): True if the operands are not equal.< (Less Than): True if the left operand is less than the right operand.> (Greater Than): True if the left operand is greater than the right operand.<= (Less Than or Equal To): True if the left operand is less than or equal to the right operand.>= (Greater Than or Equal To): True if the left operand is greater than or equal to the right operand.Comparison Operators Example
Operator Name Example Result ==Equal a == bTrue if a is equal to b !=Not Equal a != bTrue if a is not equal to b <Less Than a < bTrue if a is less than b >Greater Than a > bTrue if a is greater than b <=Less Than or Equal To a <= bTrue if a is less than or equal to b >=Greater Than or Equal To a >= bTrue if a is greater than or equal to b
3. Logical Operators
Logical operators perform logical operations and return a Boolean result :
and : True if both operands are true.or : True if at least one operand is true.not : True if the operand is false (logical negation).Logical Operators Example
Operator Name Example Result andLogical AND a and bTrue if both a and b are true orLogical OR a or bTrue if at least one of a or b is true notLogical NOT not aTrue if a is false; False if a is true
4. Assignment Operators
Assignment operators assign values to variables :
= (Assignment): Assigns the value on the right to the variable on the left.+= (Addition Assignment): Adds the right operand to the left operand and assigns the result to the left operand.-= (Subtraction Assignment): Subtracts the right operand from the left operand and assigns the result to the left operand.*= (Multiplication Assignment): Multiplies the left operand by the right operand and assigns the result to the left operand./= (Division Assignment): Divides the left operand by the right operand and assigns the result to the left operand.Assignment Operators Example
Operator Name Example Equivalent =Assignment a = bAssigns the value of b to a +=Addition Assignment a += bEquivalent to a = a + b-=Subtraction Assignment a -= bEquivalent to a = a - b*=Multiplication Assignment a *= bEquivalent to a = a * b/=Division Assignment a /= bEquivalent to a = a / b%=Modulus Assignment a %= bEquivalent to a = a % b**=Exponentiation Assignment a **= bEquivalent to a = a ** b//=Floor Division Assignment a //= bEquivalent to a = a // b
5. Membership Operators
Membership operators test if a value is a member of a sequence:
in : True if a value is found in the sequence.not in : True if a value is not found in the sequence.Membership Operators Example
Operator Name Example Result inMembership - True if value is found in the sequence a in bTrue if a is found in b not inMembership - True if value is not found in the sequence a not in bTrue if a is not found in b
6. Identity Operators
Identity operators test the identity of objects :
is : True if both operands refer to the same object.is not : True if both operands do not refer to the same object.Identity Operators Example
Operator Name Example Result isIdentity - True if both variables are the same object a is bTrue if a and b refer to the same object is notIdentity - True if both variables are not the same object a is not bTrue if a and b do not refer to the same object
7. Bitwise Operators
Bitwise operators perform bit-level operations :
& (Bitwise AND)| (Bitwise OR)^ (Bitwise XOR)~ (Bitwise NOT)<< (Left Shift)>> (Right Shift)Bitwise Operators Example
Operator Name Example Result &Bitwise AND a & bEach bit position in the result is the logical AND of the bits in the corresponding positions of a and b |Bitwise OR a | bEach bit position in the result is the logical OR of the bits in the corresponding positions of a and b ^Bitwise XOR a ^ bEach bit position in the result is the logical XOR of the bits in the corresponding positions of a and b ~Bitwise NOT ~aThe bitwise NOT of a is the same as -(a+1) <<Left Shift a << bShifts the bits of a to the left by b positions >>Right Shift a >> bShifts the bits of a to the right by b positions
Understanding and using these operators is essential for writing efficient and expressive Python code. Operators play a crucial role in manipulating data and controlling program flow.