79

Other than the standard +, -, *and / operators; but what does these mean (** , ^ , %, //) ?

>>> 9+float(2) # addition
11.0
>>> 9-float(2) # subtraction
7.0
>>> 9*float(2) # multiplication
18.0
>>> 9/float(2) # division
4.5
>>>
>>> 9**float(2) # This looks like a square, (i.e. power 2) 
81.0
>>> 9**float(3) # So ** is equivalent to `math.pow(x,p)` ?
729.0

How about the ^ operator?

>>> 9^int(2) # What is `^` in `x^u` , it only allows `int` for `u`
11
>>> 9^int(3)
10
>>> 9^int(4)
13
>>> 9^int(5)
12
>>> 9^int(6)
15
>>> 9^int(7)
14
>>> 9^int(8)
1
>>> 9^int(9)
0
>>> 9^int(10)
3
>>> 9^int(11)
2
>>> 9^int(12)
5

% in x%m returns a normal remainder modulus, but only if m < x, why is that so? What does % do?

>>> 9%float(2)
1.0
>>> 9%float(3)
0.0
>>> 9%float(4)
1.0
>>> 9%float(5)
4.0
>>> 9%float(6)
3.0
>>> 9%float(7)
2.0
>>> 9%float(8)
1.0
>>> 9%float(9)
0.0
>>> 9%float(10)
9.0
>>> 9%float(11)
9.0
>>> 9%float(12)
9.0

How about the // operator? what does it do?

>>> 9//float(2)
4.0
>>> 9//float(3)
3.0
>>> 9//float(4)
2.0
>>> 9//float(5)
1.0
>>> 9//float(6)
1.0
>>> 9//float(7)
1.0
>>> 9//float(8)
1.0
>>> 9//float(9)
1.0
>>> 9//float(1)
9.0
>>> 9//float(0.5)
18.0
cs95
  • 379,657
  • 97
  • 704
  • 746
alvas
  • 115,346
  • 109
  • 446
  • 738
  • 41
    I feel like you have answered your own question. – squiguy Mar 04 '13 at 03:02
  • 3
    If you Google "python operators" the very first link is a good list of the operators. – Hot Licks Mar 04 '13 at 03:14
  • If you have a specific question regarding the documentation you linked, quote the bit you don't understand and ask about that. – phant0m Mar 04 '13 at 10:27
  • All of these are Arithmetic (**,%,//) and Bitwise(^) operators - [Python operators are discussed in details here](https://www.codingeek.com/tutorials/python/operators/) – Hitesh Garg Feb 26 '21 at 09:39
  • See also https://stackoverflow.com/questions/22832615/what-do-and-mean-in-python =) – alvas Apr 23 '21 at 18:08
  • 1
    "It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened" – Yet it has 64 upvotes and bookmarked 32 times (as of writing this). 1 answer has 122 upvotes, another 10, and the last has 36. Still the question is closed. I came here with a question about "//" and found an answer. – zelfde Oct 21 '21 at 16:24

3 Answers3

140
  • **: exponentiation
  • ^: exclusive-or (bitwise)
  • %: modulus
  • //: divide with integral result (discard remainder)
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
37

You can find all of those operators in the Python language reference, though you'll have to scroll around a bit to find them all. As other answers have said:

  • The ** operator does exponentiation. a ** b is a raised to the b power. The same ** symbol is also used in function argument and calling notations, with a different meaning (passing and receiving arbitrary keyword arguments).
  • 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. Unlike the modulus operators in some other programming languages (such as C), in Python a modulus it will have the same sign as b, rather than the same sign as a. The same operator is also used for the "old" style of string formatting, so a % b can return a string if a is a format string and b is a value (or tuple of values) which can be inserted into a.
  • The // operator does Python's version of integer division. Python's integer division is not exactly the same as the integer division offered by some other languages (like C), since it rounds towards negative infinity, rather than towards zero. Together with the modulus operator, you can say that a == (a // b)*b + (a % b). In Python 2, floor division is the default behavior when you divide two integers (using the normal division operator /). Since this can be unexpected (especially when you're not picky about what types of numbers you get as arguments to a function), Python 3 has changed to make "true" (floating point) division the norm for division that would be rounded off otherwise, and it will do "floor" division only when explicitly requested. (You can also get the new behavior in Python 2 by putting from __future__ import division at the top of your files. I strongly recommend it!)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
10

You are correct that ** is the power function.

^ is bitwise XOR.

% is indeed the modulus operation, but note that for positive numbers, x % m = x whenever m > x. This follows from the definition of modulus. (Additionally, Python specifies x % m to have the sign of m.)

// is a division operation that returns an integer by discarding the remainder. This is the standard form of division using the / in most programming languages. However, Python 3 changed the behavior of / to perform floating-point division even if the arguments are integers. The // operator was introduced in Python 2.6 and Python 3 to provide an integer-division operator that would behave consistently between Python 2 and Python 3. This means:

| context                                | `/` behavior   | `//` behavior |
---------------------------------------------------------------------------
| floating-point arguments, Python 2 & 3 | float division | int divison   |
---------------------------------------------------------------------------
| integer arguments, python 2            | int division   | int division  |
---------------------------------------------------------------------------
| integer arguments, python 3            | float division | int division  |

For more details, see this question: Division in Python 2.7. and 3.3

Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
  • 1
    It may be worth noting that `x % m` always gives a result with the sign of `m`. – hyper-neutrino Nov 14 '17 at 22:52
  • @HyperNeutrino Thanks. I was originally only interested in explaining the specific situation the OP was asking about, but since this answer has grown into something slightly more general, I've added a note to that effect. – Kyle Strand Nov 14 '17 at 23:42
  • @HyperNeutrino ...in your comment, "x % m always gives a result with the sign of m," please elaborate on "the sign of m." What exactly does that look like? I'm having difficulty understanding it. Thank you! – Jeremy Jan 09 '21 at 01:25
  • @Jeremy If `m` is positive, the answer will be positive. For example, `5 % 3` is `2` but `-5 % 3` is `1`, not `-2`. Similar thing for `m` being negative. This works differently from certain languages like I believe, Java. – hyper-neutrino Jan 09 '21 at 19:13
  • @HyperNeutrino ...oooh! The "sign" positive (+) or negative (-). Thank you! – Jeremy Jan 09 '21 at 21:38