Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Mastering Python Operators: A Comprehensive Guide

Mastering Python Operators

In this tutorial, you'll master all Python operator categories—syntax, behavior, and practical examples—to write cleaner, more efficient code.

Video: Operators in Python

What are operators in Python?

Operators are special symbols that perform arithmetic or logical computations. The values they act upon are called operands.

For example:

>>>>> 2+3
5

Here, + is the operator performing addition, 2 and 3 are operands, and 5 is the result.


Arithmetic Operators

Arithmetic operators execute mathematical operations such as addition, subtraction, multiplication, and more.

OperatorMeaningExample
+Add two operands or unary plusx + y + 2
-Subtract right operand from left or unary minusx - y - 2
*Multiply two operandsx * y
/Divide left operand by right (always returns float)x / y
%Modulus – remainder of divisionx % y (remainder of x/y)
//Floor division – whole number result rounded downx // y
**Exponentiation – left raised to right powerx ** y (x to the power y)

Example 1: Arithmetic Operators in Python

x = 15
y = 4

print('x + y =', x + y)
print('x - y =', x - y)
print('x * y =', x * y)
print('x / y =', x / y)
print('x // y =', x // y)
print('x ** y =', x ** y)

Output

x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

Comparison Operators

Comparison operators evaluate relationships between values, returning True or False.

OperatorMeaningExample
>Greater thanx > y
<Less thanx < y
==Equal tox == y
!=Not equal tox != y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Example 2: Comparison Operators in Python

x = 10
y = 12

print('x > y is', x > y)
print('x < y is', x < y)
print('x == y is', x == y)
print('x != y is', x != y)
print('x >= y is', x >= y)
print('x <= y is', x <= y)

Output

x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True

Logical Operators

Logical operators—and, or, not—combine Boolean expressions.

OperatorMeaningExample
andTrue if both operands are truex and y
orTrue if at least one operand is truex or y
notNegates the operandnot x

Example 3: Logical Operators in Python

x = True
y = False

print('x and y is', x and y)
print('x or y is', x or y)
print('not x is', not x)

Output

x and y is False
x or y is True
not x is False

Below is a truth table illustrating these operators.


Bitwise Operators

Bitwise operators manipulate individual bits of integer operands.

For example, 2 is 10 in binary, and 7 is 111.

Let x = 10 (00001010 binary) and y = 4 (00000100 binary).

OperatorMeaningExample
&Bitwise ANDx & y = 0 (00000000)
|Bitwise ORx | y = 14 (00001110)
~Bitwise NOT~x = -11 (11110101)
^Bitwise XORx ^ y = 14 (00001110)
>>Right shiftx >> 2 = 2 (00000010)
<<Left shiftx << 2 = 40 (00101000)

Assignment Operators

Assignment operators assign values to variables, including compound forms that combine an operation with assignment.

OperatorExampleEquivalent to
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
//=x //= 5x = x // 5
**=x **= 5x = x ** 5
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = x << 5

Special Operators

Python also includes identity and membership operators for advanced comparisons.

Identity Operators

is and is not check whether two references point to the same object.

OperatorMeaningExample
isTrue if operands refer to the same objectx is True
is notTrue if operands refer to different objectsx is not True

Example 4: Identity Operators in Python

x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]

print(x1 is not y1)
print(x2 is y2)
print(x3 is y3)

Output

False
True
False

Integers and short strings are interned, so identical literals often share the same object. Lists are distinct objects even when they contain the same items.

Membership Operators

in and not in test for the presence of a value within a sequence, set, or dictionary key set.

OperatorMeaningExample
inTrue if value is found in the sequence5 in x
not inTrue if value is absent from the sequence5 not in x

Example 5: Membership Operators in Python

x = 'Hello world'
y = {1:'a',2:'b'}

print('H' in x)
print('hello' not in x)
print(1 in y)
print('a' in y)

Output

True
True
True
False

Notice that string comparisons are case‑sensitive and dictionary checks only consider keys.


Python

  1. C# Operators – Comprehensive Guide to Operators in C#
  2. Mastering C++ Operators: A Complete Guide with Practical Examples
  3. Mastering C Programming Operators: A Comprehensive Guide
  4. Python List Operations: Creation, Access, Modification, and Advanced Techniques
  5. Mastering Python Tuples: Creation, Access, and Advanced Operations
  6. Mastering Python Dictionaries: Creation, Manipulation, and Advanced Techniques
  7. Master Java Operators: Types, Syntax, & Practical Examples
  8. Comprehensive Guide to C Operators: Types, Functions & Examples
  9. Mastering C# Operators: A Comprehensive Guide
  10. Master Python Basic Operators: Arithmetic, Comparison, Logical & More