Operators

Operators are used to perform operations on variables and data.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Identity Operators
  7. Membership Operators

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on variables or data.

OperatorNameCodeDescription
+Additiona + bAdds two data values.
-Subtractiona - bSubtracts two data values.
*Multiplicationa * bMultiply two data values.
/Divisiona / bDivides two data values.
%Modulusa % bReturn remainder when two data values are divided.
//Floor Divisiona // bReturn answer of floor division.
**Exponentiationa ** bRaises the first operand to the power of the second operand.

Assignment Operators

Assignment operators are used to assign values to variables.

OperatorNameCodeDescription
=Assignmenta = 5Assign value to a variable.
+=Addition Assignmenta += bAdds a and b and stores result in a.
-=Subtraction Assignmenta -= bSubtracts a and b and stores result in a.
*=Multiplication Assignmenta *= bMultiplies a and b and stores result in a.
/=Division Assignmenta /= bDivides a and b and stores result in a.
//=Floor Division Assignmenta //= bApplies floor division on a and b and stores result in a.
%=Modulus Assignmenta %= bTakes modulus of a and b and stores result in a.
**=Exponentiation Assignmenta **= bRaises a to the power of b and assigns the result to a.
|=Bitwise OR Assignmenta |= bPerforms bitwise OR operation between a and b, and assigns the result to a.
&=Bitwise AND Assignmenta &= bPerforms bitwise AND operation between a and b, and assigns the result to a.
^=Bitwise XOR Assignmenta ^= bPerforms bitwise XOR operation between a and b, and assigns the result to a.
<<=Left Shift Assignmenta <<= bShifts the bits of a to the left by the number of positions specified on b, and assigns the result to a.
>>=Right Shift Assignmenta >>= bShifts the bits of a to the right by the number of positions specified on b, and assigns the result to a.

Comparison Operators

Comparison operators are used to compare two variables or data values.

OperatorNameCodeDescription
==equal toa == bCheck if a is equal to b.
!=not equal toa != bCheck if a is not equal to b.
>greater thana > bChecks whether a is greater than b.
<less thana < bChecks whether a is less than b.
>=greater than or equal to thana >= bChecks whether a is greater than or equal to b.
<=less than or equal to thana <= bChecks whether a is less than or equal to b.

Logical Operators

Logical operators are used along with conditional statements.

OperatorNameSyntaxDescription
andLogical ANDcondition1 and condition2check if both conditions are true.
orLogical ORcondition1 or condition2check if condition1 or condition2 is true.
notLogical NOTnot conditionreturn the opposite of the given condition.

Bitwise Operators

Bitwise operators perform operations on bit level on numbers.

OperatorNameSyntaxDescription
&Bitwise ANDnum1 & num2Performs a bitwise AND operation on the corresponding bits of two integers.
|Bitwise ORnum1 | num2Performs a bitwise OR operation on the corresponding bits of two integers.
^Bitwise XORnum1 ^ num2Performs a bitwise exclusive OR (XOR) operation on the corresponding bits of two integers.
~Bitwise NOTnum1 ~ num2Performs a bitwise NOT operation, which inverts the bits of the integer.
&ls;Bitwise Left Shiftnum1 &ls;&ls; num2Shifts the bits of an integer to the left by a specified number of positions.
&rs;Bitwise Right Shiftnum1 &rs;&rs; num2Shifts the bits of an integer to the right by a specified number of positions.

Identity Operators

Identity operators are used to compare the memory locations of two objects to determine whether they are the same object or different.

OperatorNameSyntaxDescription
isisx is yreturns True if both operands point to the same object in memory,
is notis not x is not yreturns True if both operands do not point to the same object in memory,

Membership Operators

Membership operators are used to test whether a value is a member of a sequence (like a string, list, or tuple) or a collection (like a dictionary or a set).

OperatorNameSyntaxDescription
ininvalue in sequencereturns True if the specified value is found in the sequence or collection
not in not invalue not in sequencereturns True if the specified value is not found in the sequence or collection

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Medium

What will be the output of this code?

15 XP~4 min
a = 10, b = 3
result = a / b
print(result)
Exercise 2 of 2Easy

What does the modulo operator (%) return?

10 XP~2 min