Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python Arithmetic Operators

by suresh

Python Arithmetic operators include operators like Addition, Subtraction, Multiplication, Division, Floor Division, Exponent (or Power), and Modulus. All these Arithmetic operators in Python are binary operators, which means they operate on two operands. The below table shows all the Python Arithmetic Operators with examples.

Python Arithmetic Operators OperationExample
+Addition10 + 2 = 12
–Subtraction10 – 2 = 8
*Multiplication10 * 2 = 20
/Division10 / 2 = 5.0
%Modulus – It returns the remainder after the division10 % 2 = 0 (Here remainder is zero). If it is 10 % 3, then it will be 1.
**Exponent – It returns the Power of One variable against the other10 ** 2 = 100 (It means 10 Power 2 or 10²).
//Floor Division – Same as Division but it will return the integer value by flooring the extra decimals17 // 3 = 5. If you divide 17 by 3, you get 5.667 and the floor division operator will trim the decimal values and outputs the integer

Python Arithmetic Operators Example

For this Python Arithmetic operators example, We are using two variables a and b and their values are 12 and 3. We are going to use these two variables to perform various arithmetic operations in Python programming

>>> a = 12
>>> b = 3
>>> addition = a + b
>>> subtraction = a - b
>>> multiplication = a * b
>>> division = a / b
>>> modulus = a % b
>>> exponent = a ** b
>>> Floor_Division = a // b
>>> print("Addition of two numbers 12 and 3 is : ", addition)
>>> print("Subtracting Number 3 from 12 is : ", subtraction)
>>> print("Multiplication of two numbers 12 and 3 is : ", multiplication)
>>> print("Division of two numbers 12 and 3 is : ", division)
>>> print("Modulus of two numbers 12 and 3 is : ", modulus)
>>> print("Exponent of two numbers 12 and 3 is : ", exponent)
>>> print("Floor Division of two numbers 12 and 3 is : ", Floor_Division)
Python Arithmetic Operators 1

The following Python statement will find the exponent. It means 12 power 3 = 12 * 12 * 12 = 1728

exponent = a ** b

When we are using division ( / ) operator the result will be float or decimal value. If you want to display the output as integer value by rounding the value then use Python Floor Division ( // ) operator

Python Arithmetic Operators on Strings

In this Python Arithmetic operators example, We are using two variables a and b of string data type. Next, we use these two variables to show the problems we generally face while performing arithmetic operations on String Data type.

>>> a = "Tutorial"
>>> b = "Gateway"
>>> print(a + b)
>>> print(a + " " + b)
>>> print(a * 3)
>>> print((a + " ")* 3)
>>> print(a + 3)
>>> print(a + str(3))

TIP: Please be careful with brackets, if you misplace it then you will end up with wrong results

Python Arithmetic Operators 2

In this program for Arithmetic operators in python, First, We declared two variables a and b of type string and their values are “Tutorial” and “Gateway”

a = "Tutorial"
b = "Gateway"

The following print statement will display the output as “TutorialGateway” because when we use ‘+’ operator in between string variables python will concat the two strings

print(a+b)

It will insert the empty space between the two words”Tutorial” and “Gateway”. It means the output will be “Tutorial Gateway”

print(a + " " + b)

Displays Tutorial three times

print(a * 3)

Insert space in between each iteration

print((a + " ")* 3)

It is the interesting part because we are using the ‘+’ operator between string and integer.

print(a + 3)

The above statement will throw an error because python will not convert integer object to string object implicitly. To resolve this, we have to convert explicitly.

print(a + str(3))

TIP: Be careful while Concating the String and Integers (Type casting plays a major role here).

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy