Type conversion functions

Revision as of 22:00, 10 March 2011 by Smitschu (talk | contribs) (Created page with ''''Type conversion functions''' are used in programming to convert data to primitive datatypes. ==Type Conversion in Python== *<tt>'''str'''(x)</tt> returns a string…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.