Python Operators: Arithmetic, Assignment, Comparison, Boolean, Bitwise, and Precedence
This article explains Python's arithmetic, assignment, comparison, Boolean, and bitwise operators, provides example code for each, and outlines the language's operator precedence rules to help developers write correct and readable expressions.
Arithmetic Operators
Python provides arithmetic operators such as + for addition (including string concatenation), - for subtraction, * for multiplication (including string repetition), / for true division, % for modulo, ** for exponentiation, and // for floor division, each returning appropriate numeric or string results.
Assignment Operators
Assignment operators assign values to variables, including = , += , -= , *= , /= , %= , **= , and //= , which combine an operation with assignment (e.g., x += 3 is equivalent to x = x + 3 ).
Comparison Operators
Comparison operators evaluate relationships between two values and return Boolean results: == , != , > , < , >= , and <= .
Boolean Operators
Logical operators and , or , and not combine Boolean values, returning True or False based on logical rules.
Bitwise Operators
Bitwise operators manipulate integer binary representations: & (and), | (or), ^ (xor), ~ (not), << (left shift), and >> (right shift), each producing integer results.
Operator Precedence
Python evaluates expressions according to precedence: parentheses () , exponentiation ** , unary + - ~ , multiplication/division/floor division/modulo * / // % , addition/subtraction + - , bitwise shifts << >> , bitwise & | ^ , comparisons < > <= >= == != , and finally logical not , and , or . Within the same level, evaluation proceeds left‑to‑right.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.