Open In App

Difference between / vs. // operator in Python

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the difference between the / vs // operator in Python

Python Division Operator

The division operator ‘/ ‘ performs standard division, which can result in a floating-point number. However, if both the dividend and divisor are integers, Python will perform integer division only if the result is an integer. Otherwise, it will produce a floating-point result.

Example

In the above example, we are performing division between two integers, 10 and 3. The result of dividing 10 by 3 is 3.3333333333333335

Python3




# Division operator
 
result = 10 / 3
 
print(result)


Output:

3.3333333333333335

Floor Division in Python

The floor division operator // performs division and returns the largest integer that is less than or equal to the division result. It truncates (rounds down) the fractional part of the result, ensuring that the result is always an integer.

Example

In result1 we are performing floor division between two integers, 10 and 3. The result of dividing 10 by 3 is 3.333…., but floor division returns the largest integer less than or equal to the result. Therefore, the result is 3.

Python3




result1 = 10 // 3  # Result: 3
 
print("Floor division of two integers :", result1)


Output

Floor division of two integers : 3


Difference between ‘/’ and ‘//’ in Python Division

The below table shows the differences with proper examples

Feature

Division Operator (/)

Floor Divsion Operator (//)

Return Type

Floating-point. Returns in integer only if the result is an integer

Integer

Fractional Part

Returns the fractional part

Truncates the fractional part

Examples

  • 10 / 3 = 3.333333…
  • 10 / 5 = 2.0
  • 5 / 2 = 2.5
  • -17 / 5 = -3.4
  • -17 / -5 = 3.4
  • 10 // 3 = 3
  • 10 // 5 = 2
  • 5 // 2 = 2
  • -17 // 5 = -4
  • -17 // -5 = 3


Previous Article
Next Article

Similar Reads

Benefits of Double Division Operator over Single Division Operator in Python
The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division. C/C++ Code # A Python program to demonstrate use of # "//" for both integers and floating points print(5//2) print(-5//2) print(5.0//2) Output: 2 -3 2.0 The time complexity of the program is O(1) as it conta
2 min read
Difference between == and is operator in Python
When comparing objects in Python, the identity operator is frequently used in contexts where the equality operator == should be. In reality, it is almost never a good idea to use it when comparing data. What is == Operator?To compare objects based on their values, Python's equality operators (==) are employed. It calls the left object's __eq__() cl
3 min read
Difference between != and is not operator in Python
In this article, we are going to see != (Not equal) operators. In Python != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Whereas is not operator checks whether id() of two objects is same or not. If same, it returns False and if not same, it returns
3 min read
Python MySQL - BETWEEN and IN Operator
In this article, we are going to see the database operations BETWEEN and IN operators in MySQL using Python. Both of these operators are used with the WHERE query to check if an expression is within a range or in a list of values in SQL. We are going to use the below table to perform various operations: The SQL BETWEEN condition is used to test if
3 min read
Operator Overloading in Python
Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because '+' operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows d
7 min read
Python | Operator.countOf
Operator.countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. Syntax : Operator.countOf(freq = a, value = b) Parameters : freq : It can be list or any other data type which store value value : It is value for which we have to count the number of occurrences Returns: Count of number of occ
2 min read
Python | List comprehension vs * operator
* operator and range() in python 3.x has many uses. One of them is to initialize the list. Code : Initializing 1D-list list in Python # Python code to initialize 1D-list # Initialize using star operator # list of size 5 will be initialized. # star is used outside the list. list1 = [0]*5 # Initialize using list comprehension # list of size 5 will be
2 min read
Check if a value exists in a DataFrame using in & not in operator in Python-Pandas
In this article, Let’s discuss how to check if a given value exists in the dataframe or not.Method 1 : Use in operator to check if an element exists in dataframe. C/C++ Code # import pandas library import pandas as pd # dictionary with list object in values details = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'], 'Age
3 min read
Concatenate two strings using Operator Overloading in Python
Operator Overloading refers to using the same operator to perform different tasks by passing different types of data as arguments. To understand how '+' operator works in two different ways in python let us take the following example C/C++ Code # taking two numbers a = 2 b = 3 # using '+' operator add them c = a+b # printing the result print("
2 min read
Modulo operator (%) in Python
When we see a '%' the first thing that comes to our mind is the "percent" but in computer language, it means modulo operation(%) which returns the remainder of dividing the left-hand operand by right-hand operand or in layman's terms it finds the remainder or signed remainder after the division of one number by another. Given two positive numbers,
3 min read
Article Tags :
Practice Tags :