6

I read the following python code:

 a=2**b

I know several languages like c,c++,c#,java... i even googled ** operator without any results.

so what does 2**b means?

4
  • 1
    it's 2^b (but ^ means XOR in Python, so ** is used for the power
    – mykhal
    Commented Oct 17, 2011 at 12:55
  • Fractional and negative numbers as second operator (exponent) work, too! So you can draw arbitrary roots and get reciprocal values! 2**0.5 is 1.414 (=sqrt(2)) and 2**-0.5 is 0.707.
    – cfi
    Commented Oct 17, 2011 at 13:48
  • Same operator works in perl, too. ** is not uncommon these days.
    – cfi
    Commented Oct 17, 2011 at 13:50
  • If you'd ever learned Fortran you'd know it in your sleep.
    – Hot Licks
    Commented Oct 20, 2011 at 0:08

7 Answers 7

12

It is the exponentiation operator. In your example, a will have the result of 2 to the bth power.

Check out the last entry in the table in this section.

10

it's simple ** means power, so 2**b means 2 to the power of b

0
7

It's python's power operator. You can write this as a = pow(2, b)

0
5

In that example ** does represent exponation. but **(and also * ) can be used as unpacking operators. for instance when using a list of variables of unknown length as args for a function. I'm new to programming and python so I have difficulty using this in an example. perhaps one of you more experienced users can demonstrate

5

** can also be used as a function parameter to pass a variable number of keyword arguments to a function. Example:

def x(**kwargs):
    for key, value in kwargs.items():
        print "%s: %s" % (key, value)

x(y=2, z=3, a=1, b=2)

Prints:

y: 2
z: 3
a: 1
b: 2
2

It means 2^b in other languages. Or math.pow(2, 4) if you were using the math module.

See operator documentation here: http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex

1
  • "2^b in other languages" might be ambiguous and probably not what you meant. Many lanugages use ^ as the xor operator.
    – Marlon
    Commented Oct 19, 2011 at 23:49
2

This means to raise 2 to the power b.

See http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex

Not the answer you're looking for? Browse other questions tagged or ask your own question.