Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 operator.div?

I have a slight problem with randomly generated operators in Python 3.

import operator
hardOperators = [operator.add, operator.sub, operator.mul]
random_hardOperator = random.choice(hardOperators)

So you see, I have the operator functions add, sub and mul. But when I try and add the div operator I get an alert saying that operator has no attribute 'div'.

I've never actually tried using random operators in Python before, so this might sound a foolish question, but it's one which is giving me hell in debugging.

like image 474
TomWarren Avatar asked May 16 '16 16:05

TomWarren


People also ask

What is Div in Python?

div() is used to find the floating division of the dataframe and other element-wise. This function is similar to dataframe/other, but with an additional support to handle missing value in one of the input data. Syntax: DataFrame.div(other, axis='columns', level=None, fill_value=None)

How do you divide in Python 3?

Floor Division and True Division In Python 3. x, slash operator ("/") does true division for all types including integers, and therefore, e.g. 3/2==1.5. The result is of a floating-point type even if both inputs are integers: 4 / 2 yields 2.0.

What are the 3 operators in Python?

Python divides the operators in the following groups: Arithmetic operators. Assignment operators. Comparison operators.

What is floor division in Python 3?

Floor division is an operation in Python that divides two numbers and rounds the result down to the nearest integer. The floor division happens via the double-backslash (//) operator. r = a // b.

Is there an integer division operator in Python?

Fortunately, Python has us covered with the integer division operator. Many popular languages such as JavaScript, Java, and PHP use the double forward slash // as a comment operator. However, in Python this simple operator is in fact used for integer division. There isn’t too much to say, so let’s take a look at an example.

What is the + operator in Python?

Python Operators Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values:

What are the different types of Division in Python?

In this tutorial of Python Examples, we learned how to perform two types of Python Division namely: Integer Division and Float Division.

How to perform float Division in Python?

Float division means, the division operation happens until the capacity of a float number. That is to say result contains decimal part. To perform float division in Python, you can use / operator. Division operator / accepts two arguments and performs float division.


1 Answers

There is no operator.div in Python 3, no; that only existed in Python 2.

There is a operator.truediv() function instead, as well as a operator.floordiv() function. The reason for this division (no pun intended) is that the old Python 2 / operator would return an integer if both operands are integers, a float otherwise, while these two functions always return the same type, regardless of the type of inputs.

like image 78
Martijn Pieters Avatar answered Oct 01 '22 13:10

Martijn Pieters