raw_input function in Python

It presents a prompt to the user (the optional arg of raw_input([arg])), gets input from the user and returns the data input by the user in a string. See the docs for raw_input(). Example: This differs from input() in that the latter tries to interpret the input given by the user; it is usually best to avoid input() and to stick with raw_input() and custom … Read more

Python division

You’re using Python 2.x, where integer divisions will truncate instead of becoming a floating point number. You should make one of them a float: or from __future__ import division, which the forces / to adopt Python 3.x’s behavior that always returns a float.

What is the difference between range and xrange functions in Python 2.X?

In Python 2.x: range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements. xrange is a sequence object that evaluates lazily. In Python 3: range does the equivalent of Python 2’s xrange. To get the list, you have to explicitly use list(range(…)). xrange no longer exists.

What is the difference between rw+ and r+

On Linux at least, there’s no difference as far as I can tell. Here’s a test script and its corresponding OS syscalls (using strace); tested on python 2.7.9. See http://man7.org/linux/man-pages/man2/open.2.html for more details on the file access and creation flags. It’s not accurate to say a file object opened with ‘r+’ cannot be used to truncate a file – … Read more