What do >> and << mean in Python?

246,253

Solution 1

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

Solution 2

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.

Solution 3

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".

Solution 4

They are bit shift operator which exists in many mainstream programming languages, << is the left shift and >> is the right shift, they can be demonstrated as the following table, assume an integer only take 1 byte in memory.

| operate | bit value | octal value |                       description                        |
| ------- | --------- | ----------- | -------------------------------------------------------- |
|         | 00000100  |           4 |                                                          |
| 4 << 2  | 00010000  |          16 | move all bits to left 2 bits, filled with 0 at the right |
| 16 >> 2 | 00000100  |           4 | move all bits to right 2 bits, filled with 0 at the left |

Solution 5

The other case involving print >>obj, "Hello World" is the "print chevron" syntax for the print statement in Python 2 (removed in Python 3, replaced by the file argument of the print() function). Instead of writing to standard output, the output is passed to the obj.write() method. A typical example would be file objects having a write() method. See the answer to a more recent question: Double greater-than sign in Python.

Share:
246,253

Related videos on Youtube

user3201152
Author by

user3201152

Updated on July 08, 2022

Comments

  • user3201152
    user3201152 almost 2 years

    I notice that I can do things like 2 << 5 to get 64 and 1000 >> 2 to get 250.

    Also I can use >> in print:

    print >>obj, "Hello world"
    

    What is happening here?

    • Ashwini Chaudhary
      Ashwini Chaudhary about 10 years
    • user2357112
      user2357112 about 10 years
      Here, now you can plug those symbols into a search engine and have it actually search for them: symbolhound.com
    • vaultah
      vaultah about 10 years
      possible duplicate of Bitwise Operation and Usage
    • user3201152
      user3201152 about 10 years
      @user2357112 That'll be really helpful, thanks. And to those saying it's a basic question, it may be, but I had no concept of bitwise operators, so I would never have thought to look that up in documentation...
    • Jordan Reiter
      Jordan Reiter almost 8 years
      @joaquin it's now the number one result in google for python greater than greater than.
    • GiriB
      GiriB about 7 years
      In the first case it is Bitwise Shift operators, in the second case it is overloaded for StringIO() objects. Take a look at this print chevron
  • bartekbrak
    bartekbrak over 8 years
    Perhaps an example would help, type a series of these in Python: print bin(1), print bin(1 << 1), print bin(17), print bin(17 >> 1) and so on. You can see how it works without explanations.
  • Qi Fan
    Qi Fan almost 8 years
    Bit shift operator requires 2 operands so why operand is "print" and operand 2 an object? as in print >>obj, "Hello world"
  • z33k
    z33k almost 5 years
    It is an answer to the context the OP provided (and thus known to OP) and not to the question asked.
  • James
    James almost 5 years
    @Qi Fan @z33k The question was edited to add the reference to print more than a year after this answer was written. The original question mentioned only 2 << 5 and 1000 >> 2 ¯_(ツ)_/¯
  • schimmy
    schimmy almost 5 years
    FYI that another common use of the right shift operator you might see in Python code will occur in Airflow files. The Airflow framework overloads the '>>' operator to indicate one task is upstream from the other: stackoverflow.com/questions/52389105/…
  • shaik moeed
    shaik moeed over 4 years
    Exactly searching for this explanation.
  • trudolf
    trudolf about 4 years
    you can actually override these operators via the __rshift__ and __lshift__ methods.
  • lightbox142
    lightbox142 almost 4 years
    I'm getting that 2 >> 5 is equal to 0. I thought it should equal to 0.0001?
  • yosemite_k
    yosemite_k almost 4 years
    @teter123f the operation is binary. it is not decimal operation.
  • Andy
    Andy over 3 years
    No bug, it just isn't limited to 8 bits per number.
  • Nandish
    Nandish over 3 years
    Both << and >> opertors should work with same logic. But << operator is behaving differently. I filed a behavioral bug with Python just jow.
  • Davo
    Davo about 3 years
    @Nandish why... they do both work with the same logic
  • Grismar
    Grismar over 2 years
    The Python 3 update is good, but does not mention that the use of the double chevron for redirection of print is no longer possible in Python 3, as print became a function instead of a statement by itself. Other objects can overload the double chevron by implementing their own __rshift__ and __lshift__ so you may see it used as a redirection of sorts for specific packages. (this makes sense because >> generally indicates output redirection and appending in the OS)