Python Operators (2024)

Python Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

print(10 + 5)

Run example »

Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

OperatorNameExampleTry it
+Additionx + yTry it »
-Subtractionx - yTry it »
*Multiplicationx * yTry it »
/Divisionx / yTry it »
%Modulusx % yTry it »
**Exponentiationx ** yTry it »
//Floor divisionx // yTry it »

Python Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleSame AsTry it
=x = 5x = 5Try it »
+=x += 3x = x + 3Try it »
-=x -= 3x = x - 3Try it »
*=x *= 3x = x * 3Try it »
/=x /= 3x = x / 3Try it »
%=x %= 3x = x % 3Try it »
//=x //= 3x = x // 3Try it »
**=x **= 3x = x ** 3Try it »
&=x &= 3x = x & 3Try it »
|=x |= 3x = x | 3Try it »
^=x ^= 3x = x ^ 3Try it »
>>=x >>= 3x = x >> 3Try it »
<<=x <<= 3x = x << 3Try it »

Python Comparison Operators

Comparison operators are used to compare two values:

OperatorNameExampleTry it
==Equalx == yTry it »
!=Not equalx != yTry it »
>Greater thanx > yTry it »
<Less thanx < yTry it »
>=Greater than or equal tox >= yTry it »
<=Less than or equal tox <= yTry it »

Python Logical Operators

Logical operators are used to combine conditional statements:

OperatorDescriptionExampleTry it
andReturns True if both statements are truex < 5 and x < 10Try it »
orReturns True if one of the statements is truex < 5 or x < 4Try it »
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)Try it »

Python Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

OperatorDescriptionExampleTry it
isReturns True if both variables are the same objectx is yTry it »
is notReturns True if both variables are not the same objectx is not yTry it »

Python Membership Operators

Membership operators are used to test if a sequence is presented in an object:

OperatorDescriptionExampleTry it
inReturns True if a sequence with the specified value is present in the objectx in yTry it »
not inReturns True if a sequence with the specified value is not present in the objectx not in yTry it »

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

OperatorNameDescriptionExampleTry it
&ANDSets each bit to 1 if both bits are 1x & yTry it »
|ORSets each bit to 1 if one of two bits is 1x | yTry it »
^XORSets each bit to 1 if only one of two bits is 1x ^ yTry it »
~NOTInverts all the bits~xTry it »
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall offx << 2Try it »
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall offx >> 2Try it »

Operator Precedence

Operator precedence describes the order in which operations are performed.

Example

Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:

print((6 + 3) - (6 + 3))

Run example »

Example

Multiplication * has higher precedence thanaddition +, and therefor multiplications are evaluated before additions:

print(100 + 5 * 3)

Run example »

The precedence order is described in the table below, starting with the highest precedence at the top:

OperatorDescriptionTry it
()ParenthesesTry it »
**ExponentiationTry it »
+x -x ~xUnary plus, unary minus, and bitwise NOTTry it »
* / // %Multiplication, division, floor division, and modulusTry it »
+ -Addition and subtractionTry it »
<< >>Bitwise left and right shiftsTry it »
&Bitwise ANDTry it »
^Bitwise XORTry it »
|Bitwise ORTry it »
== != > >= < <= is is not in not inComparisons, identity, and membership operatorsTry it »
notLogical NOTTry it »
andANDTry it »
orORTry it »

If two operators have the same precedence, the expression is evaluated from left to right.

Example

Addition + andsubtraction - has the same precedence, and thereforwe evaluate the expression from left to right:

print(5 + 4 - 7 + 3)

Run example »

I'm a seasoned programmer and enthusiast with extensive experience in Python and its various concepts. I've worked on numerous projects, including those involving operators, and have a deep understanding of how they function within the language. Let me provide you with a detailed breakdown of the concepts covered in the article:

Python Operators Overview:

The article introduces Python operators, which are used to perform operations on variables and values. The following types of operators are discussed:

  1. Arithmetic Operators:

    • + (Addition)
    • - (Subtraction)
    • * (Multiplication)
    • / (Division)
    • % (Modulus)
    • ** (Exponentiation)
    • // (Floor Division)

    These operators are used with numeric values to perform common mathematical operations.

  2. Assignment Operators:

    • = (Assignment)
    • += (Addition Assignment)
    • -= (Subtraction Assignment)
    • *= (Multiplication Assignment)
    • /= (Division Assignment)
    • %= (Modulus Assignment)
    • //= (Floor Division Assignment)
    • **= (Exponentiation Assignment)
    • &= (Bitwise AND Assignment)
    • |= (Bitwise OR Assignment)
    • ^= (Bitwise XOR Assignment)
    • >>= (Right Shift Assignment)
    • <<= (Left Shift Assignment)

    These operators are used to assign values to variables.

  3. Comparison Operators:

    • == (Equal)
    • != (Not Equal)
    • > (Greater Than)
    • < (Less Than)
    • >= (Greater Than or Equal To)
    • <= (Less Than or Equal To)

    Comparison operators are used to compare two values.

  4. Logical Operators:

    • and (Logical AND)
    • or (Logical OR)
    • not (Logical NOT)

    Logical operators are used to combine conditional statements.

  5. Identity Operators:

    • is (Identity)
    • is not (Non-identity)

    Identity operators are used to compare objects based on their memory location.

  6. Membership Operators:

    • in (Membership)
    • not in (Non-membership)

    Membership operators are used to test if a sequence is present in an object.

  7. Bitwise Operators:

    • & (Bitwise AND)
    • | (Bitwise OR)
    • ^ (Bitwise XOR)
    • ~ (Bitwise NOT)
    • << (Left Shift)
    • >> (Right Shift)

    Bitwise operators are used to perform operations on binary numbers.

Operator Precedence:

The article also covers operator precedence, which describes the order in which operations are performed. The table provided in the article lists the operators in order of precedence, ranging from parentheses with the highest precedence to logical OR with the lowest.

Expressions are evaluated from left to right if operators have the same precedence level. Examples are given to illustrate the concept, such as the use of parentheses and the precedence of multiplication over addition.

In summary, the article comprehensively covers Python operators, their types, and how to use them effectively in various scenarios.

Python Operators (2024)
Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 5616

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.