Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Arithmetic Operators

Python

python logo

In this tutorial we will learn about Arithmetic operators in Python.

Following are the list of Arithmetic operators in Python

Addition

We use the + sign to perform addition in Python.

This operator adds the value of the left side variable with the value of the right side variable.

In the following Python program we are adding the value of two variables.

# variables
x = 10
y = 20

# operation
result = x + y

# output
print("Result:", result)

The above code will give the following result.

Result: 30

Subtraction

We use the - sign to perform subtraction in Python.

This operator subtracts the right side variable value from the left side variable value.

In the following Python program we are subtracting the value of y from x.

# variables
x = 10
y = 20

# operation
result = x - y

# output
print("Result:", result)

The above code will give the following result.

Result: -10

Multiplication

We use the * sign to perform multiplication in Python.

This operator multiplies the value of the left side variable with the value of the right side variable.

In the following Python program we are multiplying the value of x and y.

# variables
x = 10
y = 20

# operation
result = x * y

# output
print("Result:", result)

The above code will give the following result.

Result: 200

Division

We use the / sign to perform division in Python.

This operator divides the left side variable value by the right side variable value.

In the following Python program we are dividing the value of x by y.

# variables
x = 10
y = 20

# operation
result = x / y

# output
print("Result:", result)

The above code will give the following result.

Result: 0.5

Modulus

We use the % sign to perform modulus in Python.

This operator divides the value of the left side variable by the value of the right side variable and gives us the remainder value.

In the following Python program we are finding the remainder on dividing the value of x by y.

# variables
x = 101
y = 20

# operation
result = x % y

# output
print("Result:", result)

The above code will give the following result.

Result: 1

Exponent

We use the ** sign to perform exponential operation in Python.

This operator raises the left side variable value to the power of right side variable value.

In the following Python program we are raising x to the power of y i.e. xy.

# variables
x = 2
y = 5

# operation
result = x ** y

# output
print("Result:", result)

The above code will give the following result.

Result: 32

Floor Division

We use the // sign to perform floor division in Python.

Points to note!

  • When two positive numbers are involved then it returns the quotient after removing the decimal parts.
  • When one of the operand is a negative number then it floors the result towards negative infinity.

In the following Python program we are performing floor division. We have 10 and 3 and we will get 3 as the result of 10//3.

We get 3.33333... on dividing 10 by 3.
Floor(3.333...) = 3

# variables
x = 10
y = 3

# operation
result = x // y

# output
print("Result:", result)

The above code will give the following result.

Result: 3

In the following Python program we are performing floor division using two negative numbers. We have -10 and -3 and we will get 3 as the result of -10//-3.

We get 3.33333... on dividing -10 by -3.
Floor(3.333...) = 3

# variables
x = -10
y = -3

# operation
result = x // y

# output
print("Result:", result)

The above code will give the following result.

Result: 3

In the following Python program we are performing floor division using a positive and a negative number. We have 10 and -4 and we will get -3 as the result of 10//-4.

We get -2.5 on dividing 10 by -4.
Floor(-2.5) = -3

# variables
x = 10
y = -4

# operation
result = x // y

# output
print("Result:", result)

The above code will give the following result.

Result: -3

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT