Fundamentals 7 min read

Master Python Operators: A Complete Guide to Arithmetic, Comparison, and More

This article introduces Python operators, defining operands and operators, and systematically covers arithmetic, comparison, assignment, logical, bitwise, membership, identity operators, as well as operator precedence, providing clear examples and output illustrations for each category.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Operators: A Complete Guide to Arithmetic, Comparison, and More

What is an Operator?

This section explains Python operators. For example, 4 + 5 = 9 . In this example, 4 and 5 are operands, and + is the operator.

Python supports the following types of operators:

Arithmetic operators

Comparison (relational) operators

Assignment operators

Logical operators

Bitwise operators

Membership operators

Identity operators

Operator precedence

Let's study Python operators one by one.

Python Arithmetic Operators

Assume variable a = 10 and variable b = 20.

The above example demonstrates all arithmetic operators in Python.

Output of the example:

Python Comparison Operators

Assume variable a = 10 and variable b = 20.

The above example demonstrates all comparison operators in Python.

Output of the example:

Python Assignment Operators

Assume variable a = 10 and variable b = 20.

The above example demonstrates all assignment operators in Python.

Output of the example:

Python Bitwise Operators

Bitwise operators treat numbers as binary for calculations.

In the table below, variable a = 60 and b = 13 in binary:

The following example demonstrates all bitwise operators in Python.

Output of the example:

Python Logical Operators

Assume variable a = 10 and b = 20.

Output of the example:

Python Membership Operators

Beyond the previous operators, Python also supports membership operators, testing strings, lists, or tuples.

The following example demonstrates all membership operators in Python.

Output of the example:

Python Identity Operators

Identity operators compare the memory locations of two objects.

The following example demonstrates all identity operators in Python.

Output of the example:

Python Operator Precedence

The table below lists all operators from highest to lowest precedence.

Operator

Description

**

Exponent (highest precedence)

~ + -

Bitwise NOT, unary plus and minus

* / % //

Multiplication, division, modulo, floor division

+ -

Addition and subtraction

>> <<

Right shift, left shift

&

Bitwise AND

^ |

Bitwise XOR and OR

<= < > >=

Comparison operators

<> == !=

Equality operators

= %= /= //= -= += *= **=

Assignment operators

is is not

Identity operators

in not in

Membership operators

not or and

Logical operators

The following example demonstrates operator precedence in Python.

Output of the example:

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonTutorialprogramming fundamentalsoperatorsOperator Precedence
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.