What do >> and << mean in Python?
What sorts of lesser known operators and statements are there?
PEP 584 -- Add + and - operators to the built-in dict class.
How To Do Just About Anything With Python Lists
What im missing is when not to use lists, for example sets are a better choice sometimes
More on reddit.comWhat are comparison operators in Python?
Name the two membership operators in Python?
Can we use identity operators in Python with all data types?
Videos
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 a bitwise operator (right shift),
2 << 5 # shift left by 5 bits
# 0b10 -> 0b1000000
1000 >> 2 # shift right by 2 bits
# 0b1111101000 -> 0b11111010
While in the second scenario it is used for output redirection. You use it with file objects, like this example:
with open('foo.txt', 'w') as f:
print >>f, 'Hello world' # "Hello world" now saved in foo.txt
This second use of >> only worked on Python 2. On Python 3 it is possible to redirect the output of print() using the file= argument:
with open('foo.txt', 'w') as f:
print('Hello world', file=f) # "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.