Internship Assurance

DevOps & Cloud Engineering

Are you new to the Python programming language and wondering how it handles basic math operations? Would you like to learn how addition, subtraction, multiplication and other operations within arithmetics work in Python? The following blog helps you understand how to use arithmetic operators to make all these operations as easy as possible to use.

Now, let us see some of the arithmetic operators in Python that will make your journey of coding much smoother.

Python includes a set of arithmetic operators used to perform mathematical calculations. These operators are essential tools for any programmer. Here’s a quick rundown of the arithmetic operators available in Python:

**Addition (+)**: Adds two numbers.**Subtraction (-)**: Subtracts the second number from the first.**Multiplication (*)**: Multiplies two numbers.**Division (/)**: Divides the first number by the second.**Modulus (%)**: Returns the remainder when the first number is divided by the second.**Exponentiation (**)**: Raises the first number to the power of the second.**Floor Division (//)**: Divides the first number by the second and rounds down to the nearest whole number.

Let’s look at each of these operators in more detail with some unique examples.

Also read: What are Python Functions

**Output:**

**Output:**

**Output:**

**Output:**

The modulus operator returns the remainder when the first number is divided by the second.

**Output:**

The exponentiation operator evaluates the first number raised to the power of the second.

**Output:**

The Floor Division operator divides the first number by the second number, applies the floor division, and returns the nearest whole number result.

**Output:**

Have you ever wondered what happens when we mix different types of numbers in our calculations? Can we add an integer to a float without causing errors?

These are common worries when working with arithmetic operators in Python. Python makes it easy to handle mixed data types in arithmetic operations.

In this case, when addition is performed on two different data types, Python automatically converts the narrower type to the wider type. The process can be illustrated through a simple example:

**Output:**

Here, Python converts this integer to a float and then performs the addition; therefore, guaranteeing the accuracy of the answer and eliminating type errors.

The beauty of Python is that it treats mixed data types smoothly and easily. It is a powerful tool with arithmetic operations. We can juggle integers, floats, and even complex numbers in our calculations. That makes one very flexible while writing code—clean and effective.

Here’s another example involving complex numbers:

**Output:**

Internship Assurance

DevOps & Cloud Engineering

Complex numbers may look difficult, but Python handles them very easily. The arithmetic operators in Python perform these operations as easily as working with real numbers. We can operate with them in various ways by using these operators. Here is a quick demonstration with a more operational example:

**Output:**

Ever tried dividing a number by zero and wondered why your program crashes? Or have you encountered unexpected results when using arithmetic operators in Python?

These are common worries when dealing with edge cases in arithmetic operations. Python provides tools to handle these edge cases effectively.

One of the most common edge cases is division by zero. Python raises a ZeroDivisionError when we try to divide by zero. We can handle this using a try-except block:

**Output:**

What if a user enters a non-numeric value? We can handle such scenarios using exception handling:

**Output:**

Python handles large numbers gracefully, but it’s still good to be cautious.

Here’s an example of handling large numbers:

**Output:**

Negative numbers can also cause unexpected results, especially with division and modulus.

**Output:**

Since, in some cases, we have to perform more than one operation on the variables within a single problem, it is pertinent to understand the order of operation. Python does this based on the rule of precedence and associativity of the operators.

The precedence of operators determines the order in which different operations are evaluated.

Here is the order of precedence for Python arithmetic operators, from highest to lowest:

**Exponentiation (**)****Multiplication (*), Division (/), Floor Division (//), and Modulus (%)****Addition (+) and Subtraction (-)**

Associativity is the order of evaluating an expression being operated on by operators of a similar precedence level.

Python arithmetic operators have the following associativity:

**Left to Right**: *, /, //, %, +, –**Right to Left**: **

Let’s see an example that combines these rules:

Arithmetic operators in Python are the very basic tools we use every day. Mastering them will help us perform a big deal of computations with much ease and accuracy. Be it handling mixed data types, complex numbers, or edge cases, Python’s arithmetic operators make our coding experience smooth and efficient. Keep practising and trying out these operators to continue sharpening your Python skills.

FAQs

What happens when I divide by zero in Python?

In this case, Python will raise ZeroDivisionError. This can be handled through a try-except block.

What are Python's basic arithmetic operators?

These are the fundamental arithmetic operators in Python:

- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exponentiation (**)
- Floor division (//)

How does Python handle arithmetic operations with mixed data types?

Python performs an automatic conversion of the narrow data type to the wider one and carries out the operation; hence, results are always accurate.

Can Python perform arithmetic operations with complex numbers?

Python can perform arithmetic operations with complex numbers just like it performs with real numbers.

Can you explain how the '/' operator is different from '//' in Python?

- ‘/’ operator performs ‘true division’ to yield a float value.
- ‘//’ operator performs ‘floor division’ by giving the end result in integer form, ignoring the fractional part.

Book a free counselling session

Get a personalized career roadmap

Get tailored program recommendations

Explore industry trends and job opportunities

Programs tailored for your success

Popular

Data Science

Technology

Finance

Management

Future Tech

Upskill with expert articles

View all

Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved