57

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

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

2
  • This operator can be useful when dealing with paths: Commented Sep 8, 2022 at 12:21
  • @FoxyFox How could it be used in paths? Commented Oct 13, 2022 at 16:49

2 Answers 2

100

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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 ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|="

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.