**: exponentiation^: exclusive-or (bitwise)%: modulus//: divide with integral result (discard remainder)
**: exponentiation^: exclusive-or (bitwise)%: modulus//: divide with integral result (discard remainder)
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 ** bisaraised to thebpower. 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 ^ bwill return a value with only the bits set inaor inbbut not both. This one is simple! - The
%operator is mostly to find the modulus of two integers.a % breturns the remainder after dividingabyb. Unlike the modulus operators in some other programming languages (such as C), in Python a modulus it will have the same sign asb, rather than the same sign asa. The same operator is also used for the "old" style of string formatting, soa % bcan return a string ifais a format string andbis a value (or tuple of values) which can be inserted intoa. - 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 thata == (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 puttingfrom __future__ import divisionat the top of your files. I strongly recommend it!)
Videos
What is Python and the Uses of Python?
Why do We Use an Operator?
What are the Other Resources and Offers Provided by The Knowledge Academy?
In Python, += is sugar coating for the __iadd__ special method, or __add__ or __radd__ if __iadd__ isn't present. The __iadd__ method of a class can do anything it wants. The list object implements it and uses it to iterate over an iterable object appending each element to itself in the same way that the list's extend method does.
Here's a simple custom class that implements the __iadd__ special method. You initialize the object with an int, then can use the += operator to add a number. I've added a print statement in __iadd__ to show that it gets called. Also, __iadd__ is expected to return an object, so I returned the addition of itself plus the other number which makes sense in this case.
>>> class Adder(object):
def __init__(self, num=0):
self.num = num
def __iadd__(self, other):
print 'in __iadd__', other
self.num = self.num + other
return self.num
>>> a = Adder(2)
>>> a += 3
in __iadd__ 3
>>> a
5
+= adds another value with the variable's value and assigns the new value to the variable.
>>> x = 3
>>> x += 2
>>> print x
5
-=, *=, /= does similar for subtraction, multiplication and division.
It's not an operator as such, so it doesn't really have a name, but it is defined as a "syntactic rule". So it should be called:
- "the keyword argument unpacking syntax"
If you have a list of arguments, *args, it's called "argument unpacking", in the same manner **kwargs is called "keyword argument unpacking".
If you use it on the left hand side of an =, as in a, *middle, end = my_tuple, you'd say "tuple unpacking".
In total, there are three types of (single parameter) arguments:
def f(x) # x: positional argument
def f(x, y=0) # y: keyword argument
def f(x, *xs, y=0) # y: keyword-only argument
The *args argument is called the "variable positional parameter" and **kwargs is the "variable keyword parameter".
Keyword-only arguments can't be given positionally, because a variable positional parameter will take all of the arguments you pass.
Most of this can be found in PEPs 0362 and 3102, as well as in the Control Flow section of the docs. It should be noted though that the function signature object PEP is only a draft, and the terminology might just be one person's idea. But they are good terms anyway. :)
So the * and ** arguments just unpack their respective data structures:
args = (1, 2, 3) # usually a tuple, always an iterable[1]
f(*args) → f(1, 2, 3)
# and
kwargs = {"a": 1, "b": 2, "c": 3} # usually a dict, always a mapping*
f(**kwargs) -> f(a=1, b=2, c=3)
[1]: Iterables are objects that implement the __iter__() method and mappings are objects that implement keys() and __getitem__(). Any object that supports this protocol will be understood by the constructors tuple() and dict(), so they can be used for unpacking arguments.
I don't think it has a name. In the Python Docs under "Unpacking Argument Lists", it's just referred to as "the **-operator."
I'm not sure what you mean by "the other" data structure. When you do f(**kwargs) you unpack the dictionary kwargs as a sequence of key-value pairs. I don't see that there's another structure involved.
I'll copy the example in the above documentation for clarity.
>>> def parrot(voltage, state='a stiff', action='voom'):
... print "-- This parrot wouldn't", action,
... print "if you put", voltage, "volts through it.",
... print "E's", state, "!"
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
See also: What does *args and **kwargs mean?