Check string “None” or “not” in Python 2.7

For empty strings both are different: will not print anything because it is empty and therefore considered False but: will print because foo is not None! I Changed it according to PEP8. if foo is not None is equivalent to your if not foo is None but more readable and therefore recommended by PEP8. A bit more about the general principles in Python: The if will only be True if a … Read more

How do I install the yaml package for Python?

You could try the search feature in pip, which looks for packages in PyPI with yaml in the short description. That reveals various packages, including PyYaml, yamltools, and PySyck, among others (Note that PySyck docs recommend using PyYaml, since syck is out of date). Now you know a specific package name, you can install it: If you … Read more

How to create a new text file using Python

Looks like you forgot the mode parameter when calling open, try w: The default value is r and will fail if the file does not exist Other interesting options are See Doc for Python2.7 or Python3.6 — EDIT — As stated by chepner in the comment below, it is better practice to do it with a withstatement (it guarantees that the file will be closed)

Remove list from list in Python

Possible Duplicate:Get difference from two lists in Python What is a simplified way of doing this? I have been trying on my own, and I can’t figure it out. list a and list b, the new list should have items that are only in list a. So: I tried writing code, but every time it … Read more