Why is parenthesis in print voluntary in Python 2.7?

In Python 2.x print is actually a special statement and not a function*. This is also why it can’t be used like: lambda x: print x Note that (expr) does not create a Tuple (it results in expr), but , does. This likely results in the confusion between print (x) and print (x, y) in Python 2.7 However, since print is a special syntax statement/grammar construct in Python 2.x then, without … Read more

How to XOR two strings in Python

Hi, The following function is returning the result of hex() which returns a string. You should use the ^ operator on integers. I’m not sure though that’s the result you’re looking for. If you want to XOR two strings, it means you want to XOR each character of one string with the character of the other string. You should then … Read more

How to have a set of sets in Python?

Use frozenset, To represent a set of sets, the inner sets must be frozenset objects for the reason that the elements of a set must be hashable (all of Python’s immutable built-in objects are hashable). frozenset is immutable and set is mutable.

How to detect key presses?

I am making a stopwatch type program in Python and I would like to know how to detect if a key is pressed (such as p for pause and s for stop), and I would not like it to be something like raw_input, which waits for the user’s input before continuing execution. Anyone know how … Read more