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 Arithmetic Operators: Complete Guide

Python Arithmetic Operators - Complete Guide

Arithmetic operators are the foundation of mathematical operations in Python.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers, data engineers and data scientists, our mission is to make programming easy to understand through practical examples, real-world experience and clear ations.
šŸ“‘ On this page:
  • What are Arithmetic Operators?
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Exponentiation (**)
  • Floor Division (//)
  • Complete Example
  • Try It Yourself
  • Quick Quiz
  • FAQ
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
šŸ“š What You'll Learn Here
  • What arithmetic operators are — symbols that perform math operations
  • Addition (+) — adding two operands
  • Subtraction (-) — subtracting one operand from another
  • Multiplication (*) — multiplying two operands
  • Division (/) — dividing one operand by another
  • Modulus (%) — finding the remainder
  • Exponentiation (**) — raising to a power
  • Floor Division (//) — integer division
  • Hands-on practice with the interactive editor

What are Arithmetic Operators?

In Python programming, arithmetic operators are symbols used to perform mathematical operations on operands. They work on numerical values (integers, floats, and complex numbers) and are fundamental to any Python program.

Before diving into arithmetic operators, let's understand some key terms:

  • Operator: A symbol that performs an action on operands (e.g., +, -, *, /)
  • Operand: A variable or constant on which the operation is performed
  • Expression: A combination of operands and operators that computes a value

Python provides 7 arithmetic operators that work on two operands (binary operators).

āž• Addition (+)

Adds two operands

āž– Subtraction (-)

Subtracts second from first

āœ–ļø Multiplication (*)

Multiplies two operands

āž— Division (/)

Returns float result

🧮 Modulus (%)

Returns remainder

šŸ”¢ Exponentiation (**)

Raises to a power

šŸ“ Floor Division (//)

Integer division

1. Addition (+)

The addition operator (+) is used to add two operands together. It works with integers, floats, and even strings (concatenation).

# Addition examples
x = 10
y = 5
z = x + y
print("x + y =", z)  # Output: x + y = 15

# Addition with floats
a = 3.5
b = 2.5
print("a + b =", a + b)  # Output: a + b = 6.0

# String concatenation
first = "Hello"
second = "World"
print(first + " " + second)  # Output: Hello World

2. Subtraction (-)

The subtraction operator (-) subtracts the right operand from the left operand.

# Subtraction examples
x = 10
y = 5
z = x - y
print("x - y =", z)  # Output: x - y = 5

# Subtraction with floats
a = 7.5
b = 3.2
print("a - b =", a - b)  # Output: a - b = 4.3

3. Multiplication (*)

The multiplication operator (*) multiplies two operands together.

# Multiplication examples
x = 9
y = 4
z = x * y
print("x * y =", z)  # Output: x * y = 36

# Multiplication with floats
a = 2.5
b = 4.0
print("a * b =", a * b)  # Output: a * b = 10.0

4. Division (/)

The division operator (/) divides the left operand by the right operand and returns a float result.

# Division examples
x = 9
y = 4
z = x / y
print("x / y =", z)  # Output: x / y = 2.25

# Division that results in integer
a = 10
b = 2
print("a / b =", a / b)  # Output: a / b = 5.0

5. Modulus (%)

The modulus operator (%) returns the remainder of the division of the left operand by the right operand.

# Modulus examples
x = 9
y = 4
z = x % y
print("x % y =", z)  # Output: x % y = 1

# Modulus with different numbers
a = 10
b = 3
print("a % b =", a % b)  # Output: a % b = 1

6. Exponentiation (**)

The exponentiation operator (**) raises the left operand to the power of the right operand.

# Exponentiation examples
x = 9
y = 4
z = x ** y
print("x ** y =", z)  # Output: x ** y = 6561

# Square of a number
a = 5
print("a ** 2 =", a ** 2)  # Output: a ** 2 = 25

# Cube of a number
b = 3
print("b ** 3 =", b ** 3)  # Output: b ** 3 = 27

7. Floor Division (//)

The floor division operator (//) divides the left operand by the right operand and returns the largest integer less than or equal to the result (rounds down).

# Floor division examples
x = 9
y = 4
z = x // y
print("x // y =", z)  # Output: x // y = 2

# Floor division with negative numbers
a = -9
b = 4
print("a // b =", a // b)  # Output: a // b = -3

# Positive numbers
c = 10
d = 3
print("c // d =", c // d)  # Output: c // d = 3

Complete Example with All Operators

Let's see all arithmetic operators in action with a complete program.

# Working of arithmetic operators in Python
x = 9
y = 4

z = x + y
print("x + y =", z)      # Output: x + y = 13

z = x - y
print("x - y =", z)      # Output: x - y = 5

z = x * y
print("x * y =", z)      # Output: x * y = 36

z = x / y
print("x / y =", z)      # Output: x / y = 2.25

z = x % y
print("Remainder when", x, "divided by", y, "=", z)  
# Output: Remainder when 9 divided by 4 = 1

z = x // y
print("x // y =", z)     # Output: x // y = 2

z = x ** y
print("x ** y =", z)     # Output: x ** y = 6561

# Output:
# x + y = 13
# x - y = 5
# x * y = 36
# x / y = 2.25
# Remainder when 9 divided by 4 = 1
# x // y = 2
# x ** y = 6561

šŸ’” Program Explanation: The arithmetic operators +, -, *, /, %, //, and ** compute addition, subtraction, multiplication, division, remainder, floor division, and exponentiation respectively. In our program, x=9 and y=4. The results are as shown above.

Try It Yourself!

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

Loading Pyodide... 0%
Python Code Editor
========================================
ARITHMETIC OPERATORS
========================================
x = 15, y = 4

1. ADDITION (+)
x + y = 19

2. SUBTRACTION (-)
x - y = 11

3. MULTIPLICATION (*)
x * y = 60

4. DIVISION (/)
x / y = 3.75

5. MODULUS (%)
x % y = 3

6. FLOOR DIVISION (//)
x // y = 3

7. EXPONENTIATION (**)
x ** y = 50625

āœ… Arithmetic operators are essential for math operations!
šŸ†

šŸŽ‰ You've Mastered Python Arithmetic Operators!

You understand addition, subtraction, multiplication, division, modulus, floor division, and exponentiation. These are essential tools for any Python programmer.

Quick Quiz – Test Your Knowledge

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

1. What is the result of 9 // 4 in Python?
2. Which operator returns the remainder of a division?
3. What is the result of 2 ** 3 in Python?

Frequently Asked Questions

šŸ¤” What's the difference between / and //? ā–¼

/ (division) returns a float result, while // (floor division) returns the largest integer less than or equal to the result. For example, 9/4 = 2.25, but 9//4 = 2.

šŸ”§ What is the modulus operator used for? ā–¼

The modulus operator % returns the remainder of a division. It's commonly used to check if a number is even (num % 2 == 0) or for cycling through ranges.

šŸ“ Does ** work with negative exponents? ā–¼

Yes! 2 ** -2 returns 0.25. Python handles negative exponents by returning a float result.

šŸ“Š What is operator precedence? ā–¼

Operator precedence determines the order of operations. In Python, ** has the highest precedence, followed by *, /, //, %, and then + and -. Use parentheses to control evaluation order.

šŸ“š Where to Go From Here

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

šŸ“ Assignment Operators

Learn about assignment operators

šŸ“Š Relational Operators

Learn about comparison operators

🧠 Logical Operators

Learn about logical operators

šŸ“– Related Tutorials
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
šŸ“– Interview & FAQ Resources
  • SQL - Displaying Data from Multiple Tables FAQ
  • SQL Window Functions & Analytic Functions FAQ
  • Python Data Types Interview Questions
  • Python Tuples and Sets Interview Questions
  • Java Basic Input/Output Interview Questions
  • Java Advanced Threads and Concurrency Interview Questions
Previous: Operators and Expressions Next: Assignment Operators →