Type conversion functions

Revision as of 23:33, 10 March 2011 by Smitschu (talk | contribs)

Type conversion functions are used in programming to convert data to primitive datatypes.

Type Conversion in Python

  • str(x) returns a string version of x. Almost all datatypes support this function, and you can use it on your own classes by implementing the __str__ function.
  • int(x) converts x into an integer. Floats will be rounded down to the nearest integer, and strings that don't represent a valid integer will result in an error.
  • float(x) converts x into a floating point number. Strings that don't represent a valid floating point number will result in an error.
  • list(x) converts any iterable x into a list.