# syntax – What do >> and << mean in Python?

## syntax – What do >> and << mean in Python?

I think it is important question and it is not answered yet (the OP seems to already know about shift operators). Let me try to answer, the >> operator in your example is used for two different purposes. In c++ terms this operator is overloaded. In the first example it is used as bitwise operator (left shift), while in the second scenario it is merely used as output redirection. i.e.

``````2 << 5 # shift to left by 5 bits
2 >> 5 # shift to right by 5 bits
print >> obj, Hello world # redirect the output to obj,
``````

# example

``````with open(foo.txt, w) as obj:
print >> obj, Hello world # hello world now saved in foo.txt
``````

# update:

In python 3 it is possible to give the file argument directly as follows:

``````print(Hello world, file=open(foo.txt, a)) # hello world now saved in foo.txt
``````

These are bitwise shift operators.

Quoting from the docs:

``````x << y
``````

Returns `x` with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying `x` by `2**y`.

``````x >> y
``````

Returns `x` with the bits shifted to the right by y places. This is the same as dividing `x` by `2**y`.

#### syntax – What do >> and << mean in Python?

12 << 2

48

Actual binary value of 12 is 00 1100 when we execute the above statement Left shift ( 2 places shifted left) returns the value 48 its binary value is 11 0000.

48 >> 2

12

The binary value of 48 is 11 0000, after executing above statement Right shift ( 2 places shifted right) returns the value 12 its binary value is 00 1100.