galley

galley

Credit: 50
Favorite: 0
Register at: 2 months ago
galley Questions of answers
galley 2 months ago Answer question How do I convert a number to a string?

To convert, e.g., the number 144 to the string '144', use the built-in function str().

If you want a hexadecimal or octal representation, use the built-in functions hex() or oct().

For fancy formatting, use the % operator on strings, e.g. "%04d" % 144 yields '0144' and "%.3f" % (1/3.0) yields '0.333'.

galley 2 months ago Answer question What is the difference between range and xrange?
  • xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.
  • For the most part, xrange and range are the exact same in terms of functionality. They both provide a way to generate a list of integers for you to use, however you please.
  • The only difference is that range returns a Python list object and x range returns an xrange object. This means that xrange doesn't actually generate a static list at run-time like range does. It creates the values as you need them with a special technique called yielding. This technique is used with a type of object known as generators. That means that if you have a really gigantic range you'd like to generate a list for, say one billion, xrange is the function to use.
  • This is especially true if you have a really memory sensitive system such as a cell phone that you are working with, as range will use as much memory as it can to create your array of integers, which can result in a Memory Error and crash your program. It's a memory hungry beast.
galley 2 months ago Answer question What is the difference between range and xrange?
col1 col2 col3
1 2 3
4 5 6
galley 2 months ago Answer question What are the key differences between Python 2 and 3?
Division operator
`print` function  
Unicode 
xrange  
Error Handling  
`_future_` module

Although Python 2 is formally considered legacy at this point,its use is still widespread enough that is important for a developer to recognize the differences between Python 2 and 3.

  • Here are some of the key differences that a developer should be aware of:
    • Text and Data instead of Unicode and 8-bit strings. Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. The biggest ramification of this is that any attempt to mix text and data in Python 3.0 raises a TypeError (to combine the two safely, you must decode bytes or encode Unicode, but you need to know the proper encoding, e.g. UTF-8)
    • This addresses a longstanding pitfall for naïve Python programmers. In Python 2, mixing Unicode and 8-bit data would work if the string happened to contain only 7-bit (ASCII) bytes, but you would get UnicodeDecodeError if it contained non-ASCII values. Moreover, the exception would happen at the combination point, not at the point at which the non-ASCII characters were put into the str object. This behavior was a common source of confusion and consternation for neophyte Python programmers.
    • print function. The print statement has been replaced with a print() function
    • xrange – buh-bye. xrange() no longer exists (range() now behaves like xrange() used to behave, except it works with values of arbitrary size)
  • API changes:
    • zip(), map() and filter() all now return iterators instead of lists.
    • dict.keys(), dict.items() and dict.values() now return 'views' instead of lists.
    • dict.iterkeys(), dict.iteritems() and dict.itervalues() are no longer supported.
    • Comparison operators. The ordering comparison operators (<, <=, >=, >) now raise a TypeError exception when the operands don't have a meaningful natural ordering. Some examples of the ramifications of this include:
    • Expressions like 1 < '', 0 > None or len <= len are no longer valid
    • None < None now raises a TypeError instead of returning False
    • Sorting a heterogeneous list no longer makes sense.
    • All the elements must be comparable to each other