Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • 📝 Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • 📝 Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • 📝 Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • 📝 Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • 📝 While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • 📝 For Each Assignments
    • 📝 All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • 📝 Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • 📝 Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • 📝 Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • 📝 Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • 📝 Assignments
  • Exception
    • Error vs Exception
    • Exception Handling
    • Types of Exception
    • User Defined Exception
    • Logging Exception
    • 📝 Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • 📝 Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • 📝 Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • 📝 Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • 📝 Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • 📝 Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • 📝 Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • 📝 Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • 📝 Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • 📝 Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • 📝 Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • 📝 Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • 📝 Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • 📝 Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

⚙️ Python Operators & Expressions: A Practical Guide

Python Operators and Expressions

Operators are the tools that let you build expressions and manipulate data in Python.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
We are a team of Python developers, data engineers and data scientists with years of real-world experience. We have built production applications. We have tackled complex problems. We have helped teams ship better code. What drives us? Teaching — especially that moment when things finally click. Our tutorials focus on practical examples and honest guidance. Whether you are just starting out or leveling up we are here to make Python genuinely useful.
📑 On this page:
  • What are Operators & Expressions?
  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Special Operators
  • Try It Yourself
  • Quick Quiz
  • FAQ
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
📚 What You'll Learn Here
  • What operators are and how they form expressions
  • Arithmetic operators for math operations
  • Assignment operators to assign values
  • Comparison operators to compare values
  • Logical operators for boolean logic
  • Bitwise operators for low-level operations
  • Special operators like identity and membership
  • Hands-on practice with the interactive editor

What Are Operators and Expressions?

In Python, an operator is a symbol that tells the interpreter to perform a specific mathematical, logical, or other operation. An expression is a combination of values, variables, and operators that evaluates to a single value.

Think of operators as verbs in a sentence — they act on data (operands) to produce results. For example, in a + b, the + is the operator, and a and b are operands.

# Simple expressions
x = 10 + 5      # addition expression
y = x * 2       # multiplication expression
is_greater = x > y  # comparison expression
                                

💡 Key insight: Every expression in Python evaluates to a value. Understanding operators is the key to writing powerful, concise code.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. They work on numeric values (int, float, complex).

a = 10
b = 3

print("Addition:", a + b)        # 13
print("Subtraction:", a - b)     # 7
print("Multiplication:", a * b)  # 30
print("Division:", a / b)        # 3.333...
print("Floor Division:", a // b) # 3
print("Modulus:", a % b)         # 1
print("Exponent:", a ** b)       # 1000
                                

➕ Addition (+)

Adds two operands

➖ Subtraction (-)

Subtracts second from first

✖️ Multiplication (*)

Multiplies two operands

➗ Division (/)

Returns float result

🔢 Floor Division (//)

Integer division

🧮 Modulus (%)

Returns remainder

2. Assignment Operators

Assignment operators are used to assign values to variables. The most common is =, but there are also compound assignment operators that combine arithmetic with assignment.

x = 5       # simple assignment
x += 3      # same as x = x + 3 → x = 8
x -= 2      # x = x - 2 → 6
x *= 2      # x = x * 2 → 12
x /= 4      # x = x / 4 → 3.0
x //= 2     # x = x // 2 → 1
x **= 3     # x = x ** 3 → 1
                                

3. Comparison (Relational) Operators

Comparison operators compare two values and return a Boolean (True or False). They are fundamental in decision-making and loops.

a = 10
b = 20

print("Equal:", a == b)         # False
print("Not Equal:", a != b)     # True
print("Greater:", a > b)        # False
print("Less:", a < b)           # True
print("Greater or Equal:", a >= b) # False
print("Less or Equal:", a <= b) # True
                                

4. Logical Operators

Logical operators (and, or, not) are used to combine conditional statements. They return Boolean values.

x = True
y = False

print("x and y:", x and y)  # False
print("x or y:", x or y)    # True
print("not x:", not x)      # False
                                

5. Bitwise Operators

Bitwise operators act on bits and perform operations bit-by-bit. They are useful in low-level programming, cryptography, and performance optimization.

a = 5   # binary: 0101
b = 3   # binary: 0011

print("Bitwise AND:", a & b)  # 1 (0001)
print("Bitwise OR:", a | b)   # 7 (0111)
print("Bitwise XOR:", a ^ b)  # 6 (0110)
print("Bitwise NOT:", ~a)     # -6
print("Left Shift:", a << 1)  # 10 (1010)
print("Right Shift:", a >> 1) # 2 (0010)
                                

6. Special Operators

Python has two special operators: identity (is, is not) and membership (in, not in). They are used to check object identity and membership in sequences.

# Identity operators
a = [1, 2, 3]
b = [1, 2, 3]
c = a

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

# Membership operators
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits)   # True
print("grape" not in fruits) # True
                                

Try It Yourself!

Experiment with operators directly in your browser. Modify the code and see the results in real time.

Loading Pyodide... 0%
Python Code Editor
========================================
ARITHMETIC OPERATORS
========================================
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3.3333333333333335
10 // 3 = 3
10 % 3 = 1
10 ** 3 = 1000

========================================
COMPARISON OPERATORS
========================================
10 == 3: False
10 != 3: True
10 > 3: True

========================================
LOGICAL OPERATORS
========================================
True and False: False
True or False: True
not True: False

========================================
BITWISE OPERATORS
========================================
5 & 3 = 1
5 | 3 = 7
5 ^ 3 = 6

✅ Experiment with different operators!
🏆

🎉 You've Mastered Python Operators!

You understand arithmetic, assignment, comparison, logical, bitwise, and special operators. These are essential tools for every Python programmer.

Quick Quiz – Test Your Knowledge

Let's see what you've learned about Python operators:

1. Which operator is used for exponentiation in Python?
2. What is the result of 5 & 3 in Python?
3. Which operator checks if a value is present in a sequence?

Frequently Asked Questions

🤔 What's the difference between == and is? ▼

== checks value equality, while is checks object identity (whether two references point to the same object).

⚡ What is operator precedence? ▼

Operator precedence determines the order in which operations are evaluated. For example, multiplication has higher precedence than addition. Use parentheses to control evaluation order.

📐 What is floor division? ▼

Floor division (//) returns the largest integer less than or equal to the division result. It's different from true division (/) which returns a float.

📚 Where to Go From Here

Now that you understand Python operators, here are some related topics to explore:

📊 Data Types

Review Python data types

📝 Constants

Learn about constants in Python

📋 Assignment Statements

Understand assignment in Python

📖 Related Tutorials
  • Data Types in Python
  • Tokens in Python
  • Variable and Identifiers
  • Constants in Python
Previous: Data Types Next: Constants →