Difference between revisions of "Type conversion functions"

(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…')
 
Line 6: Line 6:
 
*<tt>'''float'''(x)</tt> converts x into a [[floating point number]].  Strings that don't represent a valid floating point number will result in an error.
 
*<tt>'''float'''(x)</tt> converts x into a [[floating point number]].  Strings that don't represent a valid floating point number will result in an error.
 
*<tt>'''list'''(x)</tt> converts any [[python sequence types|iterable]] x into a [[list]].
 
*<tt>'''list'''(x)</tt> converts any [[python sequence types|iterable]] x into a [[list]].
 +
 +
[[Category:Introduction to Programming]]

Revision as of 23:03, 10 March 2011

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.