Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the /= operator mean in Python?

Tags:

python

syntax

What does the operator /= (slash equals) mean in Python?

I know |= is a set operator. I have not seen /= previously though.

like image 881
ShanZhengYang Avatar asked Sep 09 '15 23:09

ShanZhengYang


People also ask

What does /= in Python mean?

/= Division AssignmentDivides the variable by a value and assigns the result to that variable.

What does /= mean?

The division assignment operator ( /= ) divides a variable by the value of the right operand and assigns the result to the variable.

What does a% mean in Python?

The ^ operator does a binary xor. a ^ b will return a value with only the bits set in a or in b but not both. This one is simple! The % operator is mostly to find the modulus of two integers. a % b returns the remainder after dividing a by b .


2 Answers

It's an assignment operator shorthand for / and =.

Example:

x = 12 
x /= 3 # equivalent to x = x / 3 

If you use help('/='), you can get the full amount of symbols supported by this style of syntax (including but not limited to +=, -=, and *=), which I would strongly encourage.

like image 198
Makoto Avatar answered Sep 28 '22 17:09

Makoto


It's an augmented assignment operator for floating point division. It's equivalent to

x = x / 3

Per Makota's answer above, the following is provided by python 3, including the target types and operators, see https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements for more info:

augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression)

augtarget ::= identifier | attributeref | subscription | slicing

augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|="

like image 38
Jacob Helfman Avatar answered Sep 28 '22 16:09

Jacob Helfman