input() error – NameError: name ‘…’ is not defined

TL;DR input function in Python 2.7, evaluates whatever your enter, as a Python expression. If you simply want to read strings, then use raw_input function in Python 2.7, which will not evaluate the read strings. If you are using Python 3.x, raw_input has been renamed to input. Quoting the Python 3.0 release notes, raw_input() was renamed to input(). That is, the new input() function reads a … Read more

How exactly does a generator comprehension work?

Do you understand list comprehensions? If so, a generator expression is like a list comprehension, but instead of finding all the items you’re interested and packing them into list, it waits, and yields each item out of the expression, one by one. Because a generator expression only has to yield one item at a time, … Read more

In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string:

The problem isn’t your use of not, it’s that or doesn’t mean what you think it does (and if you think it through, it couldn’t): You’re asking whether the expression (“StatusRequest” or “StatusResponse”) appears in line. But that expression is just the same thing as “StatusRequest”. Put it in English: you’re not trying to say “if neither of these is in line”. Python … Read more

Indent Expected?

There are, in fact, multiple things you need to know about indentation in Python: Python really cares about indention. In other languages, indention is not necessary but only serves to improve the readability. In Python, indentation is necessary and replaces the keywords begin / end or { } of other languages. This is verified before the execution of the … Read more

Filtering a NumPy Array

Summary The following tests are meant to give some insights into the different approaches and should be taken with a grain of salt. What is being tested here is not exactly generic filtering, but rather just applying a threshold, which has the notable feature that computing the condition is pretty fast. Very different results would … Read more

How do operator.itemgetter() and sort() work?

Looks like you’re a little bit confused about all that stuff. operator is a built-in module providing a set of convenient operators. In two words operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it. So, you can’t use key=a[x][1] there, because python has no idea what x is. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)