Tutorials Logic
Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Python Operators Tutorial - Arithmetic, Logical, Comparison

Python Operators

An operators are used to perform a mathematical operation on single or multiple operands. There are various operators available in Python, which are categorized into 5 parts-

Arithmetic Operators:-

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (Remainder of a division)
**Exponentiation
//Floor Division

Assignment Operators:-

OperatorDescription
=Assign
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign
%=Modulus and assign
//=-
**=-
&=-
|=-
^=-
>>=-
<<=-

Comparison Operators:-

OperatorDescription
OperatorDescription
==Equal to
!=Not equal to
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to

Boolean or Logical Operators:-

OperatorDescription
andLogical AND
orLogical OR
notLogical NOT

Identity Operators:-

OperatorDescription
is-
is not-

Membership Operators:-

OperatorDescription
in-
not in-

Bitwise Operators:-

OperatorDescription
&-
|-
^-
~-
<<-
>>-

Python Operators in Action

Let's see all the major operator categories with practical code examples.

Arithmetic & Assignment
a, b = 10, 3

# Arithmetic
print(a + b)   # 13  - addition
print(a - b)   # 7   - subtraction
print(a * b)   # 30  - multiplication
print(a / b)   # 3.333... - true division (always float)
print(a // b)  # 3   - floor division (integer result)
print(a % b)   # 1   - modulus (remainder)
print(a ** b)  # 1000 - exponentiation

# Assignment operators
x = 5
x += 3;  print(x)  # 8
x -= 2;  print(x)  # 6
x *= 4;  print(x)  # 24
x //= 5; print(x)  # 4
x **= 2; print(x)  # 16
x %= 5;  print(x)  # 1

Comparison, Identity, and Membership Operators

Comparison, Identity, Membership
# Comparison operators
print(5 == 5)   # True
print(5 != 3)   # True
print(10 > 5)   # True
print(3 >= 3)   # True
print(2 < 5)    # True
print(4 <= 3)   # False

# Logical operators
print(True and False)  # False
print(True or False)   # True
print(not True)        # False

# Identity operators - check if same object in memory
a = [1, 2, 3]
b = a          # same reference
c = [1, 2, 3]  # different object, same value

print(a is b)      # True  - same object
print(a is c)      # False - different objects
print(a is not c)  # True

# Membership operators - check if value exists in sequence
fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits)      # True
print('mango' not in fruits)  # True

text = 'Hello, Python!'
print('Python' in text)       # True
print('Java' not in text)     # True

# Walrus operator := (Python 3.8+)
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if (n := len(numbers)) > 5:
    print(f"List has {n} elements, which is more than 5")
Key Takeaways
  • Python uses // for floor division (integer result) and / for true division (always returns float).
  • The ** operator handles exponentiation - use it instead of pow() for simple cases.
  • is checks object identity (same memory address); == checks value equality.
  • in and not in are membership operators - they work on lists, tuples, strings, sets, and dicts.
  • Python logical operators are words (and, or, not) not symbols (&& || !) like in C/Java.
  • The walrus operator := (Python 3.8+) assigns and returns a value in a single expression.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.